This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Quession on if(){} and so on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Quession on if(){} and so on" Watch "Quession on if(){} and so on" New topic
Author

Quession on if(){} and so on

Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
class sreejith
{
String s;
public static void main(String args[])
{
sreejith ks=new sreejith();
int j,i;
i=ks.hai();
j=ks.hello();
System.out.println(i+" "+j);
}
int hai()
{
if (s==null)| | (s.length()==0))
return 10;
else return 0;
}
int hello()
{
if (s==null)| (s.length()==20))
return 10;
else return 0;
}
}
what will be printed out?
A. 10 10
B. 10 null
C. 0 0
D. Compilation Error
E. An Exception in hai
F. An Exception in hello
Ans is "F"
isn't A I have a doubt on it
ruthie
Greenhorn

Joined: Nov 27, 2001
Posts: 3
when s is null. the use of s.length() will throw an nullpointerexception
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9048
    
  10
ruthie,
Please read http://www.javaranch.com/name.jsp and re-register. We would like you to continue to post here at JavaRanch.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
Originally posted by ruthie:
when s is null. the use of s.length() will throw an nullpointerexception


But s is null in if (s==null)| | (s.length()==0))
return 10;
else return 0;
}will throw an nullpointerexception too???
vishal avad
Ranch Hand

Joined: Nov 29, 2001
Posts: 45
hi hardes
in ur question in first case i.e. first if condition
short circuit operator | | is used.
meaning of is that if first condition is true i.e. s==null which is true then the resultant condition is going to be true only
irrespective of second conditions result so check for s.lenght() is no going to execute but in case of next if it is executed
due to use of binary | operator
so it throws nullpointerexception
rgds
vishal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quession on if(){} and so on