Special Offer take any 4 courses for INR 21999.00*

Courses
0

What are the main data types in Java? Provide examples.

December 26, 2024

Java has two main categories of data types: primitive and non-primitive.
1. Primitive Data Types
These are predefined by the Java language and are used to store simple values. There are 8 primitive data types:
boolean: Represents true or false.
Example: boolean isJavaFun = true;
byte: An 8-bit signed integer. Range: -128 to 127.
Example: byte byteValue = 10;
short: A 16-bit signed integer. Range: -32,768 to 32,767.
Example: short shortValue = 5000;
int: A 32-bit signed integer. Range: -2^31 to 2^31-1.
Example: int intValue = 100000;
long: A 64-bit signed integer. Range: -2^63 to 2^63-1.
Example: long longValue = 10000000000L;
float: A 32-bit floating-point number. Used for decimals.
Example: float floatValue = 5.75f;
double: A 64-bit floating-point number. More precise than float.
Example: double doubleValue = 19.99;
char: A 16-bit Unicode character.
Example: char letter = ‘A’; .
2. Non-Primitive Data Types
These include objects, arrays, and classes. They are more complex and user-defined.
String: Used to store a sequence of characters.
Example: String text = “Hello, Java!”;
Array: A collection of elements of the same type.
Example: int[] numbers = {1, 2, 3, 4}; .

Leave a Comment

Drop a Query

Whether to upskill or for any other query, please drop us a line and we'll be happy to get back to you.

Drop a Query NEW

Request A Call Back

Please leave us your contact details and our team will call you back.

Request A Call Back

By tapping Submit, you agree to Cambridge infotech Privacy Policy and Terms & Conditions

Enquiry Now

Enquiry popup