Java Variable Declaration
Java programming language requires variables to operate and handle data. Java creates several variables as per data format and data types. The variable declaration means creating a variable in a program for operating different information.
The Java variable declaration creates a new variable with required properties. The programming language requires four basic things to declare a variable in the program.
Data-type: It represent the type of value variable.
Variable name: The Java variable declaration requires a unique name. We prefer to declare small and understandable variable names.
Initial value: Java language requires the initial value of the variable. Declare variable with initial value does not necessary in the main class. We must assign the initial value in the default constructor. The "final variable" needs to declare the initial value.
Semicolon: The semicolon represents the end of the variable declaration statement.
There are two ways to declare a variable in Java. The first method is to assign the initial value to the variable. The second method declares variable without initial value.
Declare a Variable with Initial Value
Declare a Variable without Initial Value
Java Variable Declaration Example: With Initialization
Create several variables with the different data formats. Here, we can use int, String, Boolean and other data types.
Here, the output displays several types of variable values. Java variable declaration is necessary to allocate data memory and display relevant data.
Java Variable Declaration Example: Without Initialization
Java language needs to create multiple variables with different data formats. Here, Java requires int, float, string, boolean, and other data types.
We can declare all types of data variables in the Java programming language. The variable store's multiple data type's information.
Java Variable Declaration Example: Parameterize Method
Create variable in default and parameterize method. Here, you can declare a variable as an argument in the method. Assign value in the method and display it as an output.
Example with different data type variables:
Java variable declaration creates similar data type variables in the method. The following example returns integer and string data simultaneously.
Example with same data type variables:
Java variable declaration creates similar data type variables in the method. We can operate and display variable data in parameterize method. The given example displays a similar type of data.
Java Variable with Array data
Java requires variable declaration in non-primitive values. Java needs variables for class, array, interfaces, and other functions. The array required a variable to store data and allocate the memory.
There are three ways to declare array variables in Java.
Java Variable Declaration Example: Array Data
Java variable declaration creates variables and occupies space in the memory. The variable defines values that can be varied or unstable. The variable declaration uses the Java abstraction, interfaces, classes, and methods. We declare and operate variables in any method of the Java code.