C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Variable declaration, initialization in Java

Just like C programming language, we can declare and initialize variables in Java too, here is the syntax

data_type variable_name;
variable_name=value;

OR

data_type variable_name=value;

Consider the following Integer declaration and initialization statements

int number;
number=0;

OR

int number=0;

We would recommend using second style of variable declaration and initialization, as such kind of declaration also stores the default value to the variable.

int number=0;

Other type of variables can also be declared following given syntaxes, consider the given example, here we are declaring some of the variables with initial (default) values to them and then we will print the values.

public class Numbers{

     public static void main(String []args){
		int 	a	=10;
		char 	b	='X';
		float 	c	=10.235f;
		String 	str	="Hello";
		
		System.out.println("a= "+a);
		System.out.println("b= "+b);
		System.out.println("c= "+c);
		System.out.println("str= "+str);
     }
}

Output

a= 10 
b= X
c= 10.235 
str= Hello






COMMENTS