Java Basics

Jimmy Gam
6 min readJan 27, 2021

TIL: RUN 1 out of 3 learning Java

So, my main tech stack has been frontend just because I remember hating taking lectures with C/C++ and all those other statically typed languages. On the other hand, dynamically typed languages such as python and javascript attracted me because I could get right into the coding rather than understanding background of static checks and etc. Anyways, now I am aware of python and Javascript fairly well enough but the problem is, I still need to get back to those statically typed languages and learn them for being a full stack. So, I did a little challenge to keep up with my self on backend knowledges. Starting with Java!

Integers

Integers, in java, when declared, will be saved in memories depending on the type of the integers. Here is the graph for the types of integers.

— — — Integer — — — char — — — Real — — — Boolean

1byte — byte — — — — char — — — — — — — — — — —

2bytes — short — — — char — — — — — — — — — — — —

4bytes — int — — — — — — — — — float — — — — — — —

8bytes — long — — — — — — — — double — — — — — — —

Basically, all the data types are saved in bytes but need to specify which data type depending on the data. Larger bytes can store larger number.

Before going into operators, let’s briefly discuss about constants. Constants are variables that do not change and you can declare constant with final keyword.

So all these data types can only interact with their own kinds? Of course, we cannot get sum of boolean value and an int value but how about float with int? This is where the implicit type casting comes in and basically, whenever type conversion to be occurred, it occurs in a flow of smaller byte to bigger bytes. So adding double with int will result a value of double type.

Operators

Operand is the values being used in computations and operator is a symbol that defines which computations.

Operator precedence

Briefly, you can assign a value to a new variable by using unary operator and you can have a new variable with sum of two other variables.

Classes and Objects

Classes in Java are integral especially understanding the concept of Object oriented programming. Classes itself, contain property values and methods. Property values define the variables in classes and methods are functions that are used in classes. Basically, a main file uses different classes and their properties and methods for a certain logic.

Class example

In the above code, there is a class called “Student.”(Also be careful with casings! Usually class begins with a capital letter) It has gotten other properties such as studentId, studentName, and etc. Then, two functions exist: showStudentInfor and getStudentName. These two functions differ in their return types: one is void(which does not return anything) and the other is a String.

This is very basics about classes but we can go a little further to it. A constructor is called with New keyword and it is there to initialize the properties of the class. When there is no constructor, the JVM automatically creates empty constructor for the class.(So I assume, class always needs to have a constructor?)

Constructor

Also you can declare property values with private keyword which is only accessible within the class. So in order to assign some values to properties of a class from outside of the class by calling an instance of the class, what should we do? That is why we need a getter/setter method that gets or sets the properties of the class.

The good old this also is widely used. This basically points to the address of a class that is sitting in the dynamically allocated memory in heap. We can use this in a constructor to simply call another constructor in the class when setting some values. The reason for doing this is for constructor overriding.(I think?)

The static variable is also very integral in Java class. When you create an instance, the instance lies within Heap memory. For instance, the earlier example of Student class, studentID, studentName, grade, and address all stay in HEAP memory when created as an instance. How about we create multiple students? How can we increment the student ID according to every instance that gets created? That is when we use static variables which will be saved in data and it should be accessed via class rather than instance.(You can through instance but there exists a warning) How do we increment? In the constructor, we can simply increment the static variable in this case, studentID and the trick is done!

Lastly, I want to wrap it up with a singleton pattern. As many of you guys heard, this is probably one of the most famous design patterns in programming. It restricts the instantiation of a class to one “single” instance. Imagine, we want to build a system that a company produces multiple cars from building various kinds of engines, tires, windows, and etc. We can make engines, tires, or even cars as classes. But, can we simply make a company as a class? The answer is no. There should be only one company that produces all those goods and this is where singleton pattern restricts a single instance instantiation.

singleton pattern

As you can see, in class Company, there is private Company constructor and it is called by private static Company. This, will not be exposed to other classes so there is another method that can expose this private instance, getInstance. Now, through this, we can simply use class Company but privately and following singleton pattern.

Array and Array List

In Java, we can declare array in several ways. The two simple ways are the following:

  1. dataType [] nameofArray = new dataType[numberofElements]
  2. dataType nameofArray [] = new dataType[numberofElements]
array for loop

This picture demonstrates how to declare array and assign values to each element. So this is pretty simple stuff but there is one tricky thing we should be always careful about: “copy”

When you try to copy an array from the other, what is the simple logic? Create another array and use arraycopy, a builtin method in Java. However, after using arraycopy, the newly copied array changes when we change the original array. Why does that happen? Because, using arraycopy, we simply copied the memory reference of the original array so if we are changing the values of memory address specifically being looked by two arrays, those arrays will display the changed values. The trick is, we need to create new instances for the copied array!

ArrayList is basically a two dimensional array. Let’s look at the code.

Two dimensional array

It is no different from array except you just have two []s when declaring and just fill in the values with double for loops.

--

--

Jimmy Gam

Hello, I am a software engineer @Amazon. Any technical discussions are welcome so please contact me at jgam@alumni.nd.edu