JAVA-Datatypes - Notes By ShariqSP
Data Types in Java
Data types in Java are used to specify the type of data that a variable can hold. They define the size and format of the data and determine the operations that can be performed on the data. Java supports two main categories of data types: primitive data types and reference data types.
Primitive Data Types
Primitive data types are the basic building blocks of data in Java. They represent simple values and are stored directly in memory. Java provides eight primitive data types:
- byte: 8-bit signed integer
- short: 16-bit signed integer
- int: 32-bit signed integer
- long: 64-bit signed integer
- float: 32-bit floating-point number
- double: 64-bit floating-point number
- char: 16-bit Unicode character
- boolean: Represents true or false values
Syntax and Examples
Syntax for Variable Declaration:
dataType variableName;
Example:
int age;
double height;
boolean isStudent;
Initializing Variables:
dataType variableName = value;
Example:
int count = 10;
double pi = 3.14;
boolean isActive = true;
Reference Data Types
Reference data types are used to store references to objects. They do not hold the actual data but rather point to the memory location where the data is stored. Java provides various reference data types, including classes, interfaces, arrays, and enums.
Data Type Details
Data Type | Size (in bits) | Range |
---|---|---|
byte | 8 | -128 to 127 |
short | 16 | -32,768 to 32,767 |
int | 32 | -231 to 231-1 -2,147,483,648 to 2,147,483,647 |
long | 64 | -263 to 263-1 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 32 | Approximately ±3.40282347E+38F Approximately ±3.40282347 x 1038 |
double | 64 | Approximately ±1.79769313486231570E+308 Approximately ±1.79769313486231570 x 10308 |
char | 16 | 0 to 65,535 |
boolean | 1 | true or false |