Flylib.com

Books Software

 
 
 

Running Java


Running Java

To execute the Java application just created, enter the following instruction:


JAVA BuyCar

The JAVA command invokes the JVM and passes it the name of the Java application to run. The application does not take any arguments yet, so it simply runs and prints the following line of text:


Vehicle: Ford Explorer



Java Variables

Java has eight primitive data types, shown in Table 26.1:

Table 26.1: Java Primitive Data Types

Type

Size

Range of Values

byte

8 bits

-128 to 127

short

16 bits

-32,768 to 32,767

int

32 bits

-2,147,483,648 to 2,147,483,647

long

64 bits

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float

 

1.4E-45 to 3.4E+38

double

 

4.9E-324 to 1.7E+308

char

 

one character

boolean

 

true or false

Notice that no String data type is available. The closet thing to a string is the byte data type that represents a single character. Java handles Strings with a class named String. Capitalization is important here. Notice that all the data types appear in lowercase. They must be written that way in the Java code as well. The String class is capitalized to denote that it is a class rather than a data type. All the primitive data types also have associated classes. These classes provide special functions related to the associated data type.

For example, the int data type has a corresponding Integer class. Figure 26.5 illustrates the use of the int data type, its associated Integer class, and its functions:

image from book
Figure 26.5: Integer data type, class, and functions.

The boolean data type has a corresponding Boolean class. Boolean values are "true/false" values. Figure 26.6 illustrates the use of the boolean data type, its associated Boolean class, and its functions:

image from book
Figure 26.6: Boolean data type, class, and functions.

The char data type has a corresponding Char class. Char values are single character values. Figure 26.7 illustrates the use of the char data type, its associated Char class, and its functions:

image from book
Figure 26.7: Char data type, class, and functions.

Arrays are defined by adding brackets ([]) to the end of the data type or class name in the array definition. Figure 26.8 illustrates the use of arrays:

image from book
Figure 26.8: Arrays using the [] characters .



Passing Arguments to Java

Java Applications define an array of optional String arguments. The example shown in Figure 26.9 makes use of arguments. It receives a list of values and prints them:


JAVA BuyCar Explorer, Jeep, Accura

image from book
Figure 26.9: Passing arguments to a Java application.

This line of code invokes the Buycar Java class and passes it three arguments "Explorer," "Jeep," and "Accura." The application prints each of the values as follows :

  • Explorer

  • Jeep

  • Accura

Strings are not the only kind of data Java has to work with. The various numeric data types, such as the integer type discussed earlier, are commonly used in Java and are often used in mathematical formulas. A list of valid arithmetic operators are listed in Table 26.2.

Table 26.2: Arithmetic Operators

+

Add

Subtract

*

Multiply

/

Divide

%

Modulus (remainder after division)

+=

Add to itself

-=

Subtract from itself

*=

Times itself

/=

Divide into itself

Var++

Return value of variable then add one to variable

++Var

Add one to variable then returns its new value

Var

Return value of variable then subtract one from it

Var

Subtract one from variable then returns its new value