• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

string and object (null considered string?)

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I have 2 questions regarding the following code. First question, why is it that null here is considered as a string and it would run if I will not include the object created with whatever. Second question, why do I get an error msg when I include whatever that it cannot resolve symbol?

[This message has been edited by Cindy Glass (edited October 25, 2001).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, I'm not sure what you're asking here Mag...I compiled the code, but put quotation marks around "whatever" to make it a String. The output was: String String. But I'm still not sure what you're asking??
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
[url = http://www.geocities.com/mjbruesch/games]My Java Games, I'm quite proud[/url]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Margaret,
'null' is really a 'null literal'. It's valid in Java and can be legally assigned to an Object; which is why <code>new Sample(null)</code> works. It gets treated as a String vs an Object because, when given a choice between two methods where the argument is valid for both parameters, the compiler will always choose the most specific parameter as a match. In this case, 'null' is a literal that can be handled as an Object and a String. String is more specific so the compiler uses <code>Sample(String s)</code> vs <code>Sample(Object o)</code>
'whatever' has no meaning in Java; it's not a literal or a built-in object and you haven't given it any definition so the compiler doesn't know what it is and gives you an error.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Margaret Tan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane,
I still do not fully grasp why Java does this but it is a bit clearer now, thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic