This week's book giveaway is in the Python forum.
We're giving away four copies of Hello World and have Warren & Carter Sande on-line!
See this thread for details.
Win a copy of Hello World this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Jxam

 
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
 
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.
 
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.
 
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.
 
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
 
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.
 
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?
 
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).]
 
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.

 
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.
 
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).]
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!