• 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
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

default value for object refernce

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the default value for an object refernce. Is the null a default value for these refernce variables. What if an object is instantiated and it has no constrcutor... Is the default value null?

When you use that refernce varaible to the left of instanceof operator with the Object on the right side of the operator. Will it produce false?
 
Marshal
Posts: 26690
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default value for a variable that can contain an object reference is null. But your question "What if an object is instantiated and it has no constrcutor" doesn't mean anything. You can only instantiate an object by calling one of its constructors, and it isn't possible for a class to not have any constructors.

And your other question is what does "null instanceof Object" return? I will leave you to write a one-line test program for that.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All classes have a constructor. If none are explicitly written into the class definition, then a default no-args constructor is provided by the compiler.

Class/instance variables for non-primitive types are automatically initialized to null. However, local variables (e.g., declared within a method) must be explicitly initialized before they are used or a compiler error will result.

As for "null instanceof ..." See JLS 15.20.2.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic