Javatpoint Logo
Javatpoint Logo

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.

  1. Data-type
  2. Variable name
  3. Initial value
  4. Semicolon

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.

Variable Declaration

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

  • For example: String my_name = "Java coder";
  • We initialize data with a given variable and display it as an output.
  • The way of declaration works on the default method of the class.

Declare a Variable without Initial Value

  • For example: String my_name;
  • We do not need to initialize data with a given variable.
  • Assign value in any method and display it as an output.
  • The way of declaration works inside and outside of the default method.
  • The variable data is displayed inside of the default method of the class.

Examples

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.

  • Create variables with required data types in the default method.
  • Use variable name and its value.
  • Return this value in the method as per data format.

CreateVariable.java

Output:

Java Variable Declaration

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.

  • Create variable in the default method.
  • Initialize value with the respective variable name and data type.
  • Then return value in the method.

DeclareVariable.java

Output:

Java Variable Declaration

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.

  • Create variables in the customized method.
  • Return variable in the method.
  • Initialize a value in the default method.
  • The variable declaration does not need a semicolon due to the argument format.

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.

CreatediffVariable.java

Output:

Java Variable Declaration

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.

SameVariable.java

Output:

Java Variable Declaration

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.

Or

Or

Java Variable Declaration Example: Array Data

  • Java variable declaration using array with array size shows below.
  • Initialize the data with variable name and index.
  • Display the variable data as per requirement.

ArrayVariable.java

Output:

Java Variable Declaration

Conclusion

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA