In the previous section, we have discussed the variables and data types we usually use in Java programming. Consequently, in this lesson, we are going to focus on the Java initialization.
Initialization
Assigning a value for the variables is termed as the initialization. Well, if you are a beginner, it will be hard to understand this. For them, please look at the differences between the declaration and initialization. We hope that it will be sufficient to understand easily.
Difference between declaration and initialization
The process of defining a variable and data type is the declaration in Java. Let’s say an example;
char x; // we declared just the variable and data type
On the other hand, Assigning a value into that variable is the initialization. Observe the example below;
int x =1; // we initialized 1 as the value of the variable x
In an easy word, if you didn’t assign any values in the variables in the first line of your variables and data types, then it is a declaration. If you assigned the values, it is an initialization.
Dynamic Initialization
Let’s start with an example. If you want to find the circumference of a circle, what will you do? You will first insert the value of the radius. And then, you will find a circle circumference using the formula,
circumference = 2* pi *r
Here the amount you got for the circumference variable is the dynamic initialization of the variable r. Therefore we can say that Dynamic initialization can be obtained using the passing parameters and constructors values to the constructor. So think about the example below,
package main; public class First{ public static void main( String args[] ) { int r; double circumference; r= 6; //Initialization circumference = 2 * Math.PI * r; //Dynamic Initialization System.out.println(" circumference= " + circumference); } }
Output:

Difference between initialization and dynamic initialization
Assigning a value with the declared variable is initialization. For example, int x=2
, double y= 4.8
, etc. On the other hand, The calculated value from the initial variable through some formulas or mathematical solution is Dynamic Initialization. Check out the previous example related to the circumference of a circle.
Java Initialization of Array
We all know that the array is a collection of more than one data with the same data types. The array code is standard programming knowledge in every programming language and to assign an array in Java, we should follow three steps.
- First, declare the data type you want to initialize, e.g., int, char, double, etc.
- Put “[]” this sign right after the data type. It specifies the declared variables in the array.
- Type your array name. It can be anything related to your Programming.
Now, if you want to assign your values, just directly put the values inside the third column “{}.” Or you can follow the second two steps.
- Put a “new” after the equal sign and then type the declared data type.
- After that, declare your array “[].” Put your values in the third column. After that, don’t forget to put a semi-colon after the initialization.
Remember, both of them have a similar function. But we will demonstrate both of the examples. Let’s check out an example. You will understand the sequence quickly.
First way
package main; public class First{ public static void main( String args[] ) { int [] array = {2,5,7,8,9, 10}; for (int i = 0; i <= 5; i++){ System.out.println("Array= " + array[i]); } } }
Second way
package main; public class First{ public static void main( String args[] ) { int [] array; array = new int[] {2, 5, 7, 8, 9, 10}; for (int i = 0; i <= 5; i++){ System.out.println("Array= " + array[i]); } } }
Output

We have learned the necessary information about java initialization and dynamic initialization. So you are an intermediate java programmer, maybe you will look for how to initialize a list in Java. Well, we can do this in several ways. For example,
- List.add()
- Arrays.asList()
- Collections class methods such as, Collections.addAll(), Collections.unmodifiableList(), Collections.singletonList().
- Java 8 Stream
- Java 9 List.of()
Afterall, stay with infolinux, we are here to bring you the most straightforward java tutorials with easy codes and illustration. Each of the contents is well explained and demonstrated. Therefore, stay with us and follow our lessons. We can assure you we will learn enough to be a java developer.
Pingback: Java Format Specifiers & sub-specifiers - Info Linux
Pingback: Java User Input | Scanning and Taking input in Java - Info Linux