java Declaration and Initialization

Declaration and Initialization

In Java programming, declaring and initializing variables as needed is a fundamental programming concept. Remember, variable declaration and initialization are essential tasks in any Java program to store, manage, and process variable data. Remember, variable declaration and initialization define how programmers can create, declare, and initialize new variables as needed, and assign unique values ​​to these declared custom user variables.

java Declaration and Initialization

So, let’s better understand variable declaration and initialization in Java programming.

Variable Declaration in Java.

Declaring a variable in a Java program means that the programmer defines a meaningful program variable name and a data type, allowing the Java compiler to allocate memory for the variable and store and process the value within the program. Variable declarations help programmers declare multiple variables in a single program and provide program value.

Syntax of variable declaration in Java.

dataType variableName;

Element of a variable declaration.

  • Variable dataType – This is how a programmer defines the behaviour of the declared variable in a Java program, such as the data type of int, double, String, float, etc.
  • variableName – This is the short name for the program storage variable declared in a Java program. It holds a numeric value from the time the variable is declared and until the end of the program execution. For example, int p, float salary, string emp_name[100], etc.

Examples of Java variable declarations.

int value; // Here an integer variable named value is declared.

double temp; // Here a double variable named temp is declared.

String info; // Here a string variable named info is declared.

Here only program variables are declared, while no values ​​have been assigned to them yet.

Variable initialization in Java.

Variable initialization in Java programming means manually assigning a value to a variable by a programmer, either at the time of variable declaration or later in the Java program. Here the program variable value declared by you must exactly match the data type.

Syntax of Java variable initialization.

variableName = value;

Variable syntax elements.

  • variableName – This is the name of the variable declared in the program and initialized in the program.
  • value – This is the numeric, text, or string value assigned to the variable in the program.
  • With this, Java programmers can combine the variable declaration and initialization processes by declaring them together in a single step.

Examples of Java variable initialization.

value = 7; // Here the value variable is initialized with the value 7

temp = 9.78; // Here the temp variable is initialized with the value 9.78

info = “Welcome”; // Here the info variable is initialized with the value “Welcome”

Java programmers can also declare and initialize a variable value in the same program statement.

Example of combined Java variable declaration and initialization.

int value = 7; // Here the ‘value’ variable is declared and initialized to the integer value 7

double temp = 9.78; // Here the ‘temp’ variable is declared and initialized to 9.78

String info = “Welcome”; // Here the ‘info’ variable is declared and initialized to the “Welcome” string data type

Types of Variable Initialization in Java.

In any Java program, you have three basic steps to initialize variables.

Direct variable initialization/direct value assignment in Java.

This is the most basic variable initialization process in a Java program, in which Java programmers directly assign a value to a program variable during or after its declaration.

int p = 4; // Here an integer variable value is directly initialized.

double salary = 999.49; // Here a double variable is directly initialized.

Default variable initialization/for instance variables.

Remember, if Java programmers declare a variable value within a class program, but do not assign or provide an initial value, a default value is assigned to that variable based on the data type of the variable. This is preferable to the instance variable and array element declaration process in Java.

For numeric data types (byte, short, int, long, float, double) – the default numeric value is 0 for integers, and 0.0 for floating-point data types.

For the Boolean data type – the default Boolean value is defined as false.

For the char data type – here the default value ‘\u0000’ is defined as the null character value.

For reference data types (object, array) – here the default value for object and array is defined as null.

Example of default variable initialization.

public class DefaultValueInitialize {

int p; // Here the default value of p variable int is 0 define

boolean Islogic; // Here the default value of Islogic variable boolean value is false define

String emp_name; // Here the default value of a String variable emp_name value is reference type is null value define

public static void main(String[] args) {

DefaultValueInitialize obj = new DefaultValueInitialize();

System.out.println(“Variable p value is – ” + obj.p); // Result is – 0

System.out.println(“Variable Islogic value is – ” + obj.Islogic); // Result is – false

System.out.println(“Variable emp_name value is – ” + obj.emp_name); // Result is null

}

}

For variable initialization/instance variables via the Java constructor.

In Java programming, instance variables of an object-oriented programming class are mostly initialized via the constructor. Constructors in Java are special methods for creating new objects. Java constructors are used to initialize and create new class objects.

public class Employee {

String emp_name;

int emp_age;

// Here we create a constructor for initializing the instance variables

public Employee(String emp_name, int emp_age) {

this.emp_name = emp_name;

this.emp_age = emp_age;

}

public static void main(String[] args) {

Employee employee = new Employee(“Bhavishi”, 22); // Here the constructor initializes the instance of the variables.

System.out.println(“The Employee Name is – ” + employee.emp_name);

System.out.println(“The Employee Age is – ” + employee.emp_age);

}

}

Local vs. Instance vs. Static Java Variables.

Local Java Variable Declaration.

Variables can be declared inside methods, constructors, or blocks in a Java program.

But here, no default value is assigned to them, so you must initialize them before using them.

Here, in a Java program, they can only be accessed within the class method in which they are declared.

public class LocalVariableDeclaration {

public static void main(String[] args) {

int localVariable = 90; // Local variables here must be initialized before use in the program

System.out.println(“Java Local variable – ” + localVariable);

}

}

Instance Java variable declaration.

Declared inside a class in a Java program but outside any method, constructor, or block.

If a variable is not explicitly initialized here, it is automatically initialized with a default value in the Java program.

public class Employee {

int emp_age; // Instance variable declared here with the default value 0

String emp_name; // Instance variable defined here with the default value null

}

Static Java variable declaration.

Static variables are declared with the static keyword in a Java program, and they are shared by all instances of the current class.

Static variables are initialized only once, when the Java class is loaded into the program. Like instance variables, if a static variable is not initialized explicitly, it is automatically assigned a default value.

public class Limiter {

static int limit = 0; // Here, the limit static variable is initialized with 0.

public static void increment() {

limit++;

}

public static void main(String[] args) {

Limiter.increment();

System.out.println(“The limit value is – ” + limit); // Result is – 1

}

}

Constant Java Variables/Use of the final Keyword.

In Java programming, a constant variable is a variable declared to have a value that cannot be modified once assigned. Constant data types in Java are declared using the final keyword.

Constant data types in Java are typically declared as static final variables so that they can be easily accessed within the program without creating an instance of the class.

Example Java program with a constant.

public class ConstantsVariable {

/ Let’s declare a constant variable data type

public static final double TEMP = 78.98;

public static void main(String[] args) {

/ Here we are using the constant variable value

System.out.println(“The value of Temp is – ” + TEMP);

// Uncommenting the following line here will result in a program error

// TEMP = 78.98; // Display Error – Here it cannot assign a value to a final variable

}

}

Here TEMP is a constant declared variable, and once initialized in the program, its value cannot be modified.

Detail Explanation of java variable Declaration and Initialization

Variable ConceptDescription aboutExample type
Variable DeclarationHere we Define the variable’s data type and name of variable without assigning any manual value.int values;
Variable InitializationHere we Assigning an initial value to a declared program variable value according to need.values = 7;
Combined variable Declaration & InitializationHere we Declaring and initializing a variable in only one step.int integer = 9;
Default variable InitializationHere Default variable values assigned to the instance variables.int p; // Default value define is 0
Constructor variable InitializationHere Initializing program variables using a constructor method.Employee p = new Employee (“Siddhi”, 27);
Final Constants variableHere we can Declare a constant variable with the final java keyword method.public static final double TEMP = 89.24;
Array values InitializationHere we can Declare and initializing arrays element.int [] array = {9, 8, 7, 4};

Java Array Initialization.

After declaring arrays in a Java program, programmers can manually initialize them by defining values ​​at the time of declaration or using a constructor.

Example of array initialization with Java values.

int[] values ​​= {7, 9, 8, 3, 2, 1}; // Example of array initialization with Java values

Example of array initialization using the new keyword.

int[] values ​​= new int[7]; // Here, the array with size 7, values, is initialized with the default value (0).

Example of assigning values ​​to an array.

numbers[0] = 9;

numbers[1] = 8;

numbers[2] = 7;

numbers[3] = 6;

numbers[4] = 2;

numbers[5] = 1;

numbers[6] = 4;

Leave a Reply