What are the 8 basic data types in java with examples

Josephine 51 Published: 12/11/2024

What are the 8 basic data types in java with examples

I'm happy to help you with that!

In Java, there are eight basic data types that are used to store values in variables. These data types are:

Byte: This is an 8-bit signed integer type that ranges from -128 to 127.

Example: byte myByte = 42;

Short: This is a 16-bit signed integer type that ranges from -32768 to 32767.

Example: short myShort = 1234;

Int: This is a 32-bit signed integer type that ranges from -2147483648 to 2147483647.

Example: int myInt = 987654321;

Long: This is a 64-bit signed integer type that ranges from -9223372036854775808 to 9223372036854775807.

Example: long myLong = 123456789012345L;

Float: This is a single-precision 32-bit floating-point type that represents numbers with a minimum value of 1.4E-45 and a maximum value of 3.4E38.

Example: float myFloat = 3.14f;

Double: This is a double-precision 64-bit floating-point type that represents numbers with a minimum value of 4.9E-324 and a maximum value of 1.8E308.

Example: double myDouble = 2.71e+5;

Boolean: This is an 8-bit signed integer type that can take on the values true or false.

Example: boolean myBoolean = true;

Char: This is a 16-bit unsigned integer type that represents Unicode characters with values ranging from 0 to 65,535.

Example: char myChar = 'A';

It's worth noting that in addition to these basic data types, Java also provides several reference types, such as:

String Array Object

These types are used to represent objects and arrays in your program.

What are the 8 basic data types in java javatpoint

I'll respond in English as per your request!

In Java, there are eight basic data types that are predefined and can be used directly without declaring them. These data types are:

Byte: The byte data type is an 8-bit signed integer with a minimum value of -128 and a maximum value of 127.
byte b = 100;

Short: The short data type is a 16-bit signed integer with a minimum value of -32,768 and a maximum value of 32,767.
short s = 30000;

Int: The int data type is a 32-bit signed integer with a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647.

int i = 1000000;

Long: The long data type is a 64-bit signed integer with a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807.
long l = 1000000000L;

Float: The float data type is a single precision floating point number with a minimum value of -3.4E38 and a maximum value of 3.4E38.
float f = 10.5f;

Double: The double data type is a double precision floating point number with a minimum value of -1.7E308 and a maximum value of 1.7E308.
double d = 12.456789;

Boolean: The boolean data type has two possible values: true and false.
boolean b = true;

Char: The char data type is a single character with a minimum value of 'u0000' (the null character) and a maximum value of 'uffff'.
char c = 'A';

These basic data types are the building blocks for more complex data structures, such as arrays and objects. They can be used to store and manipulate different types of data in your Java program.

Note: The actual values mentioned above may vary based on the specific JVM (Java Virtual Machine) being used.