Java Datatype

In this tutorial you will learn about the Java Datatype and its application with practical example.

Java Datatype

Like in any other programming language Java Variables are one of the vital building blocks of any Java Program. Variables are used to represent reserved memory locations that are used to store values, when we create a variable we are supposed to allocate some memory space for that variable. Java is a statically typed programming language, which means that variables always have a specific type and that type cannot change. In Java, all variables must first be declared before they can be used. This involves stating the variable’s data type and name of the variable. Get clear your fundamentals of java datatype here itself.

Table Of Contents

Example:-

Here, age is a variable, and the data type of the variable is int. The int data type determines that the age variable can only contain an integer value.

In Java, every variable has a data type associated with it, the data type for a variable defines –

  • The amount of memory space allocated for variables.
  • A java datatype specifies the possible values for variables.
  • The operations that can be performed on variables.

Java data types can be broadly classified as –

  • Primitive Data Types
  • Non-Primitive Data Types

Java Primitive Data Types

The primitive data type is a basic building block provided by the Java programming language. The primitive data type is predefined by the language and for which the programming language provides built-in support. Java supports eight primitive data types, which are further classified into four groups –

Integer:- Integers are used to store whole numbers. Java supports four different integer types, varying internal sizes for storing signed.

Type Contains Default Size Range
byte Signed integer 0 8 bit or
1 byte
-27 to 27-1 or
-128 to 127
short Signed integer 0 16 bit or
2 bytes
-215 to 215-1 or
-32,768 to 32767
int Signed integer 0 32 bit or
4 bytes
-231 to 231-1 or
-2147,483,648 to 2147,483,647
long Signed integer 0 64 bit or
8 bytes
-263 to 263-1 or
-9223,372,036,854,755,808 to 9223,372,036,854,755,807

byte:- It is 1 byte (8-bits) integer data type. It can hold values ranging from -128 to 127. It’s used instead of int or other integer data types to save memory if the value of a variable will be within -128 to 127. An uninitialized byte has a default value of zero(0). the byte can be declared using the byte keyword.

Example:-

short:- It is 2 bytes(16-bits) integer data type. It can hold values ranging from -32768 to 32767. An uninitialized short has a default value of zero(0). The short can be declared using a short keyword.

Example:-

int:- It is 4 bytes(32-bits) integer data type. It can hold values ranging from -2147483648 to 2147483647. An uninitialized int has a default value of zero(0). the int can be declared using the int keyword.

Example:-

long :- It is 8 bytes(64-bits) integer data type. It can hold values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. An uninitialized long has default value of zero(0). the long can be declared using long keyword.

Example:-

Floating Point Number:- The floating-point numbers are used to store numbers that contain a decimal component (real numbers). Java supports two different types of floating-point numbers.

Type Contains Default Size Range
float IEEE 754 floating point
single-precision
0.0f 32 bit or
4 bytes
±1.4E-45 to
±3.40282347E+38F
double IEEE 754 floating point
double-precision
0.0 64 bit or
8 bytes
±439E-324 to
±1.7976931348623157E+308

float:- A float is used to represent a 32-bit floating-point number and numbers with smaller decimal points. An uninitialized float has default value of 0.0 (0.0f). Float can be declared using the float keyword.

Example:-

double:- A double is used to represent a 64-bit floating-point number and numbers with larger decimal point values. An uninitialized double has default value of 0.0 (0.0d). Double can be declared using the double keyword.

Boolean:- The Boolean data type is used to represent the truth values, which can be either True or False. An uninitialized float has a default value of false. Boolean is commonly used in decision-making statements. Boolean can be declared using the boolean keyword.

Type Contains Default Size Range
boolean true or false false 1 bit true or false

Example:-

Characters (char):- The character data type is used to hold the single literal (16-bit Unicode character). Characters are declared using single quotes. A char type occupies two bytes of memory space. The char is declared char keyword.

Type Contains Default Size Range
char Unicode character
unsigned
\u0000 16 bits or
2 bytes
0 to 216-1 or
\u0000 to \uFFFF

Example:-

Java Non-Primitive Data Types

In Java, non-primitive data types are those derived from primitive data types or defined by the programmer itself such as Classes, Interface, Arrays, etc.

In this tutorial we have learn about the Java Datatype and its application with practical example. I hope you will like this tutorial.