In this tutorial, we will learn about variables in Java language. We will cover the basics of declaring and using variables, including naming conventions.
Variables in Java are used to store data values that can be manipulated during program execution. They have a data type that determines the kind of data they can hold, such as integers, strings, or boolean values.
When naming variables in Java, follow these conventions:
The syntax to declare variables in Java is:
data_type variable_name;
num
.public class Main {
public static void main(String[] args) {
// Declare and initialize an integer variable
int num = 10;
// Print the value of the variable
System.out.println("The value of num is: " + num);
}
}
The value of num is: 10
name
.public class Main {
public static void main(String[] args) {
// Declare and initialize a string variable
String name = "John";
// Print the value of the variable
System.out.println("The value of name is: " + name);
}
}
The value of name is: John
isTrue
.true
to the variable.public class Main {
public static void main(String[] args) {
// Declare and initialize a boolean variable
boolean isTrue = true;
// Print the value of the variable
System.out.println("The value of isTrue is: " + isTrue);
}
}
The value of isTrue is: true