Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Chapter 2

Dr. Sami Albouq


You Will Learn
• perform exponent operations using Math.pow(a, b)
• write integer literals, floating-point literals, and literals in
scientific notation
• write and evaluate numeric expressions
• Increment and decrement
• Input from console
• Data type compatibly

Dr. Sami Albouq


Exponent Operations
Class
action Value 1 Value 2
name

Math.pow (double base, double exponent)


Returns the value of the first argument raised to the power
of the second argument.

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

• int value = 2147483647;

• A compilation error would occur if the literal were


too large for the variable to hold

• byte b = 1000 would cause a compilation error,


because 1000 cannot be stored in a variable of the
byte type.

• Note byte in java ranges between 128 to -127


Dr. Sami Albouq
Integer Literals
• To denote an integer literal of the long type,
append it with the letter L or l. L is preferred
because l (lowercase L) can easily be confused with
1 (the digit one).

• long value = 21474836475;


• long value = 21474836475L;

Example:
System.out.println(12343);

By default, an integer literal


is treated as int. The computer
will allocate 4 bytes for it.

Dr. Sami Albouq


double vs. float
• The double type values are more accurate than the float
type values. For example,

System.out.println(1.0 / 3.0);
displays 1.0 / 3.0 is 0.3333333333333333

16 digits

System.out.println(1.0F / 3.0F);

displays 1.0F / 3.0F is 0.33333334


7 digits

Dr. Sami Albouq


How to Evaluate an Expression
• Unary operators of equal precedence are grouped right-
to-left

+-+rate is evaluated as +(-(+rate))

• Binary operators of equal precedence are grouped left-


to-right

base + rate + hours is evaluated as

(base + rate) + hours

Dr. Sami Albouq


How to Evaluate an Expression
3 + 4 * 4 + 5 * (4 + 3) – 1

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 -

Dr. Sami Albouq


Arithmetic Expressions (Deferent
operators)

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);

Where should I start from?


Dr. Sami Albouq
Increment and Decrement Operators

In Java, the increment operator is denoted by "++" and is a


unary operator that adds 1 to the value of a variable. The
increment operator can be used with integer, floating-point,
and even character variables.

• There are two types of increment operators in Java:

• Pre-increment: In pre-increment, • Post-increment: In post-increment,


the value of the variable is the value of the variable is
incremented by 1 before the incremented by 1 after the
expression is evaluated. The expression is evaluated. The
syntax for pre-increment is: syntax for post-increment is:

++variable variable++

Dr. Sami Albouq


Increment and Decrement Operators

In Java, the decrement operator is denoted by "--" and is a


unary operator that subtracts 1 from the value of a
variable. The decrement operator can be used with integer,
floating-point, and even character variables.

• There are two types of decrement operators in Java:


• Pre-decrement: In pre-decrement, • Post-decrement: In post-decrement,
the value of the variable is the value of the variable is
decremented by 1 before the decremented by 1 after the
expression is evaluated. The expression is evaluated. The
syntax for pre-decrement is: syntax for post-decrement is:

--variable variable--

Dr. Sami Albouq


Increment and Decrement Operators

Step 1: Add 1 to x and


store it in the memory
int x = 1;
System.out.print(++x); // Display 2
Step 2: Retrieve the
value of x from the
memory

Behind the seen the computer does the following:


Step 1: Declare a variable, name it x, and assign 1 to it in the memory.
Step 2: Evaluate ++ by adding 1 to x to become 2 and store it in the memory
Step 3: Retrieve the value of x from the memory and put it in the print statement
Step 4: Execute the print statement to display the value 2 on the screen.

Note that the evaluation of the increment or decrement start from left to right

Dr. Sami Albouq


Increment and Decrement Operators

Step 1: Retrieve the


value of x from the

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

Behind the seen the computer does the following:


Step 1: Declare a variable and name it x and assign 1 to it in the memory.
Step 2: Retrieve the value of x from the memory and put it in the print statement.
Step 3: Evaluate ++ by adding 1 to x and change the value of x in the memory
Step 4: Execute the print statement to display the retrieved value of x on the screen, which is 1.
Step 5: Retrieve the value of x from the memory put in the print statement to display the
value 2 on the screen

Note that the evaluation of the increment or decrement start from left to right

Dr. Sami Albouq


Increment and Decrement Operators

Step 1: Subtract 1 from


x and store it in the

int x = 1; memory

System.out.print(--x); // Display 0
Step 2: Retrieve the
value of x from the
memory

Behind the seen the computer does the following:


Step 1: Declare a variable, name it x, and assign 1 to it memory.
Step 2: Evaluate -- by subtracting 1 from x to become 0 and store it in the memory
Step 3: Retrieve the value of x from the memory and put it in the print statement
Step 4: Execute the print statement to display the value of x which is 0 on the screen.

Note that the evaluation of the increment or decrement start from left to right

Dr. Sami Albouq


Increment and Decrement Operators

Step 1: Retrieve the


value of x from the

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

Behind the seen the computer does the following:


Step 1: Declare a variable and name it x and assign 1 to it.
Step 2: Retrieve the value of x from the memory and put it in the print statement.
Step 3: Evaluate -- by subtracting 1 from x and change the value of x in the memory
Step 4: Execute the print statement to display the retrieved value of x on the screen, which is 1.
Step 5: Retrieve the value of x from the memory put in the print statement to display the
value 0 on the screen

Note that the evaluation of the increment or decrement start from left to right

Dr. Sami Albouq


Pre and Post Increment Examples

• Pre-Increment • Post-Increment

Step 1: Replace the

Examples 1: Step 1: Add 1 to x and Examples 2: value of x++ with the


current value of x
replace ++x with the new
int x = 3 ; value of x which is 4 int x = 3 ; which is 3

int y = 2 ; int y = 2 ;
System.out.println(++x * y); // Display 8 System.out.println(x++ * y); // Display 6

Step 3: Evaluate 4 * 2 Step 3: Evaluate 3 * 2 Step 2: Replace y with


Step 2: Replace y with
and print the result on and print the result its value, which is 2
its value, which is 2
the screen. on the screen. Next,
add 1 to the variable
x to become 4

Dr. Sami Albouq


Pre and Post Increment Examples

• Pre-Decrement • Post-Decrement

Step 1: Replace the


Step 1: Subtract 1 Examples 2: value of x-- with the
current value of x
from x and replace --x
Examples 1: with the new value of int x = 3 ; which is 3
int x = 3 ; x which is 2
int y = 2 ;
int y = 2 ; System.out.println(x-- * y); // Display 6
System.out.println(--x * y); // Display 4
Step 3: Evaluate 3 * 2 Step 2: Replace y with
Step 3: Evaluate 2 * 2 Step 2: Replace y with and print the result its value, which is 2
and print the result on its value, which is 2 on the screen. Next,
the screen. subtract 1 from the
variable x to become 4

Dr. Sami Albouq


Increment and Decrement Operators

int i = 10; Same effect as


int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;

int i = 10; Same effect as


int newNum = 10 * (++i); i = i + 1;
int newNum = 10 * i;

Dr. Sami Albouq


Home Practice Exercises
What is the value of x and y after executing the following code:
Example 1: Example 2: Example 3:
int x = 5; int x = 10; int x = 4;
int y = ++x; int y = x--; int y = x++ + ++x;
y--; ++y; x--;
Answer: Answer: Answer:
x = 6, y = 5 x = 9, y = 11 x = 9, y = 11

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.

Now, let's calculate y:


y = 3 + 3 - 4;
y = 2;
After these calculations, y holds the value 2.
Finally, y++ is used to increment the value of y by 1, making y equal to 3.
So, the final values are:
- x is 4.
- y is 3.
Dr. Sami Albouq
Increment and Decrement Operators

Using increment and decrement operators makes expressions short,


but it also makes them complex and difficult to read. Avoid
using these operators in expressions that modify multiple
variables, or the same variable for multiple times such as this:

int k = ++i + i;

Dr. Sami Albouq


Increment and Decrement Operators

Using ncrement and decrement operators makes expressions short,


but it also makes them complex and difficult to read. Avoid
using these operators in expressions that modify multiple
variables, or the same variable for multiple times such as this:

int k = ++i + i;

Dr. Sami Albouq


Console Input Using the Scanner Class

• To receive input from the keyboard in you application, you


need to use specific class.

• Java includes a class for doing simple keyboard input named


the Scanner class

• In order to use the Scanner class, a program must include the


following line near the start of the file:
o import java.util.Scanner;

• This statement tells Java to


o Make the Scanner class available to the program
o Find the Scanner class in a library of classes (i.e., Java
package) named java.util

Dr. Sami Albouq


How to Use Scanner Class?
• Import the Scanner class at the beginning of your code. You can do this by
adding the following line at the beginning of your Java file:
o import java.util.Scanner;

• 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

nextByte () reads an integer of the byte type.

nextShort () reads an integer of the short type.


nextInt () reads an integer of the int type.

nextLong () reads an integer of the long type.

nextFloat () reads a number of the float type.


nextDouble() reads a number of the double type.

Dr. Sami Albouq


How to Use One of The Methods

Scanner keyboard = new Scanner(System.in);

Variable data type must


match the method data
type, otherwise an
error may occur

Data_Type value = keyboard.ANY_Method();


Note keyboard can be used over and over
nextByte() | nextShort()
to input data from the keyboard.
nextFloat()| nextInt()
Therefore, you do not need to recreate
nextLong() | nextDouble()
another Scanner variable.

Example
int x = keyboard.nextInt();

Dr. Sami Albouq


How It Looks in Your Application

import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in); // scanner object declaration

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

// Similar to the above, but the input is an integer value


System.out.println("Enter value 2");
int val2 = keyboard.nextInt();

System.out.println("Val 1 = " + val1 + " val 2 = " + val2);

}
}

Dr. Sami Albouq


Type Compatibility

• 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.

• Every variable has a type which is strictly defined.

• All assignments ,whether explicit or via parameter passing in


method calls ,are checked for type compatibility and any
mismatches result in errors .

Dr. Sami Albouq


Type Conversion in Primitive Types

• 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

Dr. Sami Albouq


Type Casting in Primitive Types
• A cast is an explicit type conversion of incompatible types.
• General form : (target-type)value
Target-type is the desired type to convert the value to.

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)

Dr. Sami Albouq


Type Conversion Example

• 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)

• What is wrong? int x = 5 / 2.0;

range increases

byte, short, int, long, float, double

Dr. Sami Albouq

You might also like