Online Tutorials & Training Materials | STechies.com
Register Login

Create/ Use of Variables in Java Programs

|| || 0

Create/ Use of Variables in Java Programs
Stechies

How to Create and Use Variables in JAVA Programs?

In this tutorial, we will see how to create and use Variables in JAVA programs.

As we already know that Variables are the name given to the memory location which is used to store data type.

Before creating variables first, we must specify the type of data we want to be store in the variable.

Creating variable to store integer

public class Program {

public static void main(String[] args) {

int marks; //declare the variable//

marks = 80; //assigning the value to variable//

System.out.println(marks);

}
}

Declaring multiple variable

public class Program {

public static void main(String[] args) {

int marks = 80; age = 24; //declaring multiple variable//

System.out.println(marks);

System.out.println(age);

} }

Declaring Char datatype

public class Program {

public statics void main(String[] args) {

char grades = 'A' //declaring char variable//

System.out.println(grades);

}
}

Declare variable for float variable

public class Program {

public static void main(String[] args) {

double average = 19.3 //declaring double variable//

System.out.println(average);

Declaring a variable to store a boolean value

public class Program {

public statsic void main(String[] args) {

boolean areyouIndian = false //declaring boolean varaible//

System.out.println(areyouIndian);

Declaring different variable types in same JAVA program

We cannot declare different types of variable in the same line therefore we have to declare them all in different lines.

public class Program {

public static void main(String[] args) {

boolean areyouIndian = false;
float= average;
int age = 21;

System.out.println(areyouIndian);
System.out.println(average);
System.out.println(age)
}
}

 


Related Articles