my dog learned polymorphism*
The moose likes Beginning Java and the fly likes output for this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "output for this program" Watch "output for this program" New topic
Author

output for this program

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
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

Joined: Apr 14, 2004
Posts: 10336

Presumably "title" is null? Where do you set this property?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

try running this:


[ June 12, 2008: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
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


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hello Sumaraghavi,

UseCodeTags.You aren't newbie anymore.
[ June 13, 2008: Message edited by: Vishal Pandya ]
sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118
Thanks lot all of you
 
jQuery in Action, 2nd edition
 
subject: output for this program