• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Object Reference Not Initialized

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All:
What will be the output of the following code:
public class Book{
private String title;
public String getTitle() {
return title;
}
public static void main() {
Book b = new Book();
System.out.println("The title is " + b.getTitle());
}
}
The book I am reading says: The title is null
Does that mean, the default value of the class String is interpreted as string "null". Or, is there soem explicit conversion during the execution of + operator of the String class??
I thought the output should be: The title is
[ August 16, 2003: Message edited by: Brit Wright ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Brit
all instance variables gets default initialization values.
-object ref = null
-int, long,short,double = 0
-boolean = false (i don recollect about char)
so ur String variable got initialized to null and so u get null in output. u'd get the output u expect if the string is "".
regards
maulin
 
Brit Wright
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default for char is '\u0000'.

Originally posted by Maulin Vasavada:
hi Brit
all instance variables gets default initialization values.
-object ref = null
-int, long,short,double = 0
-boolean = false (i don recollect about char)
so ur String variable got initialized to null and so u get null in output. u'd get the output u expect if the string is "".
regards
maulin

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brit,
Watch out this will generate runtime error???
public class Book{
private String title;
public String getTitle() {
return title;
}
public static void main(){ // public static void main(String args[])
Book b = new Book();
System.out.println("The title is " + b.getTitle());
}
}
Thanks,
Venu Gopal.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You can refer to JLS 15.18.1 for a better explanation
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic