Friday 1 September 2017

Java Data Type

Cited from the Book Java How to Program

Java’s types are divided into primitive types and reference types. The primitive types are boolean, byte, char, short, int, long, float and double. All nonprimitive types are reference types, so classes, which specify the types of objects, are reference types.

A primitive-type variable can store exactly one value of its declared type at a time. For example, an int variable can store one whole number (such as 7) at a time. When another value is assigned to that variable, its initial value is replaced. Primitive-type instance variables are initialized by default -- variables of types byte, char, short, int, long, float and double are initialized to 0, and variables of type boolean are initialized to false. An attempt to use an uninitialized local variable causes a compilation error.

Programs use variables of reference types (normally called references) to store the locations of objects in the computer’s memory. Such a variable is said to refer to an object in the program. Reference-type instance variables are initialized by default to the value null—a reserved word that represents a “reference to nothing.”

When you use an object of another class, a reference to the object is required to invoke (i.e., call) its methods. Primitive-type variables do not refer to objects, so such variables cannot be used to invoke methods.


No comments:

Post a Comment