Operators Use in C and C++
Operators Use in C and C++
15) Determine the result of applying any operator including assignment operators and instanceof to
operands of any type class scope or accessibility or any combination of these.
Result of applying "+" operator to any combination of variables or constants of any type:
If you add two primitives a lower type is cast to a higher type in the following hierarchy:(implicit type
casting)
−−−−−−−char
|
V
double float long int short byte
For a + expression with any operand that is not of primitive numeric type :
• one operand must be a string object, otherwise the expression is illegal
• If one operand is a String object and the other is not, the non−string object is converted to a string
and result is concatenated.
Remember : Using + operator on objects other than numbers and Strings would result in a compiler error. If
either of the operand of a + expression is a string object, the meaning of the operator is changed from
numeric addition to concatenation of text.
eg,
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) − (d * s);
1
System.out.println((f * b) + " + " + (i / c) + " − " + (d * s));
System.out.println("result = " + result);
• In the first subexpression, f * b, b is promoted to a float and the result of the subexpression is float.
• Next, in the subexpression i /c, c is promoted to int, and the result is of type int.
• Then, in d * s, the value of s is promoted to double, and the type of the subexpression is double.
• Finally, these three intermediate values, float, int, and double, are considered. The outcome of float
plus an int is a float.
• Then the resultant float minus the last double is promoted to double, which is the type for the final
result of the expression.
another eg,
float f=4.6;//give compilation error
float=4 ;//compiles fine
• The reason for the first one not compiling is 4.6 is treated as double and you should cast it explicitly to
convert double to float. (4.6 is double and 4.6f is float)
• The reason for the second one getting compiled is 4 is treated as an integer, so assigning an int to float don't
need an explicit casting.
another eg,
short s = 8;
int i = 10;
float f = 6.5f;
doubld d = 10.5;
if(++s * i >= f/d)
System.out.println("greater");
else
System.out.println("smaller");
This will print "true" because flag is assigned a boolean value and which is what if expects.
gives a compiler error saying, Incompatible type for if. can't convert java.lang.String to boolean
obviously b and mb are instances of JButton and myButton respectively. mb instanceof JButton returns true
because myButton extends JButton.
Finally, b instanceof myButton prints false because b is not subclass of myButton.
another eg,
class furniture {
class table extends furniture{}
class chair extends furniture{}
public class wood {
public static void main(String arg[]) {
furniture f=new table();
if(f instanceof table) System.out.println("table");
if(f instanceof furniture) System.out.println("furniture");
if(f instanceof chair) System.out.println("chair");
else System.out.println("I am not!");
}
}
Remember : instanceof is an operator which is used with superclasses to tell if you have a particular
subclass.
16) Determine the result of applying the boolean equals(Object) method to objects of any combination of
the classes java.lang.String, java.lang.Boolean and java.lang.Object.
String s1 = new String("hi");
String s2 = new String("hi");
System.out.println(s1 == s2);
would return true. Because anytime Java encounters a literal String it gives you a new String and adds it to
the
literal pool. In the above code, by the time Java gets to s2, "hi" already exists as a literal, so it uses the same
object
for both object references s1 and s2. So the test has to return true.
String s1 = "abc" + "def";
String s2 = new String(s1);
if(s1.equals(s2)) System.out.println("equals");
if(s1==s2) System.out.println("==");
output :
equals
So, for String and Boolean, equals() is overridden to provide an equality test. For any other class that is a
direct extension of Object and does not override equals(), the test will return false.
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
17) In an expression involving the operators | & || and variables of known values state which operands are
evaluated and the value of the expression.
Operator Precedence :
||
If you use the || and &forms, rather than the | and forms of these operators, Java will not bother to evaluate
the right−hand operand when the outcome of the expression can be determined by the left operand alone.
This is very useful when the right−hand operand depends on the left one being true or false in order to
function properly.
if (denom != 0 &num / denom > 10)
Since the short−circuit form of AND (is used, there is no risk of causing a run−time exception when denom is
zero. If this line of code were written using the single version of AND, both sides would have to be evaluated,
causing a run−time exception when denom is zero.
It is standard practice to use the short−circuit forms of AND and OR in cases involving Boolean logic,
leaving the single−character versions exclusively for bitwise operations.
Another eg,
boolean b, c, d;
b = (2 > 3); // b is false
c = (3 > 2); // c is true
d = b &c;
When Java evaluates the expression d = b &c, it first checks whether b is true. Here b is false, so b &c must
|| is logical or. || combines two boolean variables or expressions and returns a result that is true if either or
both of its
operands are true. For instance
boolean b;
b = 3 > 2 || 5 7; // b is true
b = 2 > 3 || 5 7; // b is still true
b = 2 > 3 || 5 > 7; // now b is false
Remember :
• For &operation, if one operand is false, the result is false, without regard to the other operand
• For || operation, if one operand is true, the result is true, without regard to the other operand
op1 &op2 op1 and op2 are both true, conditionally evaluates op2
|| op1 || op2 either op1 or op2 is true, conditionally evaluates op2
op1 op2 op1 and op2 are both true, always evaluates op1 and op2
| op1 | op2 either op1 or op2 is true, always evaluates op1 and op2
0 1
0 0 0
1 0 1
eg, 12 13
• the binary representation of 12 is 1100, and
• the binary representation of 13 is 1101.
1100
1101
−−−−−−−−−−
1100 ie., 12 in decimal
−−−−−−−−−−
00101010 (42)
00001111 (15)
−−−−−−−−−−−−−−
00001010 (10)
| 0 1
0 0 1
1 1 1
1100
| 1101 ie., 13 in decimal
−−−−−−−−−
1101
−−−−−−−−−
00101010 ( 42)
| 00001111 (15)
−−−−−−−−−−−−−−
00101111 (47)
−−−−−−−−−−−−−−
output :
a = 0011
b = 0110
After running this program, you will see that the same logical rules apply to boolean values as they did to
bits. As you can see
from the following output, the string representation of a Java boolean value is one of the literal values true or
false:
ouput :
a = true
b = false
a|b = true
ab= false
a^b = true
!a = true
!a = false
The shift operators include left shift > , and unsigned right shift >>>.
+ ve >> :
−ve >> :
2's compliment :
−ve >>> :
+ve
18) Determine the effect upon objects and primitive values of passing variables into methods and
performing assignments or other modifying operations in that method.
Variables of built−in types are passed by value, objects are passed by reference.
• "Passing by reference" means that a referenceto (ie. the address of) the argument is passed to the
method. Using the reference, the method is actually directly accessing the argument, not a copy of it.
Any changes to the method makes to the parameter are made to the actual object used as the
argument.
Passing a handle is the most efficient form of argument passing, but if you don't want to modify the outside
object and want changes to affect only a local copy you use "pass by value".
• "Passing by value" means that the argument's value is copied, and is passed to the method. Inside the
method this copy can be modified at will, and doesn't affect the original argument.
output :
inside print : 15
inside main : 10
• A method call conversion happens when you pass a value of one type as an argument to a method
that expects a different type. For eg, Explicit cast needed to convert double to int.
eg,
float f;
double d;
f = 3.45f;
d = Math.cos(f); //passing float to method that
expects double
Remember :
• Passing by reference − affects the object itself and not the copy of it.
• Passing by value − affect only a local copy.
• Widening conversions are permitted; narrowing conversions are forbidden.