Variable Declaration In Java

You can declare a variable in Java same as C, C++. It has the same syntax which is given below:
type var-name;

Where type is the data type which a variable will hold. As given in program, println( ) is used to display the string "The value of num : 12"

The print( ) method is same as println( ), except that it does not output a newline character after each call.

  This is a second Java program.
  Call this file as "".

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 : ");

The output of program is as below:

C:\>java VarDeclDemo
The value of num : 12
The value of num + 10 : 22

No comments:

Post a comment