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 and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.