Data Types in Java

Selinnur Göl
2 min readApr 9, 2021

--

Data types are divided into two groups:

  • Primitive data types — includes byte, short, int, long , float, double, char, boolean
  • Non-primitive data types — Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.

Character;

char

The char data type is used to store a single character. The character must be surrounded by single quotes, ‘A’ ‘b’…

A
B
C

Integer ;

Integer types, stores whole numbers, positive or negative (such as 123 or -456), without decimals. Which type you should use, depends on the numeric value.

Byte

The byte data type can store whole numbers from -128 to 127.

Size:

1 byte ( 8 bits )
126
127
-128
-127

Short

The short data type can store whole numbers from -32768 to 32767

Size:

2 byte ( 16 bits )

int

The int data type can store whole numbers from -2147483648 to 2147483647.

Size:

4 byte ( 32 bits )

long

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807.

Size:

8 byte ( 64 bits )

Floating point types;

float

Size:

4 byte ( 32 bits )

Use a float if you need to save memory in large arrays float types

double

Size:

8 byte ( 64 bits )

Use double or float ?

Boolean;

boolean

can only take the values true or false :

true
false

Non- Primitive Data Types

String

Strings are defined as an array of characters.

// Declare String without using new operator 
String s = "CentralPark";

// Declare String using new operator
String s1 = new String("CentralPark");

References:

--

--