In this tutorial you will learn about the Variables Initialization Program in Java and its application with practical example.
In this tutorial, we will learn to create a Java to Program Variables Initialization Program in Java using java programming.
Prerequisites
Before starting this tutorial, we assume that you are the best aware of the following Java programming concepts.
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
Variables Initialization in Java
Just like other programming languages, we can declare and initiate variables in Java, here is the syntax and example of how we can declare a variable and initiate values of a variable.
“Data_Type => variable_name”
variable_name=value;
int a= 10;
Variables Initialization Program in Java
In this program, we will find the smallest element in an array using a nested for loop. We would first declare and initialized the required variables. Next, we will find the smallest number in an array. Let’s have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// java program how to define variable and initiate variable public class Variables{ public static void main(String []args) { //Declaring variables and initiating values.. int var =5; char var1 ='A'; float var2 =6.0f; String var3 ="Hello"; //Printing the values of variables System.out.println (" var= "+ var); System.out.println (" var1= "+var1); System.out.println (" var2= "+var2); System.out.println (" var3= "+var3); } } |
Output
Printing the values of variables
In the above program, we have first declared and initialized a set of variables required in the program.
- var = it will hold integer value.
- var1 = it will hold character value.
- var2 = it will hold float value.
- var3 = it will hold string value.
After declaring variables we initiate values in variables.
Now we will print the values of the variables.
What is the difference between Declaration vs. Initialization?
Declaration:=>
The process of Declaration is by defining the variable along with its type and name.
Example:=> we are declaring a variable named var variable.
int var;
Initialization:=>
here we assign a value to a variable as shown below
Example: Var = 10;