Unlike some programming languages in which everything is an object, Java includes eight so-called primitive data types that are not strictly speaking classes (although the java.lang packages includes shadow classes for each). The eight primitive data types are:
boolean
int
byte
short
long
float
double
char
Byte, short,
andlong
are alternative versions of the primary integer type,int
, whereasdouble
is the preferred alternative tofloat
. While not a primitive type,String
is the more appropriate choice in many contexts thanchar
. Thus, of the eight, you are likely to useboolean, int,
anddouble
much more frequently than the others.Variables that use primitive types must be specifically declared. This is usually done at the beginning of the program or some logical segment of the program, although they may also be declared in context. The following statements illustrate both options for several different types:
boolean aTest; // declares but does not initialize
boolean aTest = false; // declares and initializes
int nItems;
int nItems = 16;
double howMuch;
double howMuch = 1.23e2;
Applet context
The data types discussed above are illustrated within the context of an applet, below:import java.applet.Applet; public class primitives extends Applet{ boolean aTest; int nItems; public void init ( ){ aTest = false; nItems = 16; double howMuch = 1.23e2; System.out.println("aTest = "+aTest); System.out.println("nItems = "+nItems); System.out.println("howMuch = "+howMuch); } // end init }// end primitives