Java – Data Types and Literals
Data Types and Literals– Variables are nothing more than reserved memory locations for storing values. This means that when you create a variable, you reserve some space in memory.
Based on the data type that is assigned to the variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, Java can store integers, decimals, or characters in those variables.
There are two types of data in Java:
- simple or primitive data types;
- reference data types (reference / object).
Primitive data types
There are eight data types supported by Java. Basic data types are predefined by language and named by keyword . Now let’s look in detail at these eight basic data types in the Java programming language.
- The byte data type is an 8-bit signed integer.
- The minimum value is -128 (-2 7 ).
- The maximum value is 127 (inclusive) (2 7 -1).
- The default is 0.
- byte is intended to save space in large arrays, mostly instead of integers, since byte is four times smaller than int.
byte a = 100; byte b = -50;
- The short data type is a signed 16-bit integer.
- The minimum value is -32768 (-2 15 ).
- The maximum value is 32,767 (inclusive) (2 15 -1).
- The short type in Java can also be used to conserve memory like byte. A short is 2 times smaller than an int.
- The default is 0.
short s = 10000; short r = -20000;
- In Java, the int data type is a signed 32-bit integer.
- The minimum int size is 2,147,483,648 (-2 31 ).
- The maximum value is 2,147,483,647 (inclusive) (2 31 -1).
- The int type is usually used for integer values. If there is no concern about memory.
- The default is 0.
int a = 100000; int b =-200000;
- The long data type is a signed 64-bit integer.
- The minimum value is – 9,223,372,036,854,775,808 (-2 63 ).
- The maximum value is 9,223,372,036,854,775,807 (inclusive). (2 63 -1).
- In Java Used when a wider range than int is required.
- The default is 0L.
long a = 100000L; long b =-200000L;
- The float data type is a single-precision 32-bit IEEE 754 floating point data type.
- The float type is mainly used to store memory in large arrays of floating point numbers.
- The default is 0.0f.
- The float type should never be used for an exact value, such as currency.
float f1 = 234.5f;
- The double data type is a double-precision 64-bit IEEE 754 floating point.
- Usually used for decimal values.
- The double type should never be used for an exact value, such as currency.
- The default is 0.0d.
double d1 = 123.4;
- The boolean data type represents one bit of information.
- There are only two possible values: true and false.
- Designed for simple traits that track true or false conditions.
- The default is false.
boolean one = true;
- The char data type is a single 16-bit Unicode character.
- The minimum value is “\ u0000” (or 0).
- The maximum value is “\ uffff” (or 65535 inclusive).
- In Java, char is needed to store any character.
char letterA ='A';
Reference data types
- Reference variables are created using defined class constructors. They are for accessing objects. These variables are declared with a specific type that cannot be changed. For example Employee, Puppy, etc.
- Class objects and various kinds of array variables fall under the reference data type .
- By default in Java, the value of any reference variable is null.
- A reference variable can be used to denote any object, declared or any compatible type.
Animal animal = new Animal("giraffe");
Literals in Java
Literal – representing the source code as a fixed value. It is represented directly in the code without any computation.
A literal in Java can be assigned to any variable from the base type. For instance:
byte a = 68; char a = 'A';
Byte, int, long, and short can be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8).
When using literals in Java, the 0 prefix is used to indicate the octal system, and the 0x prefix indicates the hexadecimal system. For instance:
int decimal = 100; int octal = 0144; int hexa = 0x64;
String literals in Java are specified as in most other languages by enclosing a sequence of characters between a pair of double quotes. Examples of string literals:
"Hello World" "two\nlines" "\"This is in quotes\""
The String and char literal types can contain any Unicode characters. For instance:
char a = '\u0001'; String a = "\u0001";
The Java language supports several special escape sequences for String and char literals:
|\ n||New line (0x0a)|
|\ r||Carriage return (0x0d)|
|\ f||Page feed (0x0c)|
|\ b||Backspace (0x08)|
|\ s||space (0x20)|
|\ “||Double quote|
|\ ddd||Octal character (ddd)|
|\ uxxxx||UNICODE hex character (xxxx)|