Java contains built-in data types, also known as primitive data types. Programmers use these built-in data types in Java to create more complicated user-defined data types. Each data type requires a particular number of bits. For instance, an integer needs 8 bits or 4 bytes, while a boolean requires 1 bit alone. Hence, the boolean data type can support only true and false values.
Other data types like char requires 16 bits, short 16 bits, long 64 bits, float 32 bits, and double 64 bits.
How to declare a variable for particular data types in JAVA?
The process of specifying the type of data a variable can hold is known as variable declaration. Java uses various data types some of them include int, double, and char. Each data type formats specifies specific kinds of values.
For Example, the format for declaring variables includes
int count; char ch;
How to Assign Values to Variables?
When assigning values to variables, Java adheres to proper syntax. It is in the following format: variableName = value. As a result, count = 7 is the format to follow, assigning the integer value 7 to the variable. Likewise, ch = ‘D’ is the format to follow while assigning character ‘D’ to the variable ch.
How to Declare One or More Variables:
While declaring if we use more than one variable, it is better to use a comma. Because it acts as a separator between the two variables.
For Example, int age, marks; is one such that uses a comma.
Declaring in Single Step:
Importantly, the programmer can complete the declaration and initialization in a single step alone.
For Example,
int count = 20, age=30;
char ch= ‘A’
Dynamic Initialization:
Dynamic initialization is possible for a variable by using the following method,
int a = 20, b = 30;
int c = a + b;
What is Dynamic Initialization?
In Java, dynamic initialization means initializing variables during runtime. However, this process is different from the declaration process. While declaration declares the creation of functions and variables, initialization involves assigning values to variables.
In simpler terms, giving a name to a variable is known as variable declaration. And assigning a value to the variable is known as initialization.
For instance, data_type variable_name; (is the sample code format for declaring a variable.)
Meaning of Initialization:
Although the variable declaration is over, the initialization is yet to complete. As a result, the variable is without a value.
The initialization of the variable requires assigning a value to the variable.
Such as int employeeID = 446;
Now, a value of “446” completes the assignment to the variable name, employeeID.
We can do the variable declaration even without initializing the variable. However, the program does not allow the use of an uninitialized variable. If the programmer uses an uninitialized variable, there will be a ReferenceError.
As a result, good practice involves initializing variables along with the variable declaration. This practice will help in avoiding errors while making the code more readable.
What are Arrays in Data Types in JAVA?
Arrays are a particular kind of data type. Arrays help store the same type of data elements in consecutive memory locations. During declaration, we can specify or fix the size of an array.
For Example, char names [7];
Data can be either an object or a primitive data type. An array can access the individual elements by specifying the name and number. Soon after creating an array, space allocation occurs inside the memory to store elements of an array. This process is also known as array instantiation. Depending on the array type during array instantiation, every array element gets assigned a default value. For instance, if an array is ‘int’ type, then every array element has ‘zero’ initialization; similarly, for a Boolean, it is false. This kind of initialization is known as ‘auto initialization.’
How to Declare an Array?
By assigning the ‘new’ keyword, we can declare an array. Space allocation occurs in the memory after creating the ‘new’ keyword.
char in[] = new char[12]; It is an example for declaring an array.
The above statement can create an array with 12 characters with the name ‘in.’ Notably, the system stores elements within the ‘in’ Array in contiguous memory locations. The following represents a sample arrangement of the Array.
What is In-line Initialization?
In-line initialization allows for the initialization of array elements during the declaration itself. In such a case, it is not necessary to specify the array size. For instance,
int a[] = {50, 60, 70, 80, 90, 100} ;
What are Strings in Java?
A String is an object in Java that stores a sequence of characters in a specific order. String in Java is a non-primitive data type. The ‘String’ class in Java simplifies the handling of consecutive characters. In the following Example, we will see the creation of an instance of the String class.
String strName1 = “Rome”;
String strName2 = new String(in a day”);
An operator helps in combining two or more strings.
Strname3 = strName1 + ” wasn’t built ” + strName2;
‘strName3’ specifies, “Rome wasn’t built in a day”
Using a particular function in the String class will help calculate the length of a string. For Example, we will take the ‘strName1’ defined earlier. Here, the ‘numLength’ variable will contain four as the value.
int numLength = strname1. length ();
We can also add a numeric type to a string. For Example,
String strVar = “Test” + 2
The variable ‘strVar’ will contain “Test2” as the value.