Variable

Variable is name of reserved area allocated in memory. And all variables must be declared before they can be used. OR

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.


There are 3 Types of variables in java:

There are three types of variables in java

  1. local variable
  2. instance variable (Non-static fields)
  3. static variable (class variable)

Example to types of variables

				 
 class A{
int data=90;//instance variable
static int m=900;//static variable
void method(){
int n=90;//local variable
}
}//end of class
  1. Local Variable :- The variables defined in a method, constructors or block of code is called local variables. And Access modifiers cannot be used for local variables.
  2. Instance variables:- A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.and Instance variables can be declared in class level before or after use.

Example

import java.io.*;
public class Employee{
   // this instance variable is visible for any child class.
   public String name;
   // salary  variable is visible in Employee class only.

   private double salary;
   // The name variable is assigned in the constructor. 
   public Employee (String empName){

      name = empName;

   }
   // The salary variable is assigned a value.
   public void setSalary(double empSal){
      salary = empSal;

   }

   // This method prints the employee details.
   public void printEmp(){
      System.out.println("name  : " + name );
      System.out.println("salary :" + salary);
   }

   public static void main(String args[]){
      Employee empOne = new Employee("Ransika");
      empOne.setSalary(1000);
      empOne.printEmp();
   }
}

static variable:- Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

 import java.io.*;
 
public class Employee{
  // salary  variable is a private static variable
  private static double salary;
   // DEPARTMENT is a constant
  public static final String DEPARTMENT = "Development ";
   public static void main(String args[]){
  salary = 9000;
    System.out.println(DEPARTMENT+"average salary:"+salary);

   }

}

Note :- If the variables are access from an outside class the constant should be accessed as Employee.DEPARTMENT


Data Types in Java

There are two data types available in Java:

  • Primitive Data Types
  • Non Primitive Data Types
First

Primitive Data Types:

There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword.

Data Type Default size Default Value
Boolean 1 bit false
Char 2 byte '\u0000'
Byte 1 byte 0
Short 2 byte 0
Int 4 byte 0
Long 8 byte 0L
Float 4 byte 0.0f
double 8 byte 0.0d

java language supports few special escape sequences for String and char literals as well. They are:

Notation Character represented
\n Newline (0x0a)
\r Carriage return (0x0d)
\f Formfeed (0x0c)
\b Backspace (0x08)
\s Space (0x20)
\t tab
\" Double quote
\' Single quote
\\ backslash
\ddd Octal character (ddd)
\uxxxx Hexadecimal UNICODE character (xxxx)