Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jxam

 
Neil He
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've come across this in Jxam:
String s=null;
Which throws an exception?
1: if((s!=null) & (s.length()>0))
System.out.println(s);
2: if((s!=null) && (s.length()>0))
System.out.println(s);
3: if((s!=null) | (s.length()>0))
System.out.println(s);
4: if((s!=null) | | (s.length()>0))
System.out.println(s);

The answer given is 1,2,3 & 4.?
surely 2 won't throw an exception?
Thanks for any help.
Neil
 
fraternity lawson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right in that question, surely 2 will not throw an exacption.Just compile and make sure who is right and who is wrong.
 
Balaguru Janarthanan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s.length()=method throws null pointer exception, so all will throw exception.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to know the difference between the bitwise '& |' operators and the logical '&& | |' operators.
In an expression involving bitwise operators, all the operands are evaluated.
In an expression involving logical operators - , the second operand may not be evaluated if-

  • the operand is && and the first operand evaluates to false
  • the operand is | | and the first operand evaluates to true

  • Hope that helps!

    ------------------
    Ajith Kallambella M.
    Sun Certified Programmer for the Java�2 Platform.
    IBM Certified Developer - XML and Related Technologies, V1.
 
Tracy Qi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&& and | | are short-circuit operators.
And correct answers will 2, 4
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
It might not be clear to all those in this thread. The correct answer is 1, 2, 3, and 4 (all) will throw exceptions.
The explanation, a very good one at that, is given above by the honorable Ajith (Sheriff).
Manfred.
 
shadow liu
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the correct answer is 1,3 and 4
why 2 will give an exception?
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so is the answer 1,2,3,4

[This message has been edited by sona nagee (edited May 02, 2001).]
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sona,
can u explain why 2 is a correct answer.since (s!=null) returns false and && is being used in the expression the second operand
(s.length()>0) will not get evaluated.so how will 2 throw an exception?
any explanation???
rajashree.

 
Anjali Purohit
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Ajit is right. His explanation is enough. The answer is 1,2,3 & 4.
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks Lot of misunderstanding going on this Thread.
Well, to start with let me give the answer first and then explain the reason for the same.
The answer is (1), (3) and (4) . Please note (2) does not throws any exception.
I have given the reason in my code below.



This question is a classical SCJP question where we may be tempted to jump to conclusion.
I guess this ends this Hot discussionin this thread.

Rgds,
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 03, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic