• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Quession on if(){} and so on

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when s is null. the use of s.length() will throw an nullpointerexception
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ruthie,
Please read http://www.javaranch.com/name.jsp and re-register. We would like you to continue to post here at JavaRanch.
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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???
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic