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:
booleanintbyteshortlongfloatdoublechar
Byte, short,andlongare alternative versions of the primary integer type,int, whereasdoubleis the preferred alternative tofloat. While not a primitive type,Stringis the more appropriate choice in many contexts thanchar. Thus, of the eight, you are likely to useboolean, int,anddoublemuch 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 initializeboolean aTest = false; // declares and initializesint 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