Java Operator and Class Examples
Java Operator and Class Examples
Operator precedence can lead to unintended results, especially in print statements. Without proper use of parentheses, arithmetic operations like addition can be overridden by concatenation, resulting in '105' instead of expecting arithmetic addition. Using parentheses as in '(a+b)' ensures correct evaluation prioritizing arithmetic over string concatenation .
Unexpected results may arise when arithmetic operations are intertwined with strings due to Java's left-to-right evaluation without parentheses. For instance, 'a+b' computed within 'System.out.println' might produce '105' due to string concatenation rule precedence, instead of '15'. To correct, use parentheses like '(a+b)' to prioritize arithmetic evaluation .
Directly accessing and modifying object attributes can impact methods that use those attributes in calculations. In the Circle class, setting 'c.r=5.0' directly updates the radius, affecting the results of methods like 'circumference()' and 'area()', producing outputs of 31.4159 and 78.53975 respectively .
In Java, the right shift operator (>>) maintains the sign of the number whereas the unsigned right shift operator (>>>) does not. For a negative number like -10, -10>>2 results in -3 (preserving the sign bit), whereas -10>>>2 results in a large positive value due to zero-fill, i.e., 1073741821 .
The left shift operation (<<) multiplies a number by 2 raised to the power of the specified shift amount, maintaining the sign of the number. For example, 10<<2 results in 40 (10*2^2), while -10<<2 results in -40. The operation just shifts the bit pattern to the left for both positive and negative numbers, filling with zeros .
The discriminant (d = b^2 - 4ac) of a quadratic equation indicates the nature of the roots. If d < 0, the equation has no real roots. If d = 0, the roots are real and equal. If d > 0, the roots are real and distinct. For example, when a = 2, b = 6, c = 4, d = 4 which is > 0, indicating real and distinct roots .
A constructor in Java is a block of code similar to a method that is invoked when an object of a class is created. It is used to initialize objects. For example, in the 'Cons' class, creating a new instance by 'new Cons()' calls the constructor and prints 'In Constructor' .
Post-increment (a++) and pre-increment (++a) operations in Java affect the value of a variable differently. With a post-increment, the original value is used in the expression before the increment occurs, resulting in 'a++ is 10'. In contrast, pre-increment increments the value first and then applies it, yielding '++a is 12' when initially a = 10 .
A common mistake is forgetting to use parentheses to control operation order. For instance, 'a+b' results in '105' instead of performing arithmetic addition because concatenation occurs first. Using parentheses as in '(a+b)' ensures the arithmetic calculation happens first, resulting in '15' .
The logical NOT operator (!) inverts the boolean value of a variable. When applied to a boolean 'a' which is true, the result is false, and similarly, when applied to 'b' which is false, the result is true. This shows the inversion effect: '!a is false' and '!b is true' .