Data Types in Java
Data types refer to the process of defining the structure and characteristics of data.
data types are mainly categorized into two types.
-
Primitive Data types: Primitive types are predefined and standard data types in Java.There are 8 Primitive data types in Java that are boolean, char, byte, int, short, long, float, and double.
- boolean:boolean is a primitive data type. It is used to store only two values, either true or false.
Syntax:
boolean a1=true;
boolean a2=false;
- char:The char data type is used to store a single character. The character must be enclosed by single quotes.
Syntax:
- byte:In Java, the byte data type is one of the primitive data types and it represents an 8-bit signed integer. It can hold values in the range of -128 to 127 (inclusive). The byte data type is often used when you need to optimize the memory space.
Syntax:
- int:In Java, int is a primitive data type representing a 32-bit signed integer. It can hold values in the range of approximately -2 billion to 2 billion.Whole numbers without precision (decimal points) are called "integer numbers."
Syntax:
- short:16-bit signed integer, range approximately -32,768 to 32,767.
Syntax:
- long: 64-bit signed integer, larger range than int.
Syntax:
- float:In Java, the float data type is a primitive data type that represents single-precision 32-bit floating-point numbers.
Syntax:
- double:
In Java, the double data type is a primitive data type that represents double-precision 64-bit floating-point numbers.
Syntax:
- Non Primitive Data Types
- arrays:An array is a collection of values, and these values are stored under a single variable name. Each value in the array is accessible by its index or position within the array.
Syntax:
- strings: group of characters enclosed in double quotes is indeed called a "string."
Syntax:
- classes: A class encapsulates both data (attributes or properties) and functions (methods or behaviors) that operate on that data.