0% found this document useful (0 votes)
50 views6 pages

Module2 - B PDF

The document discusses various operators in Java including unary, binary, relational, logical, increment/decrement, and bitwise operators. It provides examples of code using these operators and the output of some sample programs using operators like checking for a leap year and counting coins by denomination. It also covers casting between numeric types in Java and the differences between widening and narrowing conversions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views6 pages

Module2 - B PDF

The document discusses various operators in Java including unary, binary, relational, logical, increment/decrement, and bitwise operators. It provides examples of code using these operators and the output of some sample programs using operators like checking for a leap year and counting coins by denomination. It also covers casting between numeric types in Java and the differences between widening and narrowing conversions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 2 - The Java Programming Language Module 2 - The Java Programming Language

OPERATORS OPERATORS

a. Unary Operators (- or +) import java.io.*;


Example: - 4, +10 public class LeapYear{
b. Binary operators (+, -, *, /, %) public static void main(String args[ ]) throws IOException{
InputStreamReader a=newInputStreamReader(System.in);
Example: 5 + 4 = 9 10 - 3 = 7
BufferedReader stndin = new BufferedReader(a);
5 * 7 = 35 10 / 3 = 3 System.out.print("Input any year [yyyy]: ");
10 % 3 = 1 String syear = stndin.readLine();
c. String Concatenation(+). int year = Integer.parseInt(syear);
Ex. Name = “Java”+“Programming”; if ((year%4)==0)
d. Relational (<, <=, >, >=, ==, !=) System.out.println(year + " is a leap year");
else System.out.println(year + " is not a leap year");
e. Logical/Conditional Operators (&&, ||, !) }
f. Bitwise Operators (&, |, ^) }
g. Increment & Decrement Operators (++, --)

Module 2 - The Java Programming Language Module 2 - The Java Programming Language

OPERATORS OPERATORS

Output of Program LeapYear: import java.io.*;

Input any year [yyyy]: 1996 public class CountCoins{


1996 is a leap year public static void main(String args[ ]) throws IOException {
InputStreamReader a=new InputStreamReader(System.in);
Input any year [yyyy]: 1998 BufferedReader stndin = new BufferedReader(a);
1998 is not a leap year String smoney;
int c25, c10, c5, c1, amnt;
System.out.print("Input any amount in cents: ");
smoney = stndin.readLine();
amnt = Integer.parseInt(smoney);

1
Module 2 - The Java Programming Language Module 2 - The Java Programming Language

OPERATORS OPERATORS

c25 = amnt/25; //assigns the quotient of amnt/25 to c25 Output of CountCoins.java:


amnt= amnt%25; // assigns the remainder of amnt%25 to amnt
c10 = amnt/10; //assigns the quotient of amnt/10 to c10 Input any amount in cents: 94
amnt= amnt%10; //assigns the remainder of amnt%10 to amnt 94 can be composed of:
c5 = amnt/5; //assigns the quotient of amnt/5 to c5
3 - piece(s) of 25 cents
c1 = amnt%5; //assigns the remainder of amnt%10 to c1
System.out.println("\n"+smoney+" can be composed of:\n "); 1 - piece(s) of 10 cents
System.out.println(c25 + " - piece(s) of 25 cents"); 1 - piece(s) of 5 cents
System.out.println(c10 + " - piece(s) of 10 cents");
4 - piece(s) of 1 cent
System.out.println(c5 + " - piece(s) of 5 cents");
System.out.println(c1 + " - piece(s) of 1 cent");
}
}

Module 2 - The Java Programming Language Module 2 - The Java Programming Language

CAST OPERATOR CAST OPERATOR

Cast Operator (type) -- is used to convert numeric values


from one numeric type to another numeric type. public class Casting{
public static void main(String args[]){
A Widening conversion -- is a conversion from a smaller double x=5487.456453135;
numeric type to a larger numeric type.
int i = 5;
Example: float x; int y; System.out.println("Orig # = " + x);
x = (float) y; System.out.println("Float = " + (float)x);
System.out.println("Integer= " + (int) x);
A Narrowing conversion -- is a conversion from a larger System.out.println("\nOrig # = " + i);
numeric type to a smaller numeric type.
System.out.println("Float = " + (float) i);
Example: double x; int y; System.out.println("Double = " + (double) i);
y = (int) x; }
}

2
Module 2 - The Java Programming Language Module 2 - The Java Programming Language

CAST OPERATOR INCREMENT & DECREMENT OPERATORS

Increment Operator (++)


Output:
int i = 5, j = 6, x=0;
Orig # = 5487.456453135
++i; // increment the value of i by 1 or this is
Float = 5487.4565
// equivalent to i = i + 1;
Integer = 5487
x = j++; // assign j to x, then increment j by 1
// so, x = 6 and j = 7
Orig # = 5
Decrement Operator (--)
Float = 5.0
int i = 5, j = 6, y=0, z=0;
Integer = 5.0
y = i--; // assigns i to j, then decrement i by 1
// j = 5, i = 4
z = --j; // decrement j by 1, assign j to z. So, j=5 & z=5

Module 2 - The Java Programming Language Module 2 - The Java Programming Language

INCREMENT & DECREMENT OPERATORS INCREMENT & DECREMENT OPERATORS

Warm up: Warm up:

int a=2, b=8;


System.out.println(”old a = " + a); int a=2, b=8;
System.out.println("a++ = " + a++); System.out.println(”old a = " + a);
old a = 2 old a = 2
System.out.println(”new a= " + a); System.out.println("a-- = " + a--);
a++ = 2 a-- = 2
System.out.println(”new a= " + a);
new a= 3 new a= 1
System.out.println(”old b = " + b);
System.out.println(”++b = " + ++b); System.out.println(”old b = " + b);
old b = 8 old b = 8
System.out.println(”new b= " + b); System.out.println(”--b = " + --b);
++b = 9 --b = 7
Compile and then run the program System.out.println(”new b= " + b);
new b= 9 new b= 7
Compile and then run the program
What are the outputs of the program.
What are the outputs of the println statements.

3
Module 2 - The Java Programming Language Module 2 - The Java Programming Language

BITWISE OPERATORS BITWISE OPERATORS


Right Shift Operator (>>) Bitwise OR operator (|)
9 >> 2 --- shifts the bits of 3 two bits to the right. 4 | 5 = (00000000 00000000 00000000 00000100) |
= (00000000 00000000 00000000 00001001) >> 2 (00000000 00000000 00000000 00000101)
= (0000000000 00000000 00000000 000010) = 2 = (00000000 00000000 00000000 00000101)
Unsigned Right Shift Operator (>>>) =5
9 >>> 2
= (00000000 00000000 00000000 00001001) >> 2 Bitwise AND operator (&)
= (0000000000 00000000 00000000 000010) = 2 4 & 5 = (00000000 00000000 00000000 00000100) &
Left Shift Operator (<<) (00000000 00000000 00000000 00000101)
2 << 3 = (00000000 00000000 00000000 00000100)
= (00000000 00000000 00000000 00000010) << 3 =4
= (00000 00000000 00000000 00000010000) = 16

Module 2 - The Java Programming Language Module 2 - The Java Programming Language

BITWISE OPERATORS BOOLEAN OPERATORS

Bitwise XOR operator (^) Boolean Operator AND (&), and short-circuit AND (&&)
4 ^ 5 = (00000000 00000000 00000000 00000100) ^ false & false = false false && false = false
(00000000 00000000 00000000 00000101) false & true = false false && true = false
= (00000000 00000000 00000000 00000001) true & false = false true && false = false
=1 true & true = true true && true = true
Boolean Operator OR (|), and short-circuit OR (||)
false | false = false false || false = false
false | true = true false || true = true
true | false = true true || false = true
true | true = true true || true = true

4
Module 2 - The Java Programming Language Module 2 - The Java Programming Language

BOOLEAN OPERATORS
ASSIGNMENTASSIGNMENT
OERATORS STATEMENT

Boolean Operator XOR (^) x=y


false ^ false = false x = x + y;  x += y;
false ^ true = true x=x–y;  x -= y;
true ^ false = true x = x * y;  x *= y
true ^ true = false x = x / y;  x /= y;
Boolean Operator NOT (!) x = x % y;  x %= y
! false = true x = x & y;  x &= y
! true = false x |= y ;  x = x | y;
x ^= y;  x = x ^ y;

Module 2 - The Java Programming Language Module 2 - The Java Programming Language

Operator Precedence and Order of Evaluation Operator Precedence and Order of Evaluation

• ++ (postfix), -- (postfix) • &&


• ||
• ++ (prefix), -- (prefix), - (unary), + (unary) • ?:
• casting • =, +=, -=, *=, /=, %=, >>=, <<=, >>>=,&=, ^=, |=
• *, /, %
What will be the value of x and y when printed:
• <<, >>, >>>
int i=9, j=7, x=0, y=0;
• <, >, <=, >=
x = 6 * 7 % 3 * ++i + 60 - --j;
• = =, !=
y = (6 * 7>>2) + 2;
•&
System.out.println(“x = “ + x, “ y = “ +y);
•^
•| Output: x = 54, y = 12

5
Module 2 - The Java Programming Language
Game Ka na Ba?
1. What is 111%13?
a) 3 b) 5 c) 7 d) 9
2. What is the value of 9 + 8%7 + 6?
a) 17 b) 16 c) 13 d) 4
3. What is the value of y after execution of the following
statements?
int x = 5; int y = 4;
y = x++;
a) 4 b) 5 c) 6 d) 7

You might also like