Tag Archives: java variables

Java variables

Variables store data for processing.A variable is given a name (or identifier), such as name, age, height, and the like. The name uniquely identifies each variable, assigning a value to the variable and retrieving the value stored.Variables have types.

For examples:

  1. int: for integers (whole numbers) such as 224 and -456002

2. double: for floating-point or real numbers with optional decimal points and fractional parts in fixed or scientific notations, such as 3.1416, -73.66,3.12e10.

3.String: for texts such as “Hello” or “Good Evening!”. Text strings are enclosed within double quotes.
You can declare a variable of a type and assign it a value.
Example:
String name = "sani kamal";
int age=25;
double interest=45.98;
This creates three variables called name of type String, age of type integer and interest of type double and assigns value “sani kamal”,25,45.98 respectively.
It is important to note that a variable is associated with a type, and is only capable of storing values of that particular type. For example, an int variable can store integer values, such as 445; but it cannot store real numbers, such as 19.34, or texts, such as “Hello Rashmi”.
Examples of variable declarations:
class DemoClass {
 public static void main(String[ ] args) {
 String name ="sani kamal";
 int age = 25;
 double score =55.9;
 char group = 'A';
 }
}

char stands for character and holds a single character.
Another type is the Boolean type, which has only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
For example:

 boolean isOpen = true;
You can use a comma-separated list to declare more than one variable of the specified type. Example:
 String a = "Hello John ", b = "India";