Java Reference
In-Depth Information
2.9.3 Exponent Operations
The Math.pow(a, b) method can be used to compute The pow method is defined in the
Math class in the Java API. You invoke the method using the syntax Math.pow(a, b) (i.e.,
Math.pow(2, 3) ), which returns the result of Here a and b are parameters for the
pow method and the numbers 2 and 3 are actual values used to invoke the method. For
example,
a b .
Math.pow(a, b) method
a b (2 3 ).
System.out.println(Math.pow( 2 , 3 )); // Displays 8.0
System.out.println(Math.pow( 4 , 0.5 )); // Displays 2.0
System.out.println(Math.pow( 2.5 , 2 )); // Displays 6.25
System.out.println(Math.pow( 2.5 , -2 )); // Displays 0.16
Chapter 5 introduces more details on methods. For now, all you need to know is how to
invoke the pow method to perform the exponent operation.
2.8
Find the largest and smallest byte , short , int , long , float , and double . Which
of these data types requires the least amount of memory?
Check
Point
2.9
Show the result of the following remainders.
56 % 6
78 % -4
-34 % 5
-34 % -5
5 % 1
1 % 5
2.10
If today is Tuesday, what will be the day in 100 days?
2.11
What is the result of 25 / 4 ? How would you rewrite the expression if you wished
the result to be a floating-point number?
2.12
Are the following statements correct? If so, show the output.
System.out.println( "25 / 4 is " + 25 / 4 );
System.out.println( "25 / 4.0 is " + 25 / 4.0 );
System.out.println( "3 * 2 / 4 is " + 3 * 2 / 4 );
System.out.println( "3.0 * 2 / 4 is " + 3.0 * 2 / 4 );
2.13
2 3.5 .
Write a statement to display the result of
2.14
mr 2
Suppose m and r are integers. Write a Java expression for
to obtain a floating-
point result.
2.10 Numeric Literals
A literal is a constant value that appears directly in a program.
Key
Point
For example, 34 and 0.305 are literals in the following statements:
literal
int numberOfYears = 34 ;
double weight = 0.305 ;
2.10.1 Integer Literals
An integer literal can be assigned to an integer variable as long as it can fit into the variable. A
compile error will occur if the literal is too large for the variable to hold. The statement byte
b = 128 , for example, will cause a compile error, because 128 cannot be stored in a variable
of the byte type. (Note that the range for a byte value is from -128 to 127 .)
 
 
Search WWH ::




Custom Search