Data Types in Java

The data types in Java tells us what type of value you will store in the variable. If you make a variable string type so you only store the string value, not an integer value. In java, There are two types of data types.

  1. Primitive data types
  2. Non-primitive data types

Primitive data types

Primitive data types are a special group of data types that do not use the keyword new when initialized. Java creates primitives as automatic variables that are stored in stack memory.

Java has eight primitive data types that are used to store data during a program’s operation.

  1. Boolean data type
  2. byte data type
  3. char data type
  4. short data type
  5. int data type
  6. long data type
  7. float data type
  8. double data type
data types

Boolean Data Type

A boolean is a data type that has two possible values either true or false. It represents 1 bit of information.

Example: boolean flag=true;

Byte Data Type

The bye data type is an 8-bit signed two’s complement integer. It stores integers from -128 to 127. It has a minimum value of -128 and a maximum value of 127. The byte data type can be useful for saving memory in large arrays.

Example: byte first=20;

Char data type

Char data type stores a 16-bit Unicode character. The size of the char data type is 2 bytes.

Example: char a='A';

Short data type

The short data type is a 16-bit signed two’s complement integer. It stores integers from -32,768 to 32,767. As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

Example: short a=5454;

Int data type

Int data type stores integers from -2,147,483,648 to 2,147,483,647. The int data type is a 32-bit signed two’s complement integer. Use the Integer class to use int data type as an unsigned integer.

Example: int num=45;

Long data type

It stores integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.It is a 64-bit two’s complement integer. The size of int is 8 bytes .

Example: long num=3459214;

Float data type

Float data type stores a positive or negative decimal number from 1.4023×10 ^ -45 to 3.4028×10 ^ +38. It is a single-precision 32-bit IEEE 754 floating-point.

Example: float f=234.5;

Double data type

This data type stores a positive or negative decimal number from 4.9406×10 ^ -324 to 1.7977×10 ^ +308. It is a double-precision 64-bit IEEE 754 floating-point.

Example: double d=000234.5;
Data Type Size Example
boolean undefined (represents 1 bit of information) true/false
byte 1 byte (8 bits) 20
char 2 bytes ‘A’
short 2 bytes 5454
int 4 bytes 45
long 8 bytes 3459214
float 4 bytes 234.5
double 8 bytes 000234.5

Non-primitive data types

Non-primitive types are created by the programmer and It is not defined by Java like primitive data type except for String class. It is called reference types because they refer to objects.

Non-primitive types start with an uppercase letter and it is used to call methods to perform certain operations. StringsArraysClassesInterfaces, etc are an example of a non-primitive data type.


Do you know?
Data Types in Java This article is contribute by Study4Geeks If you want share your knowledge on Study4Geeks then go to login section.