If this sounds magical, wait to read more in the classes and objects section coming later in this chapter.
Rest assured that, in the end, it all boils down to bits (1s and 0s) in the computer. Object orientation is just
a different way of naming and thinking about what’s going on in the computer, and it’s helpful because it usually
makes thinking about the computation more natural. Also, object orientation often leads to software that is more
easily used for multiple purposes, thus constantly expanding our resource of useful, tested programs.
But Java programmers need some basic data types to get started. These Java primitive types are not objects,
but are simply the definitions of the varieties of data with which all Java programs can work. The primitive types
fall into three categories:
1 integral types integers and characters
2 floating-point types fractional numbers
3 boolean type true or false values
It may seem strange to call characters one of the integral types, but the reason is that each character is
represented by an integer value. For instance, when you type an “A”, the keyboard sends the integer value 65,
the integer code for “A”, to the computer. The code for “B” is 66, etc. Lowercase characters have different codes.
For instance the integer code for “a” is 97, and the code for “b” is 98. So letters are just integers underneath,
and software treats the bits as numbers or character codes depending on the context.
The Java primitive types, by category, are these:
1 integral types
byte 8 bits wide −128 to 127
short 16 bits wide −32768 to 32767
int 32 bits wide −2 billion to 2 billion
long 64 bits wide very small (−263) to very big (263 −1) integers
char 16 bits wide Java uses “Unicode” character codes
2 floating-point types
float 32 bits wide +/− 3.4 × 1038 with 6–7 significant decimal digits
double 64 bits wide +/− 1.8 × 10308 with 14–15 significant decimal digits
3 boolean type
boolean logical true or false
Among the integer and floating-point types, the cost of computing with greater precision is that the
higher-precision types require more space to store, and computations involve larger numbers of bits.
Here is an example Java program that uses only primitive data types:
public class Primitive {
public static void main( String[] args ) {
int x;
int y;
int z;
y = 7;
z = 4;
x = y + z;
System.out.println( "x = " + x );
}
}
Every program in Java is a class, and that is why the first line says that this is a public class (available for
use by anyone) called “Primitive.” The next line says that this is the start of a “method” called “main.” Any Java
program must have a “main” method declared exactly as this is. The line
"public static void main( String[] args ) {"
is where the program starts. This line will be in all your programs. This line tells the Java Virtual Machine
(JVM) where to start running your program.
“Public” means that anyone can run the program, “static” means that there is only one main method for the class,
“void” means that the main method will not return any values, and “String[] args” means that if the user provided any
“command line arguments,” they are available to the program in an array of String variables called “args”. Some of
this probably doesn’t make sense to you right now, so for now simply remember that every one of your programs
must have a main method, and the first line of the main method must be written exactly as this example is written.
Notice that every statement in Java ends with a semicolon! Notice, too, that the Java language is “case-sensitive.”
The variable “x” is different from the variable “X.” The class name of “Primitive” is different from a class name
of “primitive.”
The next three lines “declare” three variables of type int. The variables x, y, and z are each int variables,
which means that each one requires 32 bits for storage, and each one represents an integer value. The
JVM will reserve the required space for x, y, and z.
The next three lines assign the value of 7 to y, 4 to z, and the sum of 7 and 4 to x.
Finally, the last line displays the characters x = and the value of x, which is 11, on the “standard output
device,” usually the display. The result is:
x = 11
Notice the “curly braces” (i.e., { ... }) in the code. One pair of curly braces surrounds the “body” of the
class Primitive, and one pair of curly braces inside Primitive surrounds the body of the method main.
You must use curly braces to mark the beginning and end of a “code block” in Java. A code block is a “compound
statement,” and a code block can consist of variable declarations and statements. Classes, methods, loops
(which we have not yet discussed), and control structures (which we have not yet discussed) all define code
blocks, and blocks can be nested within one another to any level.
Use your favorite editor to type this code, and then save your work as file Primitive.java.
The next step is to compile your code using this command:
javac Primitive.java
When that is successful, you can run your program by typing:
java Primitive
Make sure all this works before continuing.
Aside from the Java primitive types, all data types in Java are classes. In other words, every class in Java
represents a new data type. A programmer in effect creates a new Java data type every time the programmer
creates a new class. The class Primitive above is a class, but it doesn’t have any facilities for use by other
programs, so it’s not a good example of a reusable new data type. Soon we will show classes that do create new
data types that can be useful to other programs, however.
Sometimes you will find it necessary to operate on objects instead of primitive types. This is because
objects are reference types, and are handled differently internally than primitive types. For the purpose of
converting variables of primitive types to reference types, Java provides a set of “wrapper classes” so that the
programmer can always create an object having the same value as a corresponding variable of a primitive type.
The Java wrapper classes corresponding to the primitive types are these:
68 PROGRAMMING IN JAVA [CHAP. 5
Primitive Wrapper Class
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean
For example, if the programmer needs an object corresponding to the integer 22148, the programmer can
use this code:
Integer ix;
ix = new Integer( 22148 );
The first line declares that ix is a variable of type Integer. Since Integer is a class, not a primitive
type, ix is an object. The JVM will reserve space for an Integer object, not just 32 bits for an int.
The second line says to create a new Integer object whose value is 22148, and assign that object to the variable ix.
Another built-in Java class that you will use very, very often is the class String. A String object
consists of none, one, several or many characters which are treated as one object. For instance:
String myName;
myName = "Carl";
The first line declares that the variable myName is of type String. If the programmer prints the variable
myName, all the characters in the word Carl will be printed:
System.out.println( "name: " + myName );
The plus sign in the println statement says to “concatenate” (combine together) the characters "name: "
and "Carl". The result will be:
name: Carl
No comments:
Post a Comment