Dacj 1 3C
Dacj 1 3C
Knowledge Byte
In this section, you will learn about:
NIIT
Collaborate
Lesson 3C / Slide 1 of 22
Collaborate
NIIT
Collaborate
Lesson 3C / Slide 2 of 22
Collaborate
NIIT
Collaborate
Lesson 3C / Slide 3 of 22
Collaborate
You can use the following code to perform type casting of an int number, 259 and a double number, 350.55 to byte type: class TypeCast { public static void main(String arr[]) { byte b; int i = 259; double d = 350.55; b = (byte) i; System.out.println("Value of int to byte conversion " + b);
NIIT
Collaborate
Lesson 3C / Slide 4 of 22
Collaborate
NIIT
Collaborate
Lesson 3C / Slide 5 of 22
Collaborate
Overloading Constructors
A constructor is a method that is automatically invoked every time an instance of a class is created. Constructors share the same name as the class name and do not have a return type. You can use the following code to overload the Cuboid() constructor to calculate the volume of a rectangle and a square: class Cuboid { double length; double width; double height;
NIIT
Collaborate
Lesson 3C / Slide 6 of 22
Collaborate
NIIT
Collaborate
Lesson 3C / Slide 7 of 22
Collaborate
NIIT
Collaborate
Lesson 3C / Slide 8 of 22
Collaborate
Best practices on: Short-circuiting Use of switch-case and if-else construct Using the for loop construct Using brackets with the if statement Tips and Tricks on: Ternary Operator FAQs on Java Constructs and Operators
NIIT
Collaborate
Lesson 3C / Slide 9 of 22
Collaborate
Best Practices
Short-circuiting
Short-circuiting is the process in which a compiler evaluates and determines the result of an expression without evaluating the complete expression but by partial evaluation of one of the operands. Java supports conditional operators, such as AND (&&) and OR (||), which are also called short-circuit operators. For example, the expression, op1 && op2 && op3 evaluates true only if all the operands, op1, op2, and op3 are true.
NIIT
Collaborate
Lesson 3C / Slide 10 of 22
Collaborate
Best Practices
Short-circuiting (Contd.)
You can use the following code snippet to print a result if both of the conditions specified by the && operator are true: if(3<5 && 3>2) System.out.println(Result); else System.out.println(No result); You can use the following code snippet to use && operator as a short circuit operator. if(3>5 && 3>2) System.out.println(Result); else System.out.println(No result);
NIIT
Collaborate
Lesson 3C / Slide 11 of 22
Collaborate
Best Practices
Use of switch-case and if-else Construct
When you have multiple options and one choice, you use the switch-case construct. When you have multiple conditions out of which only one condition can be true, you use the nested if-else construct.
NIIT
Collaborate
Lesson 3C / Slide 12 of 22
Collaborate
Best Practices
Using the for Loop Construct
When the control variable of the looping statement is of int type and the number of times a loop should execute is known in advance, you use the, for, loop. In the, for, loop, all the three parts, such as initialization, condition, and increment or decrement are defined initially at one place.
NIIT
Collaborate
Lesson 3C / Slide 13 of 22
Collaborate
Best Practices
Using Brackets with the if Statement
You should use opening and closing brackets with the if statement. When the if block consists of more than one statement, place the statements in brackets. If the if block consists of only one statement, it is not necessary to place single statement in brackets. However, it is advisable to place even a single statement in brackets because brackets enhance the code clarity and minimize the chances of errors.
NIIT
Collaborate
Lesson 3C / Slide 14 of 22
Collaborate
The conditional operator (?:) operates on three operands and is therefore called the ternary operator. The syntax of the ternary operator is: (boolean_expr) ? val1 : val2 Ternary operator is used to replace the if-else statements. You can use the following code snippet to display the grade scored by a student using the ternary operator: Score = 95; (Score>=90) ? System.out.println(Grade is A.); : System.out.println(Grade is B.);
NIIT
Collaborate
Lesson 3C / Slide 15 of 22
Collaborate
FAQs
Can you use a switch statement inside another switch statement? Yes, you can use a switch statement within another switch statement. This is called nested switch. Each switch statement creates its own block of case statements. Therefore, no conflict occurs between the case labels of the inner and outer switch statements .
How does the program stop running if a loop never ends? A program stops running when it enters an infinite loop by pressing the keys Ctrl and C simultaneously .
NIIT
Collaborate
Lesson 3C / Slide 16 of 22
Collaborate
FAQs (Contd.)
Can you add the numeric value of one string to the value of another if the + operator is used with strings to link up two different strings? Yes, you can use the value of a String variable as an integer only by using a method that converts the value of the string variable into a numeric form. This is known as type casting because it casts one data type, such as a string into another data type, such as int.
Can the == operator be used to determine whether two strings have the same value as in name == "John"?
Yes, the == operator can be used to determine whether two strings have the same value. In Java, you can also compare two strings using the equals() method.
NIIT
Collaborate
Lesson 3C / Slide 17 of 22
Collaborate
Challenge
1. Match the following: a. ++ i. Logical AND b. && ii. Logical OR c. % iii. Logical NOT d. & iv. Unsigned Shift e. || v. Bit-wise XOR f. >>> vi. Arithmetic Assignment g. ! vii. Unary Increment h. ^ viii. Bit-wise AND i. += ix. Ternary j. ? x. Modulus 2. The if decision construct is used when there are multiple values for the same variable. (True/False).
NIIT
Collaborate
Lesson 3C / Slide 18 of 22
Collaborate
Challenge (Contd.)
3. Predict the output of the following code snippet int n=5, d=4; int remainder = n % d; if (div != 0) System.out.println(n + is not completely divisible by + d); else System.out.println(n + is completely divisible by + d); a) 5 is not completely divisible by 4 b) 5 is completely divisible by 4
NIIT
Collaborate
Lesson 3C / Slide 19 of 22
Collaborate
Solutions to Challenge
1. 2. 3. a-vii, b-i, c-x, d-viii, e-ii, f-iv, g-iii, h-v, i-vi, j-ix False a 5 is not completely divisible by 4
NIIT
Collaborate
Lesson 3C / Slide 20 of 22