Interacting With Java pt. 1

David Cha
The Startup
Published in
6 min readSep 21, 2020

--

As a bootcamp graduate, I have come a short-long way from not knowing any languages to knowing a few.

For software engineers that are aiming to become full-stack or backend developers, it is best to know languages that are Object Oriented such as Ruby, JavaScript, Python, Go, Elixir, and Java. Of course there are many more object oriented programming, but the main focus today will be Java. I wanted to learn it because, I had the need to learn a different backend language and I wanted to see if I can create a few fun projects with them! Those projects will come later, but for this reading I want to talk about Java basics.

Things you need to know

For an upcoming technical interview, it is best to know some of Java’s vocabulary. You should understand these basics words at least: abstraction, encapsulation, polymorphism, and inheritance. JavaScript also has these vocabularies, but they may contain different meanings.

Abstraction: Is knowing what the outcome is, but you don’t know how the process works. Think of putting a flat piece of paper into one side of the box and the box produces an origami at the other side. I don’t know how it folds the paper, but I do know that the flat piece of paper comes out as an origami.

Encapsulation: Is hiding a variable from other classes and methods. In Java we use the key reserved word “private” to prevent the variable from leaking out.

Polymorphism: In a generic sense, it is the ability to take on many forms. So a reference type in Java can change it’s form according to the user.

Inheritance: a subclass inheriting properties/values from the main class. In Java and every other object oriented programming language, it allows an object to take a similar property to that of it’s parents.

Data Types

Java has 8 primitive data types that hold a specific amount of space in memory.

You must declare the type and give it a name.
When you declare a data type without a value, Java will give the data type a default value.

In order to assign a value to a variable, you are required to assign a data type first, name the variable second, and give the value third. For example, I want to create a variable age. In order to write that in Java, you must declare the variable as an int then the name of our variable will be age and the value will be 24.

int age = 24; (like this)

You are also allowed to assign this variable to another variable. int herAge = age;

When you print out the variable herAge, the terminal will show 24. Quick thing to note, primitive types are copied by their value and these values are all independent from each other. So if I were to change the value of herAge to 48, that would also be valid.

Strings

In Java, strings are considered objects and they are immutable because this object is backed internally by a char array (sequence of characters). You are not allowed to mutate the size of an array, thus a string cannot be mutated. There are two ways that we can create a string which will be shown on Example 1.

Example 1
Example 2
My code editor for Java is IntelliJ

We are declaring the object type (String), then we are naming the object (a), and finally we are creating a value to a.

The other way to declare a string is to specify the object type and name then use the keyword new for the value (were allocating memory for this object).

I did mention before that we cannot mutate the string, but we can do it locally. On example 2, I have a variable called sayHello which points to “My name is Jeff”, then I change the value to “My name is not Jeff.” When I print out the variables it shows that I have changed the string object sayHello (locally). Java uses pass-by-value, not pass-by-reference. When I assign a new value to sayHello in my variable it only modifies the local sayHello, not the original sayHello in the calling code.

Reference Types

In Java, reference type is a data type that’s based on a class rather than on one of the primitive types. The class can be a class that is provided as part of the Java API class library or a class that you write yourself. When you create an object from a class, Java allocates the amount of memory the object requires to store the object. Then, if you assign the object to a variable, the variable is actually assigned a reference to the object, not the object itself. This reference is the address of the memory location where the object is stored.

To declare a variable using a reference type, you simply list the class name as the data type. For example, the following statement defines a variable that can reference objects created from a class called Date.

The Date class has some methods that we can use.

Date is part of the Java API class libraries that allows us to have access to some methods for this class of Date. Our variable now is an object from the Date class and we now have reference to the object through the variable.

Final, Finally, Finalize

These three words in the english dictionary have a similar term to one another, but in Java they all have a different meaning. It is also great to know about these three terms for a technical interview. Final is used to apply restrictions on class, method and variable (keyword). Final class can’t be inherited, final method can’t be overridden and final variable value can’t be changed. Finally is used to place important code, it will be executed whether exception is handled or not (block). Last but not least, finalize is used to perform clean up processing just before object is garbage collected (method).

https://www.geeksforgeeks.org/g-fact-24-finalfinally-and-finalize-in-java/ ← for more in-depth readings about the three Fs

There is so much we can do with Java and I will be discussing about the other types of functionality Java provides. I hope this clarifies some of the basic with you!

--

--

David Cha
The Startup

Interested in the structure of codes and chasing the Dopamine rush as I start to understand their structures.