There are some syntax errors:
long good-by ; // bad identifier: "-" not allowed short shrift = 0; // OK double bubble = 0, toil= 9, trouble = 8 // missing ";" at end. byte the bullet ; // bad identifier: can't contain a space int double; // reserved word char thisMustBeTooLong ; // OK in syntax, but a poor choice // for a variable name int 8ball; // can't start with a digit
Here is another example program, containing several variable declarations.
class Example { public static void main ( String[] args ) { long hoursWorked = 40; double payRate = 10.0, taxRate = 0.10; System.out.println("Hours Worked: " + hoursWorked ); System.out.println("pay Amount : " + (hoursWorked * payRate) ); System.out.println("tax Amount : " + (hoursWorked * payRate * taxRate) ); } }
The character *
means multiply.
In the program, (hoursWorked * payRate)
means to
multiply the number stored in hoursWorked
by the number stored in payRate
.
When it follows a character string,
+
means to
add characters to the end of the character string.
So "Hours Worked: " + hoursWorked
makes a
character string starting with "Hours Worked: " and ending with
characters for the value of hoursWorked
.
The program will print out:
Hours Worked: 40 pay Amount : 400.0 tax Amount : 40.0
The program illustrates an
Important Idea: In a calculation or in printing, use the name of the variable to represent the value it holds.
For example, in the first System.out.println
statement, the variable
hoursWorked
was used.
This means "go get the value inside hoursWorked
and use that value here".
Remember that if you want to see this program run, copy it from your Web browser, paste it into the window of Notepad, compile and run it. (See Chapter 7.)
Why did the program print the first 40
without a decimal point,
but printed the
second one with a decimal point as 40.0
?