• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mock Question Doubt...EQ+  RSS feed

 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

Quaetion ID : com.enthuware.ets.scjp.v5.2.360



The output of the above program is false...

1. Can anyone explain in words, the sequence in which below expression in if statement is evaluated...
if (i1 = b1 == i2)


2. Does == has higher precedence than =..??

Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja
[ May 21, 2007: Message edited by: Hardik Raja ]
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did it get past the compiler?
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code will not compile!!
please give the proper code!!!

thanks®ards,
krishna
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anupam : Did it get past the compiler?



chinnikrishna bulusu : The code will not compile!!
please give the proper code!!!



ya it gets past the compiler....TRY IT OUT...
 
Amit Rampurkar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code will not compile

if (i1 = b1 == i2)

This line will throw an error because on one side you have a boolean and other side you have an integer.
[ May 21, 2007: Message edited by: Amit Rampurkar ]
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hardik,
The problem you have posted clearly gives compiler error.I tried it myself.
How can it be possible that it will compile as you can see it violates basic rules?
just check it out with the question again
 
bm kanakaraju
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ithink the question should be some thing like this
public class test4{
public static void main(String args[])
{
boolean b1 = false;
int i1 = 2;
int i2 = 3;
if (b1 = i1 == i2)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
 
Reet Goel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm preparing for SCJP 5 and while solving a mock question paper encountered this question:

class test {
public static void main(String[] args) {
test inst_test = new test();
int i1 = 2000;
int i2 = 2000;
int i3 = 2;
int i4 = 2;
Integer Ithree = new Integer(2); // 1
Integer Ifour = new Integer(2); // 2
System.out.println( Ithree == Ifour );
inst_test.method( i3 , i4 );
inst_test.method( i1 , i2 );
}
public void method( Integer i , Integer eye ) {
System.out.println(i == eye );
}
}

Answer: The correct answer is false true false.
Explanation: lthree and lfour are two seperate objects. if the lines 1 and 2 were lthree = 2 and lfour = 2 the result would have been true. This is when the objects are created in the pool. When the references i and eye in the pool are compared 2==2 results in true and 2000==2000 is false since it exceeds 127.

I couldn't understand that 2000==2000 is false since it exceeds 127. What's there with 127? Is it that objects created in pool doesn't accept the value above 127?

Also, suggest some links for mock exams.

Please help!

Thanks.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

two quotations from the Java Language Specification should answer question:

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.


For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.


The complete text can be found here: http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.7

If we are nitpicking, the result of inst_test.method(i1 , i2) is undefined. Depending on the implementation, the result may be true or false.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!