aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jxam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jxam" Watch "Jxam" New topic
Author

Jxam

Neil He
Greenhorn

Joined: May 02, 2001
Posts: 1
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

Joined: Mar 11, 2001
Posts: 26
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

Joined: Nov 01, 2000
Posts: 37
s.length()=method throws null pointer exception, so all will throw exception.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Tracy Qi
Ranch Hand

Joined: Apr 23, 2001
Posts: 37
&& and | | are short-circuit operators.
And correct answers will 2, 4
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Mar 19, 2001
Posts: 33
I think the correct answer is 1,3 and 4
why 2 will give an exception?
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
so is the answer 1,2,3,4

[This message has been edited by sona nagee (edited May 02, 2001).]


sona<br />SCJP
rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
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

Joined: Apr 24, 2001
Posts: 11
Hi,
Ajit is right. His explanation is enough. The answer is 1,2,3 & 4.
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
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).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Jxam