Java has eight primitive data types, which are the foundational building blocks for storing simple values directly in memory. These types are predefined by the language and do not require object instantiation.
The Eight Primitive Data Types in Java
boolean: Stores true or false values. Used for logical conditions.char: Stores a single 16-bit Unicode character (e.g.,'A','z','1'). Range:0to65,535.byte: An 8-bit signed integer. Range: -128 to 127.short: A 16-bit signed integer. Range: -32,768 to 32,767.int: A 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647. Most commonly used for integers.long: A 64-bit signed integer. Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use suffixL(e.g.,100L).float: A 32-bit single-precision floating-point number. Use suffixf(e.g.,3.14f). Suitable for less precise decimal calculations.double: A 64-bit double-precision floating-point number. Default for decimal values (e.g.,3.14159). More accurate thanfloat.
Key Characteristics
Fixed size and range: Each type has a defined memory size and value limits.
Direct storage: Values are stored directly in the stack, making them fast and memory-efficient.
No null values: Primitive types cannot be
null, reducingNullPointerExceptionrisks.No methods: Unlike objects, primitives do not have methods; they only hold values.
Used for basic operations: Ideal for numbers, characters, and boolean logic in programs.
💡 Note: For collections (like
ArrayList) or whennullis needed, wrapper classes (e.g.,Integer,Double,Boolean) are used to wrap primitives.
ELI5: What is a primitive data type in Java?
What are Java's primitive types? - Stack Overflow
[Java] Newbie - Object and primitive data type
How important are primitive data types? More specifically in java.
Videos
I think it is the same thing as an immutable type, not sure though.
In Java, every variable has a type declared in the source code. There are two kinds of types: reference types and primitive types. Reference types are references to objects. Primitive types directly contain values. There are 8 primitive types:
- byte
- short
- int
- long
- char
- float
- double
- boolean
What do people mean by "Types"?
In the real world there are different "types" of things. e.g. There are many different "types" of vehicles: "sports cars" for driving fast, "utes" for carrying tools, "trucks" for transporting lots of goods, and limousines for traveling in luxury.
In the same way, in Java, you can have different "types" of data which serve different purposes: e.g. numbers (are used to add/subtract etc), "strings" are used to communicate words and letters. You cannot use letters to add - that just does not make sense, nor could you use numbers to write a sentence.
Primitives vs reference types - what does it mean? What's the difference?
Now there are some "types" of data which are basic. These are already created by the boffins at Redmond/Sun. These are called "primitive" java types, and they store the values within themselves. What does that mean? It's best explained by example:
Example of a primitive type
If I gave you a $50 note, then the note in and of itself is worth $50. The value is stored in the note itself.
Primitives Juxtaposed with Reference Types
Imagine that instead of giving you $50 I gave you an piece of paper which has on it an address to a safe deposit box. The piece of paper i gave you is not worth $50 in and of itself, but it points to an address where you can get your $50. This piece of paper is basically a "reference" type, because it doesn't store any values within and in and of itself, it merely points to certain addresses.
But I can give you an address to anything: planes, castles, rainforrests: anything!
Summary
You can't just hand someone a plane or a Shinkansen train from your back pocket: you just hand them an address to it. Juxtapose this with having $50, or any type of currency: the actual substance is in your back pocket.
That in an nutshell is the difference between primitive and reference types.
(Corny analogy used to help you understand and remember.)
I've been reading the differences between the two but I'm having trouble visualizing them.
From what I've read:
Primitive - a value that cannot be broken anymore;
for example:
int variable = 8;
Object data - a value that is referenced/can be broken down
for example:
int var1 = 2;
var2 = var1;
Am I correct?