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 in Java Variables are one of the vital building block of any Java Program. Variables are used to represent reserved memory locations that is used to store values, when we create a variable we are a suppose 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 variable.

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 a integer values.

In Java, every variable have data type associated to it, data type for a variable defines –

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

In 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 are predefined by the language and for which the programming language provides built-in support. The Java supports eight primitive data types, which 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 bytes(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 default value of zero(0). the byte can be declared using 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 default value of zero(0). The short can be declared using 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 default value of zero(0). the int can be declared using 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 float keyword.

Example:-

double :- A double is used to represent a 64-bit floating-point number and numbers with larger decimal point value. An uninitialized double has default value of 0.0 (0.0d). Double can be declared using 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 default value false. Boolean are commonly used in decision making statements. Boolean can be declared using 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). Character are declared using single quotes. A char type occupies two bytes of memory space.The char are 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 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.