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
Videos
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.)
It kind of depends on the language.
For example, in languages like C and C++, you have a number of built-in scalar types - int, float, double, char, etc. These are "primitive" in the sense that they cannot be decomposed into simpler components. From these basic types you can define new types - pointer types, array types, struct types, union types, etc.
Then you have a language like old-school Lisp, where everything is either an atom or a list. Again, by the above definition, an atom is "primitive" in the sense that it cannot be decomposed into something simpler.
Edit
As far as I'm concerned, the terms "primitive", "basic", and "built-in" are pretty much interchangeable. If you want to get really pedantic, though, you can distinguish between types that are "built-in" (those explicitly provided by the language definition) and types derived from the built-in types that are still "primitive" or "basic" in that they cannot be decomposed into simpler elements. C's typedef facility allows you to create new type names for existing types. Ada allows you to create new scalar types that have constraints on them. For example, you can derive a Latitude type from the built-in floating type, with the constraint that it can't take on values outside the range [-90.0, 90.0]. It's still a primitive or basic type in that it cannot be broken down into any simpler components, but since it's user-defined, it's not considered a "built-in" type.
Again, these concepts are a little fuzzy, and it really depends on context. For example, the notion of a "built-in" type is meaningless for a typeless language like BLISS.
From the Java perspective:
In Java, there is a very clear distinction between primitive and non-primitive types.
A variable of a primitive type directly contains the value of that type (in other words, they are value types).
A variable of a non-primitive type doesn't contain the value directly; instead, it is a reference (similar to a pointer) to an object. (It is not possible in Java to create user-defined value types).
Java has eight primitive types: byte, short, int, long, char, boolean, float and double. Anything else is a non-primitive type.