Variable Declaration in Java
Declaring variables in Java
Just like in C or C++, Java allows you to declare variables using a familiar syntax:
type variableName;
Here, type is the data type which a variable will hold.
Example
Here’s a simple program that demonstrates variable declaration and usage:
/*
This is a second Java program.
Call this file as "VarDeclDemo.java".
*/
public class VarDeclDemo {
public static void main(String[] args) {
int num; // This declare a variable called num
num = 12; // This assigns a value to num
System.out.println("The value of num : " + num);
num = num + 10;
System.out.print("The value of num + 10 : ");
System.out.println(num);
}
}
OUTPUT
C:\>javac VarDeclDemo.java
C:\>java VarDeclDemo
The value of num : 12
The value of num + 10 : 22
Comments
Post a Comment