Variables in Java
Variables
Variables are containers used to store value in program. There are different types of variables where we can store different kinds of values. You can use variables to store data and reuse it later in the program.
Note: We must give a unique name to a variable. The Unique name given to variables, methods, classes, etc., is an identifier.
Example: Variables in Java
public class Main {
public static void main(String[] args) {
// here a and name are variables.
int a = 10;
String name = "Bimal";
System.out.println("The value of a is "+a);
System.out.println("The value of name is "+name);
}
}
Assign Values to Variables
int age;
age = 20;
Here, we have assigned the value 20 to the variable age. We can also assign the value at the time of declaration.
int age = 20;
Types Of Variables:
They are called data types. We will learn more about data types later in this guide.
- String: For storing text values. E.g. “Ram” [Must be in double quotes]
- int: For storing integer values. [Decimal is not included] e.g. 10, -10, 8555
- float: For storing floating point value. [Decimal is included] e.g. 10.0, -10.2, 85.698
- char: For storing a single character. E.g. ‘A’, ‘a’, ‘z’ [Must be in single quotes]
- boolean : For storing true or false values.
How To Declare Variables: [Syntax]
type variableName = value;
- type is the data type of the variable.
- variableName is the name of the variable.
Example: Variables In Java
This program prints the value of variable address. The value of address is USA. Similarly, it prints the value of other variables.
public class Main {
public static void main(String[] args) {
//type variableName = value;
String address = "USA";
System.out.println(address);
int num1 = 10;
System.out.println(num1);
float price = 1008.52f;
System.out.println(price);
char gender = 'M';
System.out.println(gender);
boolean isMarried = false;
System.out.println(isMarried);
}
}
Note: Always try to give meaningful variable name. For example, age is a better variable name than just a, if you are storing age of a person.
Variables [Rules In Java]
- Declare type first. e.g
int i =1;
. - Variable name should not start with number.
- Variable names should not be keywords. [already defined words]
- Variable name should not contain space.
- Variables names are case sensitive.
Constant In Java
A constant is a variable in Java that has a fixed value. We cannot assign another value.
Syntax
final type variableName = value;
// Example
final float PI = 3.14f;
// We cannot assign other value in PI. if we assign it will give us error.
Note: Constant name should be written in capital letters. It makes your programs more understandable.
Example: Java Constant
public class Main {
public static void main(String[] args) {
// This is constant variable. We cannot change the value of PI.
final float PI = 3.14f;
// This is radius
float radius = 20.0f;
// This is formula for calculating area. i.e. PI * r * r
float area = PI * radius * radius;
// printing area
System.out.println("Area is "+area);
}
}
Literal In Java
Literal is a value that is assigned to a variable. It is a fixed value. We cannot change the value of literal. For example:
// Here 20, John, true are literals.
int age = 20;
String name = "John";
boolean isMarried = true;