Java Variables

A variable is a container which holds the value while the java program is executed. A variable is assigned with a datatype.

Variable is a name of memory location. There are three types of variables in java: local, instance and static.

A Java variable is a piece of memory that can contain a data value. A variable thus has a data type. Data types are covered in more detail in the text on Java data types.

Variables are typically used to store information which your Java program needs to do its job. This can be any kind of information ranging from texts, codes (e.g. country codes, currency codes etc.) to numbers, temporary results of multi step calculations etc.

There are two types of data types in java:
  1. primitive
  2. non-primitive.
Primitive Data Types

The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name, as you've already seen:

int gear = 1;


non-primitive.

Non primitive data types are called reference types in Java and they refer to an object. They are created by the programmer and are not defined by Java like primitives are. A reference type references a memory location where the data is stored rather than directly containing a value.

Variable

Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of "vary + able" that means its value can be changed.

variables in java
int age=31;//Here data is variable
Types of Variables

There are three types of variables in java:

  • local variable
  • instance variable
  • static variable
types of variables in java
1) Local Variable

A variable declared inside the body of the method is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists.

A local variable cannot be defined with "static" keyword.

2) Instance Variable

A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static.

It is called instance variable because its value is instance specific and is not shared among instances.

3) Static variable

A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.

Example to understand the types of variables in java
class A{ int data=50;//instance variable static int m=100;//static variable void method(){ int n=90;//local variable } }//end of class


Java Variable Example: Add Two Numbers
class Simple{ public static void main(String[] args){ int a=20; int b=30; int c=a+b; System.out.println(c); }}


Output:
20
import java.util.Scanner; class Sumoftwo { public static void main(String[] args) { int x,y,z; Scanner p=new Scanner(System.in); System.out.println("Enater x value:"); x=p.nextInt(); System.out.println("Enater y value:"); y=p.nextInt(); z=x+y; System.out.println("Sum of two numbers:"+z); } }


Output:
Enater x value: 50 Enater y value: 30 Sum of two numbers:80
Java Variable Example: Widening
class Simple { public static void main(String[] args){ int a=20; float f=a; System.out.println(a); System.out.println(f); }}


Output:
20 20.0
import java.util.Scanner; class Example { public static void main(String[] args) { int a; float f; Scanner p=new Scanner(System.in); System.out.println("Enetr a value:"); a=p.nextInt(); f=a; System.out.println("Display a value:"+a); System.out.println("Display f value:"+f); } }


Output:
Enetr a value: 20 Display a value:20 Display f value:20.0
Java Variable Example: Narrowing (Typecasting)
class Simple{ public static void main(String[] args) { float f=10.5f; //int a=f;//Compile time error int a=(int)f; System.out.println(f); System.out.println(a); }}


Output:
10.5 10
import java.util.Scanner; class Example { public static void main(String[] args){ float f; Scanner p=new Scanner(System.in); System.out.println("Enter f value:"); f=p.nextFloat(); //int a=f;//Compile time error int a=(int)f; System.out.println("Display f value:"+f); System.out.println("Display a value:"+a); } }


Output:
Enter f value: 50 Display f value:50.0 Display a value:50

A variable is a container which holds the value while the java program is executed. A variable is assigned with a datatype.

Java Program to Find ASCII Value of a character(Typecasting)
import java.util.Scanner; public class ChartoAscii { public static void main(String[] args) { int c; Scanner p=new Scanner(System.in); System.out.print("Enter a character:"); c =p.next().charAt(0); int i=(int)c; // cast from a char to an int System.out.println("Charactor to Ascii:" + i); } }


Output:
Enter a character:c Charactor to Ascii: 99
Java Program to Find ASCII Value of a character(Typecasting)
import java.util.Scanner; class SciitoChar { public static void main(String[] args) { int x; Scanner p=new Scanner(System.in); System.out.println("Enter x value"); x=p.nextInt(); char y=(char)x; System.out.println("Ascii to charactor :"+y); } }


Output:
Enter x value 99 Ascii to charactor :c
Java Variable Example: Overflow
class Simple{ public static void main(String[] args){ //Overflow int a=130; byte b=(byte)a; System.out.println(a); System.out.println(b); } }


Output:
130 -126
import java.util.Scanner; class Example { public static void main(String[] args){ //Overflow int a; Scanner p=new Scanner(System.in); System.out.println("Enter any value"); a=p.nextInt(); byte b=(byte)a; System.out.println(a); System.out.println(b); } }


Output:
Enter any value 50 50 50 Enter any value 700 700 -68
Java Variable Example: Adding Lower Type
class Simple { public static void main(String[] args){ byte a=10; byte b=30; //byte c=a+b;//Compile Time Error: because a+b=40 will be int byte c=(byte)(a+b); System.out.println(c); } }


Output:
40



Instagram