Special Offer take any 4 courses for INR 21999.00*

Courses
0

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

December 18, 2024

Java features two categories of data types: Primitive and Non-Primitive. Here’s an overview with examples:

1. Primitive Data Types
byte: Stores whole numbers from -128 to 127.
Example: byte age = 25;
short: Stores whole numbers from -32,768 to 32,767.
Example: short population = 15000;
int: Stores whole numbers from -2^31 to 2^31-1.
Example: int distance = 123456;
long: Stores whole numbers from -2^63 to 2^63-1.
Example: long universeAge = 12345678901L;
float: Stores fractional numbers with up to 7 decimal digits.
Example: float price = 19.99f;
double: Stores fractional numbers with up to 16 decimal digits.
Example: double pi = 3.14159265359;
char: Stores a single character, using single quotes.
Example: char grade = ‘A’;
boolean: Stores true or false.
Example: boolean isJavaFun = true;
2. Non-Primitive Data Types
Include String, Arrays, Classes, and Interfaces.
Examples:
String: String name = “John”;
Array: int[] numbers = {1, 2, 3};
Key Differences:
Primitive types are pre-defined by Java and store simple values.
Non-Primitive types are created by the programmer, and they can store references to objects or complex data structures.

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