Data Types in Java


Data Types Java PrimitiveViews 11505

Data Types in Java denotes the type of value a variable can hold. In the previous article, we have seen how to declare a variable. In this tutorial, we will learn about different data types and how to use them in variable declaration. We will discuss Primitive Data Types and Non-Primitive Data Types in this article.

For any variable we declare, a data type is a must since it denotes how much memory the variable requires to store the value. Let us recollect the variable declaration. For example, in the below code, we have declared and initialized an integer variable with the value 10.

int a = 10;

There are 2 categories of Data Types in Java:

  • Primitive data types –  This includes byte, short, int, long, char, double, float, and boolean.
  • Non Primitive data types – This covers String, Array, Class, and Interface.

Data Types in Java

In this tutorial, we will learn in detail about Primitive data types. Non Primitive Data types of String and Array are covered in separate tutorials.

Primitive Data Types in Java

There are 8 different types of Primitive Data Types in Java that specify the type and value of the variable.

Data TypeSizeDescriptionDefault value
byte1 byteStores whole numbers from -128 to 1270 (zero)
short2 bytesStores whole number from -32768 to 327670 (zero)
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,6470 (zero)
long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
float4 bytesStores fractional numbers up to 6-7 decimal digits0.0f
double8 bytesStores fractional numbers with up to 15 decimal digits0.0d
char2 bytesStores single character/letter'\u0000'
boolean1 bitStores true or falsefalse

Byte Data Type in Java

Byte data type in Java stores whole numbers between the range -128 to 127. This data type is mainly used to save memory since it is 4 times smaller than int and when we know that the whole number is within this limit.

public class DataTypeDemo {

  public static void main(String[] args) {
    byte b = 100;
    System.out.println(b);
    
  }
}
100

If we initialize a byte variable with a value beyond the specified limits, it will throw a compilation error.

public class DataTypeDemo {

  public static void main(String[] args) {
    byte b = 130;
    System.out.println(b);
    
  }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
  Type mismatch: cannot convert from int to byte

  at DataTypeDemo.main(DataTypeDemo.java:5)

Short Data Type in Java

The short data type is greater in size than byte but less than an integer. It can hold values between -32768 to 32767. This data type in Java also saves memory when compared to an integer. This also throws “Type mismatch” error if we initialize values beyond the limit.

public class DataTypeDemo {

  public static void main(String[] args) {
    short s = 10000;
    System.out.println(s);
    
  }
}
10000

Int Data Type in Java

Int is the most commonly used data type in Java to store whole numbers. It can store values in the range of -2,147,483,648 to 2,147,483,647.This is nothing but -2^31 to 2^31 – 1

public class DataTypeDemo {

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

Long Data Type in Java

We use a Long data type in Java when we need to store a value that is greater than the integer limit. It has capacity between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 which is in the range of -2^63 to 2^63 – 1. This is not used very frequently.

public class DataTypeDemo {

  public static void main(String[] args) {
    long l = 1023435235235235L;
    System.out.println(l);
    
  }
}
1023435235235235

Float Data Type in Java

We use the Float data type in Java to store a fractional value which is a single-precision 32 bit IEEE754 floating point. This data type is smaller than double but we cannot use this to store precise fractional values.

public class DataTypeDemo {

  public static void main(String[] args) {
    float f = 4.5678f;
    System.out.println(f);
    
  }
}
4.5678

Double Data Type in Java

Double data type in Java also holds a fractional value but of double-precision 64 bit IEEE 754 floating-point. We can use this for decimal values similar to float.

public class DataTypeDemo {

  public static void main(String[] args) {
    Double d = 56.567891234d;
    System.out.println(d);
    
  }
}
56.567891234

Char Data Type in Java

We use the char data type in Java to store a single character or letter. It denotes a 16-bit Unicode character and value ranges between 0(‘\u0000’) to 65535(‘\uffff’)

public class DataTypeDemo {

  public static void main(String[] args) {
    char c ='j';
    System.out.println(c);
    
  }
}
j

Boolean Data Type in Java

This is another most commonly used data type in java which stores values like true or false. We use this as flags for conditional purposes.

public class DataTypeDemo {

  public static void main(String[] args) {
    boolean b;
    int a = 4;
    int i = 8;
    if(a>i)
      b = true;
    else
      b = false;
    System.out.println(b);
    
  }
}
false

Non-Primitive Data Types in Java

Non-Primitive Data types in Java include String, Array, Class, and Interface. We can also call them as Reference Data Types. We will cover in detail about Non-Primitive Data types in upcoming tutorials.

String in Java

A string is another most commonly used data type that denotes an array of characters. The value is always enclosed within double-quotes(” “).

String str = "Java Programming";

Array in Java

An array can hold multiple values of the same data type. We can use an array to store any type of data.

String[] names = {"Ram","Leela","Tejas"};
int[] num = {3,6,12,89,24};

Class in Java

A class in Java contains several methods and variables. We need to create an instance of the class in order to use them.We can use a single object to access any data inside the class. For example, we create an instance or object named d if we want to access any methods or variables inside a class.

public class DataTypeDemo {

  public static void main(String[] args) {
    DataTypeDemo d = new DataTypeDemo();
    
  }
}

Interface in Java

An interface is just like a class having only functions or variables but no implementation. The implementation of these functions will be somewhere else. In other words, it just tells what a class does and not how it does.

//interface
interface StudentDetails {
  public void getStudentName();
  public void getStudentDepartment();
}

//implementation of the methods
public class Student implements StudentDetails {
  
  @Override
  public void getStudentName() {
    
  }

  @Override
  public void getStudentDepartment() {
    
  }
}

Reference

Translate »