DataTypes in java
As explained in the
text about java variable , each variable in Java has a data type. Data
types into two groups:
Primitive data types
Object references
A variable takes up a
certain amount of space in memory. How much memory a variable takes depends on
its data type.
A variable of a
primitive data type contains the value of the variable directly in the memory
allocated to the variable. For instance, a number or a character.
A variable of an object
reference type is different from a variable of a primitive type. A variable of
an object type is also called a reference.
The variable itself does not contain the object, but contains a reference to the object. The
reference points to somewhere else in memory where the whole object is stored.
Via the reference stored in the variable you can access fields and methods of
the referenced object. It is possible to have many different variables
reference the same object. This is not possible with primitive data types.
The Java language
contains the following primitive data types:
Data type
|
Description
|
boolean
|
A binary value of
either true or false
|
byte
|
8 bit signed value,
values from -128 to 127
|
short
|
16 bit signed value,
values from -32.768 to 32.767
|
char
|
16 bit Unicode
character
|
int
|
32 bit signed value,
values from -2.147.483.648 to 2.147.483.647
|
long
|
64 bit signed value,
values from -9.223.372.036.854.775.808 to 9.223.372.036.854.775.808
|
float
|
32 bit floating point
value
|
double
|
64 bit floating point
value
|
That these are
primitive data types means that they are not objects, nor references to objects
(classes and objects are explained in later texts in this Java tutorial).
Here is an example of
how you declare a variable of a primitive type:
int myInt;
The primitive types
also come in versions that are full-blown objects. That means that you
reference them via an object reference that you can have multiple references to
the same value, and you can call methods on them like on any other object in Java.
The list of core object data types below contains the object versions of the
primitive types. The list also contains a few others of the core object types
in Java.
Data type
|
Description
|
Boolean
|
A binary value of
either true or false
|
Byte
|
8 bit signed value,
values from -128 to 127
|
Short
|
16 bit signed value,
values from -32.768 to 32.767
|
Character
|
16 bit Unicode
character
|
Integer
|
32 bit signed value,
values from -2.147.483.648 to 2.147.483.647
|
Long
|
64 bit signed value,
values from -9.223.372.036.854.775.808 to 9.223.372.036.854.775.808
|
Float
|
32 bit floating point
value
|
Double
|
64 bit floating point
value
|
String
|
N byte Unicode string
of textual data. Immutable
|
Notice how object types
are spelled with a capital letter in the beginning of their name, where the
primitive version (non-object) is spelled in all lowercase characters. There
are also abbreviation differences, likeint vs. Integer and char vs. Character.
There are of course
many other components you can use in the Java API, but the above mentioned data
types are the core Java types. The String type is explained in more detail in
its own text: java Strings
You can also create your own more complex data
types by creating custom classes. I will get back to how in a later text.
Here is how you declare
a variable of (reference to) one of the core object types:
Integer myInteger;
When you declare an
object reference variable, the reference does not point to any object. You need
to create (instantiate) an object first. Here is how that is done:
Integer myInteger;
myInteger = new
Integer(45);
This example makes the myInteger variable
reference an Integer object which internally contains the value 45.
It is the new Integer(45) part of the code that creates the Integer object.
You can also create the
object already when the variable is declared, like this:
Integer myInteger = new Integer(45);
The object versions of
the primitive data types are immutable, meaning the values stored inside them
cannot be changed once set. For instance, the value stored inside an Integer object
cannot be changed once the object has been created.
The variable that
references the object can be made to point to another object though. Here is an
example:
Integer myInteger = new
Integer(45);
myInteger = new
Integer(33);
As you can see, the
variable in the example is made to point to another Integer object.
Before Java 5 you had
to call methods on the object versions of the primitive types, to get their
value out as a primitive type. For instance:
Integer myInteger = new
Integer(45);
int myInt = myInteger.intValue();
From Java 5 you have a
concept called "auto boxing". That means that Java can automatically
"box" a primitive variable in an object version, if that is required,
or "unbox" an object version of the primitive data type if required.
For instance, the example before could be written like this:
Integer myInteger = new
Integer(45);
int myInt = myInteger;
In this case Java would
automatically extract the int value from the myInteger object
and assign that value to myInt.
Similarly, creating an
object version of a primitive data type variable was a manual action before
Java:
int myInt = 45;
Integer myInteger = new
Integer(myInt);
With auto boxing Java
can do this for you. Now you can write:
int myInt = 45;
Integer myInteger =
myInt;
Java will then
automatically "box" the primitive data type inside an object version
of the corresponding type.
Java's auto boxing
features enables you to use primitive data types where the object version of
that data type was normally required, and vice versa. There is one pitfall to
keep in mind though. A variable of type object (a reference to an object) can
point to null, meaning it points to nothing - no object. If you try to
convert null to a primitive value you will get a NullPointerException (an
error that causes the program to fail). This code shows an example of that:
Integer myInteger =
null;
int myInt = myInteger;
This code will compile
alright, but when executed it will result in NullPointerException because
the variable myInteger points to null. It is thus not possible
to convert (unbox) the value of the object it points to, because it does not
point to any object.
No comments:
Post a Comment