int x 10 int y x 2 Systemoutprint x x y y x11 y22 output x11 y20 output 27 Quiz

# Int x 10 int y x 2 systemoutprint x x y y x11 y22

This preview shows page 14 - 19 out of 22 pages.

int x = 10; int y = x++ * 2; System.out.print( " x= " +x + " y= " + y); x=11 y=22 output x=11 y=20 output 27 Quiz on pre/post increment/decrement What is the output of the program below? public class Ops { public static void main(String[] args) { int a = 10,p,q,r,s; p = a++; // assign a to p then add 1 q = ++a; // add one to a then assign to q r = --a; s = a--; System.out.println("p = " + p); System.out.println("q = " + q); System.out.println("r = " + r); System.out.println("s = " + s); System.out.println("a = " + a); } } Ans: _______________________________________ _______________________________________ _______________________________________ _______________________________________ _______________________________________ 28 Operator precedence & Associative Rule Category Operators Associativity Operations on references . [] L to R Unary ++ -- ! - (type) R to L Multiplicative * / % L to R Additive + - L to R Shift (bitwise) << >> >>> L to R Relational < <= > >= instanceof L to R Equality == != L to R Boolean (or bitwise) AND & L to R Boolean (or bitwise) XOR ^ L to R Boolean (or bitwise) OR | L to R Logical AND && L to R Logical OR || L to R Conditional ?: R to L Assignment = *= /= %= += -= R to L Order in which operations are evaluated are based on precedence chart (below) and associative rule. Associative Rule: Operators with equal precedence are carried out from left to right, except for = operator (which is right to left) 29 Order of Evaluation: Expression 3 + 8 * 4 > 5 * ( 4 + 3 ) - 1 3 + 8 * 4 > 5 * (4 + 3) - 1 3 + 8 * 4 > 5 * 7 – 1 3 + 32 > 5 * 7 – 1 3 + 32 > 35 – 1 35 > 35 – 1 35 > 34 true (1) Inside parentheses first (2) Leftmost * next (* / % group) (3) Remaining * next (* / % group) (4) Leftmost + next (+- group) (5) Remaining – next (+- group) (6) > next 30 IF IN DOUBT YOU CAN ALWAYS USE EXTRA PARENTHESES! 3 Predefined Objects for Input/Output System.out PrintStream System.err PrintStream Input a line Error in Input System.out.println("Input a line"); System.err.println("Error in Input"); System.in InputStream String message = System.in.nextLine(); ? No Such method ! 31 Output using print() import java.util.Scanner; public class SumDemo2 { public static void main (String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter value for n1 : "); double n1 = keyboard.nextDouble(); System.out.print("Enter value for n2 : "); double n2 = keyboard.nextDouble(); double sum = n1 + n2; System.out.print("Sum of "+n1+ " and " + n2 + " is " + sum); } } Unlike print(), printf() allows setting width & decimal places directly. 32 Formatted output using printf() import java.util.Scanner; public class SumDemo3 { public static void main (String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter value for n1 : "); double n1 = keyboard.nextDouble(); System.out.print("Enter value for n2 : "); double n2 = keyboard.nextDouble(); double sum = n1 + n2; System.out.printf("Sum of %.2f and %.2f is %.2f", n1, n2, sum); } } 2 2 2 33 Commonly used printf()/String.format conversion symbols General Syntax %[argument_index\$][flags][width][.precision]conversion Sample conversions •%c a character •%d a decimal integer •%f a floating point number •%s a string •%b a boolean value See * See also java.lang.String.format() method 34 Using width and precision modifiers in printf() In the previous program changing last statement to System.out.printf("Sum of %10.2f and %8.2f is %6.2f", n1, n2, sum); produces the following results: 10 8 6 35 Tabulating output using printf()  #### You've reached the end of your free preview.

Want to read all 22 pages?

• One '14
• • • 