In this article we will take a look on

  • Different data types available in java thier classfication and usage.
  • Literals that are present in java and thier meaning.
  • Different variable types thier classfication and example describing them.

Data types in java

Data types in java

Variables are reserved memory locations to store values. When you create a variable you reserve some space in the memory.

Based on the data type of the variable system allocates specific amount of space in the memory and decides what can be stored in that. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

There are two data types available in Java −

  • Primitive Data Types
  • Reference/Object Data Types

Primitive data types

There are eight primitive data types in java. These are pre-defined by language and named by a keyword. Let’s look into them in detail. The size of the primitive data types will not change from one operating system to another, as its size is pre-defined and cannot be changed.


  • Byte data type is an 8-bit signed two's complement integer
  • Minimum value is -128
  • Maximum value is 127
  • Default value is 0
  • This data type is basically used to save space in large arrays, in place of integers, since a byte is four times smaller than an integer.
  • Example: byte s = 100, byte w = -50


  • Short data type is a 16-bit signed two's complement integer
  • Minimum value is -32,768
  • Maximum value is 32,767
  • This data type can also be used to save memory as byte data type. A short is 2 times smaller than integer
  • Default value is 0.
  • Example: short s = 10000, short w = -20000


  • Int data type is a 32-bit signed two's complement integer.
  • Minimum value is - 2,147,483,648
  • Maximum value is 2,147,483,647
  • Integer is generally used as the default data type for integral values unless there is a concern about memory.
  • The default value is 0
  • Example: int s = 100000, int w = -200000


  • boolean data type represents one bit of information
  • There are only two possible values: true and false
  • This data type is used in places where we need to track true/false conditions
  • Default value is false
  • Example: boolean one = true


  • Long data type is a 64-bit signed two's complement integer
  • Minimum value is -9,223,372,036,854,775,80
  • Maximum value is 9,223,372,036,854,775,807
  • This type is used when a wider range than int is needed
  • Default value is 0L
  • Example: long s = 100000L, long w = -200000L


  • Float data type is a single-precision 32-bit floating point
  • Float is mainly used to save memory in large arrays of floating point numbers
  • Default value is 0.0f
  • Float data type is never used for precise values
  • Example: float s = 234.5f


  • double data type is a double-precision 64-bit floating point
  • This data type is generally used as the default data type for decimal values.
  • Double data type should never be used for precise values such as currency
  • Default value is 0.0d
  • Example: double s1 = 123.4


  • char data type is a single 16-bit Unicode character
  • Minimum value is '\u0000' (or 0)
  • Maximum value is '\uffff' (or 65,535 inclusive)
  • Char data type is used to store characters
  • Example: char letterS = 'S'

Array and string doesn’t come under primitive data types. One of the main reason for that is Primitive data types has limitation that they have fixed size and can hold data of that type only but String on the other hand can vary in size and it can hold any type of data using its wrapper class.

Reference data types

  • Reference variables are created using constructors of the classes. These are used to access objects. These variables are declared to be of a specific type. For example, Student, Plant, etc.
  • Class objects, various type of array and string variables come under reference data type.
  • Default value of any reference variable is null.
  • A reference variable can be used to refer any object of the declared type or any compatible type.
  • Example: Student s = new Student("abcd");

Up casting and down casting

Up casting is casting to a super type, this is by default done by the compiler and doesn’t require any external commands to perform the operation. The up casting is always allowed by java compiler.

down casting is casting to a subtype. down casting involves a type check and can throw a Class cast exception. Down casting is done using code. Down casting in simple terms can be explained as subclass type refers to a parent of object class.

Below example shows how downcasting is performed.

class Teacher { }  
class Student extends Teacher {  
  static void method(Teacher t) {  
       Student s=(Student)t;//downcasting  
       System.out.println("ok downcasting performed");  
   public static void main (String [] args) {  
    Teacher t=new Student();  

Output: Ok downcasting performed

Literals in java

These are used to represent constant values. Literals are source code representation of a fixed value or the sequence of characters which represents the constant value that is to be stored in a variable. There are five different kinds of literals present in java.

  • Integer
  • Floating point
  • String
  • Character

Integer Literals

The number without decimal is the integer literals. The java compiler treats all the integer literals as int by default. We can assign different values to integer literal. They are:

Decimal Literals –

These literals doesn’t need any prefix.
For example: int d = 10;

Binary Literals –

The binary literal is prefixed with ‘0b’.
For example: int x = 0b1010; //the value in decimal is 10

Hexadecimal Literals –

The hexadecimal literal is prefixed with ‘0x’.
For example: int a = 0x17; //the value in decimal is 23

Octal Literals –

The octal literals are prefixed with ‘0’.
For example: int y = 023; //the value in decimal is 19

Long Literals –

The long literals are suffixed with ‘l’ or ‘L’.
For example: long b = 34562780l; // the value in decimal is 34562780

Floating point

These are used when we want to represent a decimal value with a fractional component. These basically contains two literals one for representing floating point values and another one for representing double values Floating point values are represented by ‘f’ and double is represented by ‘d’


These are basically a sequence of characters enclosed in double quotes. These string literals have to occur in a single line.


There are so many characters literals that are used in java and each has its own usage. Here are some of the character literals listed down below. Character

Variables in java

Variables are basically named storage places in java. These provide us with memory space which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

A variable has to be declared first before we can make use of that variable. Data type variable name ( = value ) if you want to assign value to the variable.

Here are some of the valid variable declarations in java

int a,b;
int s=10,w=20;
float f=2.154f;
double d=6.145528;
char c='c';

Various variable types available in java are
  • Local variables
  • Instance variables
  • Class/static variables

Local variables

  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
  • Access modifiers cannot be used for local variables.
  • Local variables are visible only within the declared method, constructor, or block.
  • Local variables are implemented at stack level internally.
  • There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.

Example showing the local variable declaration and utilisation in java.

public class Student{
   public void StuAge() {
      int age = 0;
      age = age + 7;
      System.out.println("Student age is : " + age);

   public static void main(String args[]) {
      Test test = new Test();

Output: Student age is 7

Instance variables

  • Instance variables are declared in a class, but outside a method, constructor or any block.
  • When a space is allocated for an object in the heap, a slot for each instance variable value is created.
  • Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
  • Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.
  • Instance variables can be declared in class level before or after use.
  • Access modifiers can be given for instance variables.
  • The instance variables are visible for all methods, constructors and block in the class. Normally, it is recommended to make these variables private (access level). However, visibility for subclasses can be given for these variables with the use of access modifiers.
  • Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference.VariableName.

Here is an example showing the intialisation and utilisation of instace variables in java

public class Student {

   // this instance variable is visible for any child class.
   public String name;

   // salary  variable is visible in Employee class only.
   private int age;

   // The name variable is assigned in the constructor.
   public Student (String stuName) {
      name = stuName;

   // The salary variable is assigned a value.
   public void setAge(int stuage) {
      age = stuage;

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

   public static void main(String args[]) {
      Student stuOne = new Student("Chertech");

Output : name : Chertech
age : 10

Class/Static Variables

  • Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
  • There would only be one copy of each class variable per class, regardless of how many objects are created from it.
  • Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from their initial value.
  • Static variables are stored in the static memory. It is rare to use static variables other than declared final and used as either public or private constants.
  • Static variables are created when the program starts and destroyed when the program stops.
  • Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.
  • Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.
  • Static variables can be accessed by calling with the class name ClassName.VariableName.
  • When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables are not public and final, the naming syntax is the same as instance and local variables.

Here is an example indicating static variable initialisation and utilisation in java

public class Student {

   // age  variable is a private static variable
   private static int age;

   // DEPARTMENT is a constant
   public static final String NAME = "Chertech ";

   public static void main(String args[]) {
      age = 10;
      System.out.println(NAME + "age :" + age);

Output : Chertech age : 10

About Author

This is Krishna.I am the author of this blog. I am a technology enthusiast. If you found this article helpful please share it with your friends. Please provide your valuable comments on this blog.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions