Professional Documents
Culture Documents
Chapter 2-2
Chapter 2-2
Math
In order to access any method that is
pow(double a, double b) inside the Math class, you need to write
the class name followed by dot and then
sin(……) the method name
cos(…..)
……
Dr. Sami Albouq
Exponent Operations
Several ways of using the pow method
Examples 1:
System.out.println(Math.pow(2, 3));
// Displays 8.0
Examples 2:
System.out.println(Math.pow(2.5, -2));
// Displays .016
Examples 3:
double base = 2, exp = 2;
double myPow = Math.pow (base, exp);
System.out.println(myPow);
// Displays 4.0
Dr. Sami Albouq
Integer Literals
• An integer literal can be assigned to an integer
variable as long as it can fit into the variable
Example:
System.out.println(12343);
System.out.println(1.0 / 3.0);
displays 1.0 / 3.0 is 0.3333333333333333
16 digits
System.out.println(1.0F / 3.0F);
Inside parentheses () 3 + 4 * 4 + 5 * 7 - 1 ( )
3 + 16 + 5 * 7 – 1 *
Multiplication * Division / Modulus %
If an expression contains several operations 3 + 16 + 35 – 1 *
19 + 35 - 1 +
Addition + Subtraction -
54 - 1 +
53 -
3 + 4 x 10( y - 5)(a + b + c) 4 9+ x
- + 9( + )
5 x x y
is translated to : I assume x is a double variable
double result = (3 + 4 * x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9 + x)/y);
++variable variable++
--variable variable--
Note that the evaluation of the increment or decrement start from left to right
int x = 1; memory
System.out.print(x++); // Display 1
System.out.print(x); // Display 2
Step 2: Add 1 to x and
store it in the memory
Note that the evaluation of the increment or decrement start from left to right
int x = 1; memory
System.out.print(--x); // Display 0
Step 2: Retrieve the
value of x from the
memory
Note that the evaluation of the increment or decrement start from left to right
int x = 1; memory
System.out.print(x--); // Display 1
System.out.print(x); // Display 0
Step 2: Subtract 1 from
x and store it in the
memory
Note that the evaluation of the increment or decrement start from left to right
• Pre-Increment • Post-Increment
int y = 2 ; int y = 2 ;
System.out.println(++x * y); // Display 8 System.out.println(x++ * y); // Display 6
• Pre-Decrement • Post-Decrement
Explanation of Example 4:
Example 4: x is initially set to 3, and y is calculated using a series of operations on x.
int x = 3; 1. x-- is a post-decrement operation. It first uses the current value of x (which
int y = x-- + ++x - ++x; is 3) in the expression and then decrements x by 1. So, x-- evaluates to 3, and x
y++; becomes 2 afterward.
2. ++x is a pre-increment operation. It first increments x by 1 (from the previous
Answer: value of 2 to 3) and then uses the updated value. So, ++x evaluates to 3.
x = 2, y = 4 3. Another ++x is a pre-increment operation, which increments x by 1 (from 3 to 4)
and then uses the updated value. So, ++x evaluates to 4.
int k = ++i + i;
int k = ++i + i;
• Create a Scanner object. You can create a Scanner object by using the new
keyword, like this:
o Scanner input = new Scanner(System.in);
• Once a Scanner object has been created, a program can then use that object to
perform keyboard input using methods of the Scanner class
Method Description
Example
int x = keyboard.nextInt();
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
System.out.println("Enter value 1"); // a print statement to prompt the user to enter a byte value
byte val1 = keyboard.nextByte(); // read the input from keyboard and assigns it to val1 as byte
}
}
• Two Types are compatible if values of one type can appear wherever
values of the other type are expected, and vice versa.
• Java is a strongly typed language and from there comes it’s safety
and robustness.
• Widening
o When one type of data is assigned to another type of variable,
an automatic(implicit) type conversion will take place if,the
two types are compatible and the destination type is larger
than the source type.
o Example: int i; byte b; i=b; //valid conversion
byte 1 b 4 b
int 4 b 1 b
• Narrowing
o Converting a value of larger range to value of smaller range.
o Example: int i; byte b; b=i; //invalid
int 4 b
byte 1 b
Example
int x = 3;
byte y = x; // this is not correct and you should use casting
byte y = (byte) x;
• A different type of conversion called truncation will occur when a floating point
value is assigned to an integer type.
• If the size of the whole number is too large to fit into the target int type, then
that value will be reduced modulo the target type’s range.
Example
double x = 3.5;
int y = x; // this is not correct and you should use casting
double y = (int) x; // y will only store the int value (3)
• Implicit casting
double d = 3; (type widening)
• Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)
range increases