File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes string and object (null considered string?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string and object (null considered string?)" Watch "string and object (null considered string?)" New topic

string and object (null considered string?)

Margaret Tan

Joined: Sep 14, 2001
Posts: 20
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).]
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
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 =]My Java Games, I'm quite proud[/url]

Michael J Bruesch<br /><i>I code, therefore I am.</i>
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Margaret Tan

Joined: Sep 14, 2001
Posts: 20
I still do not fully grasp why Java does this but it is a bit clearer now, thanks!
I agree. Here's the link:
subject: string and object (null considered string?)
It's not a secret anymore!