Java Variable DeclarationJava 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. Variable DeclarationThere 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
ExamplesJava Variable Declaration Example: With InitializationCreate several variables with the different data formats. Here, we can use int, String, Boolean and other data types.
CreateVariable.java Output: 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 InitializationJava language needs to create multiple variables with different data formats. Here, Java requires int, float, string, boolean, and other data types.
DeclareVariable.java Output: 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 MethodCreate 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. CreatediffVariable.java Output: 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 with Array dataJava 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
ArrayVariable.java Output: ConclusionJava 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. Next TopicNude Numbers in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India