This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "output for this program" Watch "output for this program" New topic

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

Joined: Apr 14, 2004
Posts: 10336

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

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11881

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="" 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
I agree. Here's the link:
subject: output for this program
It's not a secret anymore!