GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing jenkov.com — Java Data Types Java Language
Java Data Types
As explained in the text about Java variables, each variable in Java has a data type. Data types into two groups:
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. Primitive Data TypesThe Java language contains the following primitive data types:
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; Object TypesThe 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 other of the core object types in Java.
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, like
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 You can also create the object already when the variable is declared, like this: Integer myInteger = new Integer(45); Object Versions of Primitive Data Types Are Immutable
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
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 Auto BoxingBefore 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 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 Integer myInteger = null; int myInt = myInteger;
This code will compile alright, but when executed it will result in a Data Types in Other LanguagesFor the curious reader, here are links to tutorials about data types in all the programming languages covered here at Jenkov.com:
Close TOC
All Trails Trail TOC Page TOC Previous Next | ||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||||||||||||||||||||||||||||||||||||||||