Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

output for this program

 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Book {
private String title; // instance reference variable
Why it is not printing the output line for the following program?


public String getTitle() {
return title;
}
public static void main(String [] args) {
Book b = new Book();
String s = b.getTitle(); // Compiles and runs
if (s != null) {
String t = s.toLowerCase();
System.out.println("output is" + t);
}
}
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably "title" is null? Where do you set this property?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12126
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try running this:


[ June 12, 2008: Message edited by: fred rosenberger ]
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are implicitly assigned to null, if you don't give them a value. In your case, title is never given a value so it is set to null. HEnce your null check fails and there is no output
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sumaraghavi,

UseCodeTags.You aren't newbie anymore.
[ June 13, 2008: Message edited by: Vishal Pandya ]
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks lot all of you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic