4.5 Primitives

 < Day Day Up > 



4.5 Primitives

Primitive data types are built directly into the Java virtual machine (JVM). For example, an int is a primitive, but an Integer is an object that wrappers an int primitive. You can always tell the primitives in the Java language because they start with a lower-case character; for example, float is a primitive, but Float is an object.

Because the Java language has no union constructs or pointers, [2] the data type of the variable referenced by the identifiers for primitives cannot be changed in Java. When a variable of a different data type is used in an assignment statement for a primitive, it always involves copying the variable and converting the copy to the correct type. For example, when setting an int variable to a float variable, the float variable is copied to another variable that has a data type of int, so the reference cannot change the data type it represents. And, because both the Java compiler and the JVM share the same representations and operations for primitive data types, they can agree that the compiler can generate the code to manipulate these variables, and their data types can be safely forgotten after the compilation step. This saves space, makes operations on primitives faster than operations on objects, and is similar to the mechanisms employed by the C/C++ compiler. However, these primitives are safe in Java because no mechanism is included to change the data type of any identifier or the referenced variable once it is created. In Java, then, the programmer can always rely on the fact that the data type of the identifier is the same data type of the variable it references. Thus, for Java primitives, while it is strictly incorrect to refer to the identifier and the variable as being the same, not making the distinction has no serious consequence; however, the mechanism for ensuring safe access of objects is very different than that for primitives in Java.

[2]This statement is often misinterpreted to mean that Java has no pointers. Pointers are always used when running a program. The point here is that the Java language does not allow a programmer to access a pointer directly. This ensures that a programmer cannot make a reference to an invalid type when accessing a variable.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net