- What is the data types? A data type is a classification that tells a programming language what kind of value a variable can hold and what operations can be performed on it.
02.How many data types?
There are two data types
Data types:
Primitive data types
Non primitive data types
03.What are the Primitive data types?
There are 8 primitive data types
byte, short, int, long, float, double, char & boolean
04.What are the Non-Primitive data types?
String, Arrays, classes, interfaces, etc.
Why data types in Java?
Different data types occupy different amounts of memory. Using the correct type helps manage memory better.Data type importance?
They define how much memory to reserve.
They tell the compiler what operations are allowed.
They help prevent errors by enforcing type rules.
- What are the Primitive data types, size and example? byte, short, int, long, float, double, char & boolean.
i). What is a byte?
Byte is a primitive data type
1byte=8bits (2^8)=256
Stores values from -128 to 127.
Saves memory in large arrays (useful when working with large amounts of data, especially in embedded systems or file I/O).
ii). Why use byte?
To save memory when you know the value range is small.
Useful in networking, file handling, image processing, etc.
iii). what is int data type?
Use: Stores integers (whole numbers)
Size: 4 bytes (32 bits)
Range: –2,147,483,648 to 2,147,483,647
Example:
int age = 25;
iv). what is the short data type?
Use: Stores small integers
Size: 2 bytes (16 bits)
Range: –32,768 to 32,767
Example:
short temperature = 300;
v). what is the long data type?
Use: Stores very large integers
Size: 8 bytes (64 bits)
Range: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Example:
long population = 8000000000L;
vi). what is the float data type?
Use: Stores decimal numbers (with less precision)
Size: 4 bytes (32 bits)
Range: Approximately ±3.4e–38 to ±3.4e+38
Note: Must add f at the end of the value
Example:
float price = 19.99f;
vii). what is the boolean data type?
Use: Stores true or false
Size: 1 bit (but JVM uses 1 byte)
Example:
boolean is Logged In = true;
viii). what is the char data type?
Use: Stores a single character (in single quotes)
Size: 2 bytes (16 bits, Unicode)
Example:
char grade = 'A';
ix). what is the double data type?
Use: Stores decimal numbers (with high precision)
Size: 8 bytes (64 bits)
Range: Approximately ±1.7e–308 to ±1.7e+308
Default type for floating point numbers
Example:
double distance = 12345.6789;
Top comments (0)