Java Data Types

Data Types in Java

Data types in Java are fundamental to the language. They define the kind of values that variables can hold and how those values behave. In this tutorial, we'll explore Java's data types, from simple primitive types to more complex reference types, ensuring that learners of all levels can grasp this crucial concept.

Primitive Data Types

Definition: Primitive data types represent single values and have predefined behaviors in Java.

Examples:

  • int: Stores whole numbers, e.g., 5, -10, 0.
  • double: Stores floating-point numbers, e.g., 3.14, -0.01.
  • boolean: Stores true or false values, e.g., true, false.
  • char: Stores a single character, e.g., 'A', '1', '$'.

There are eight primitive data types in Java:

Data TypeSizeDescription
byte1 byteStores whole numbers from -128 to 127
short2 bytesStores whole numbers from -32,768 to 32,767
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits
boolean1 bitStores true or false values
char2 bytesStores a single character/letter or ASCII values

Numbers in Java - Integers and Floating-Point

In Java, numbers come in two flavors: integers (whole numbers) and floating-point (decimal numbers). Let's break them down, keeping it concise and easy to understand.

Integer Types

byte:

Range: -128 to 127. Memory-efficient for small numbers.

Example
byte age = 25;

short:

Range: -32768 to 32767. Useful for a wider range of numbers.

Example
short distance = 5000;

Int:

Range: -2147483648 to 2147483647. Commonly used for whole numbers.

Example
int population = 100000;

Long:

Range: -9223372036854775808 to 9223372036854775807. For very large values; end with 'L'.

Example
long bigNumber = 15000000000L;

Floating-Point Types

You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.

The float and double data types can store fractional numbers. Note that you should end the value with an "f" for floats and "d" for doubles:

Float & Double Ex:
float price = 5.75f;
double pi = 3.1415;
  • Choosing Between float and double:

Use float when memory is a concern, but remember it has lower precision. For most calculations, it's safer to use double, which provides higher precision (about 15 digits).

Boolean Types

boolean: Stores either true or false.

Exmaple
boolean flag1=true;
boolean flag2=false;

Character

char: Stores a single character, enclosed in single quotes, e.g., 'A'. Example: char myGrade = 'A';

Alternatively, if you are familiar with ASCII values, you can use those to display certain characters .

Example : char myVar1 = 65 // output := A

Non-primitive / Reference Data Types

Non-primitive data types are called reference types. Reference data types don't store the data itself but store references (memory addresses) to objects.

Examples: String: Stores sequences of characters. User-defined classes: Custom data types created by the programmer.

Contact Us

Name
Email
Mobile No:
subject:
Message: