[post-views]
In this lesson, you will learn.
In Java, data types specify the type of data that a variable can hold. Java has two categories of data types: primitive data types and reference data types.
byte
, short
, int
, long
, float
, double
, char
, and boolean
.
1. Integral Data Types:
byte byteVar = 127;
short shortVar = 32767;
int intVar = 2147483647;
long longVar = 9223372036854775807L; // Note the 'L' suffix for long literals
2. Floating-Point Data Types:
float floatVar = 3.14f; // Note the 'f' suffix for float literals
double doubleVar = 3.14159;
3. Character Data Type:
char charVar = 'A';
4. Boolean Data Type:
boolean boolVar = true;
Here’s a summary table of all primitive data types in Java:
Data Type | Size (in bits) | Default Value | Example |
---|---|---|---|
byte |
8 | 0 | byte byteVar = 127; |
short |
16 | 0 | short shortVar = 32767; |
int |
32 | 0 | int intVar = 2147483647; |
long |
64 | 0L | long longVar = 9223372036854775807L; |
float |
32 | 0.0f | float floatVar = 3.14f; |
double |
64 | 0.0 | double doubleVar = 3.14159; |
char |
16 | ‘u0000’ | char charVar = 'A'; |
boolean |
– | false |
boolean boolVar = true; |
// Non-primitive data type (Object)
String stringValue = "Hello, Java!"; // String is a class in Java
// Non-primitive data type (Array)
int[] intArray = {1, 2, 3, 4, 5}; // Array is a reference type
// Non-primitive data type (Interface)
interface Printable {
void print();
}
class MyClass implements Printable {
public void print() {
System.out.println("Printing...");
}
}
Printable printableObj = new MyClass(); // Interface type
In summary, primitive data types are the basic building blocks directly representing simple values, while non-primitive data types are more complex and represent references to objects, arrays, or interfaces.
Understanding the distinction between these two types is important for effective Java programming and memory management.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.