Table of Contents
Back Button
Back to Chapter
No items found.

Java Data Types

Introduction:

Data types are the kind of data that variables hold in a programming language. A data type is an attribute of data that tells the compiler or interpreter how the programmer plan to use the data. There are two types of data types in Java:

  1. Primitive data types ( intrinsic or built-in types )
  2. Non-primitive data types (derived types )

Data types

Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false values
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values

1. Primitive Data Types :

Primitive types are predefined (already defined) in Java. A primitive type starts with a lowercase letter. The primitive data types include char, byte, short, int, long, boolean, float, and double.

char :

The char data type is used to store a single character. The character must be surrounded by single quotes.

char grade = 'A';//A is a charater System.out.println(grade);

byte:

The byte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory.

byte num = 120; System.out.println(num);

short:

The short data type can store whole numbers from -32768 to 32767.

short num = 6000; System.out.println(num);

int:

The int data type is the preferred data type when we create variables with a numeric value. The int data type can store whole numbers from -2147483648 to 2147483647.

int num = 100000; System.out.println(num);

long:

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L".

long num = 16000000000L; System.out.println(num);

boolean:

A boolean data type is declared with the boolean keyword and can only take the values true or false.

boolean male = true; boolean female = false; System.out.println(male); // Outputs true System.out.println(female); // Outputs false

float:

The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f".

float num = 4.75f; //System.out.println(num);


double:

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a "d".

double num = 18.85d; System.out.println(num);


2. Non-Primitive Data Types

Non-primitive types are created by the programmer. non-primitive types start with an uppercase letter. The non-primitive data types include Classes, Interface, Strings, and Arrays.

Classes:

A class is a group of objects which have common properties. It is a user-defined data type with a template that serves to define its properties. To create a class, use the keyword class. Let's learn the class in detail in upcoming chapters.

public class Main { // Main is the class name   int num = 20;   void getdata(){   }   void putdata(){   } }


Interfaces:

The interface keyword to create an interface. An interface is slightly different from the class. It contains only ****constants and method declarations.

interface car   // car is the interface name{   void start();   void stop();  }


Strings:

The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes.

String greeting = "Hello World"; System.out.println(greeting);


Arrays:

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets.

String[] cars = {"Audi", "BMW", "Ford"}; int[] num = {10, 20, 30, 40};


Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?