Chapter 2_3:Java Variables

Java Variables

A variable is a name which contain a value that can be cahenged.Variables are containers for storing data values.

For example

Int i =10;Here, I is a variable which contain value 10 and int it is a datatype that represents that this variable can hold the integer typ of data.

Declaration of Variables:

To create a variable, you must define the type and assign it a value:

Syntax
type variable = value;

Here, type is one of datatypes (such as int or String), and variable is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

To create a variable that should store text, look at the following example:

Example

Create a variable called name of type String and assign it the value “Nikhil”:

public class DemoClass {

	public static void main(String[] args) {

		String name = "Nikhil";
		System.out.println(name);

	}

}

 You can also declare a variable without assigning the value, and assign the value later:

Example
public class DemoClass {

	public static void main(String[] args) {
		int myNum;
		myNum =15;
		System.out.println(myNum);
		
	}
}

Display Variables

The println() method is frequently used to display variables.

To combine both text and a variable, use the + character:

Example
public class DemoClass {

	public static void main(String[] args) {
		
		String name ="John";
		System.out.println("Hello "+ name);
	}

}

Output:

Hello John

For numeric values, the + character works as a mathematical operator (attention that we use int (integer) variables here):

Example
public class DemoClass {

	public static void main(String[] args) {
		
		int x = 7;
		int y = 9;
		System.out.println(x + y);
		
	}
}

From the example above, you can expect:

  • x stores the value 7
  • y stores the value 9
  • Then we use the println() method to display the value of x + y, which is 16
  • To declare more than one variable of the same type, use a comma-separated list:

For Example

public class DemoClass {

	public static void main(String[] args) {
		int x = 5,y = 6, z =50;
		System.out.println(x + y + z);
		
		
		
	}
}

Output:

61

Java Identifiers

All Java variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Variables naming convention in java

  • Names can contain letters, digits, underscores, and dollar signs
  • Names should begin with a letter
  • Names can also begin with $ and _ (but we will not use it in this tutorial)
  • Names are case sensitive (“myVar” and “myvar” are different variables)
  • Names should start with a lowercase letter and it cannot contain whitespace
  • Reserved words (like Java keywords, such as int or String) cannot be used as names

Types of Variables in Java

There are three types of variables in Java.
 

  1. Local variable
  2. Static (or class) variable
  3. Instance variable

Local Variable

A variable declared inside the body of the method is called local variable.These variables are declared inside method of the class. Their scope is limited to the method which means that You can’t change their values and access them outside of the method.

In this example, I have declared the instance variable with the same name as local variable, this is to demonstrate the scope of local variables.

Static (or class) Variable

A variable which is declared as static is called static variable. It cannot be local.Static variables are also known as class variable because they are associated with the class and common for all the instances of class. For example, If I create three objects of a class and access this static variable, it would be common for all, the changes made to the variable using one of the object would reflect when you access it through other objects.

Instance variable

A variable declared inside the class but outside the body of the method, is called instance variable.Each instance(objects) of class has its own copy of instance variable. Unlike static variable, instance variables have their own separate copy of instance variable. We have changed the instance variable value using object obj2 in the following program and when we displayed the variable using all three objects, only the obj2 value got changed, others remain unchanged. This shows that they have their own copy of instance variable.