Java primitive data types

Variables are reserved memory locations for storing values. When a variable is created, it reserves some space in memory. Based on the variable's data type, the operating system allocates memory and decides what can be stored in reserved memory. Therefore, by assigning different data types to variables, it is possible to store integers, decimals or characters in these variables.

There are two data types in Java −

  • primitive data type
  • Reference/Object Data Type

1. Primitive data types

Java supports eight primitive data types. Primitive data types are predefined by the language and named by keywords. Let's understand and learn these eight primitive data types in detail.

Types of describe range of values Defaults example
byte Represents a 81-bit signed two's complement integer -128 (-2^7)-127 (2^7 -1) 0 byte a = 100, byte b = -50
short Represents a 161-bit signed two's complement integer -2^15-2^15 -1 0 short s = 10000, short r = -20000
int Represents a 321-bit signed two's complement integer -2^31-2^31 -1 0 int a = 100000, int b = -200000
long Represents 64a signed two's complement integer -2^63-2^63 -1 0L long a = 100000L, long b = -200000L
float Represents a single-precision 32bit IEEE 754 floating-point number - 0.0f float f1 = 234.5f
double Represents a double- 64bit IEEE 754 floating-point number - 0.0d double d1 = 123.4
boolean Represents a bit of information true/false false boolean one = true
char Represents a single 16-bit Unicode character \u0000-\uffff - char letterA = 'A'

 

2. Reference data type

  • Create a reference variable using the constructor defined in the class. They are used to access objects. Declare these variables to be of a specific type that cannot be changed. For example, Employee, Dogclass, etc.
  • Class objects and array variables of various types are reference data types.
  • The default value for any reference variable is null.
  • A reference variable can be used to refer to any object of the declared type or to any compatible type.
  • Example:Dog dog = new Dog("ask");

 

3. Java literals

A literal is a source code representation of a fixed value. They are represented directly in the code without any calculations. Literals can be assigned to any primitive type variable. E.g -

byte a = 68;
char a = 'A';

byte, int, longand shortcan also be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8) numbers.

The prefixes 0are used to indicate octal and the prefixes 0xindicate hexadecimal when using these number systems for literals. E.g-

int decimal = 100;
int octal = 0144;
int hexa =  0x64;

String literals in Java are specified by enclosing a sequence of characters between a pair of double quotes, just like they do in most other programming languages. Examples of string literals are
 
"Hello World"
"two\nlines"
""This is in quotes""
Literals of type string and character can contain any Unicode characters. E.g 
 
char a = '\u0001';
String a = "\u0001";
Java  also supports several special escape sequences for String and char literals. They are
 
Symbol Character

\n

line break(0x0a)

\r

Return (0x0d)

\f

form feed(0x0c)

\b

backspace(0x08)

\s

Space (0x20)

\t

Tabs

\"

Double quotes

\'

Apostrophe

\\

backslash

\ddd

octal(ddd)

\uxxxx

hexadecimal UNICODE

(xxxx)