It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about null" Watch "about null" New topic
Author

about null

Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
public class Test
{
public static void main(String[] args)
{
String t1 = null;
Object t2 = null;
Test t3 = null;
System.out.println(null); //1
System.out.println(t1); //2
System.out.println(t2); //3
System.out.println(t3); //4

}
}

Line 1 shows the compile time error, why?
Explain me....


" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Well that is because the reference to println is ambiguous, both method println(char[]) in java.io.PrintStream and method println(java.lang.String) in java.io.PrintStream match.

The compiler doesn't spit out garble. It's meaningful, useful, diagnostic, helpful information.


Tony Morris
Java Q&A (FAQ, Trivia)
Chit Ming Chong
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
The compiler cannot figure out what type it is. The literal null is not defined to be any type; An exception will be thrown because the println function could find the right overloaded method with appropiate parameter type to call.
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
hi

There is no exception, only compile time error...

To overcome the problem by using,

System.out.println((String) null); at //1.

Then the null will be printed....
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
i am not able to figure out the exact location of the problem.i.e between which two method the problem arises.

there is object type and string type then still how char[] comes into the picture..
anybody please
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
hi paramesh,

The println() method has been overloaded in many ways.

println(char[] c);
println(String s);
println(double d);
println(int i); and etc...

System.out.println(null); //1

Here compiler get confused which method ((char[] c) or (string s))to be invoked,so compiler gives an ambiguous error.

hope u clear...
Sunyaluk Boonmas
Greenhorn

Joined: Apr 18, 2005
Posts: 24
Thank you , very clear explanation


SCJP Passed(93%)<br />SCWCD Preparing<br />SCEA future.<br /> <br />Here is the great community!!
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
skywalker :,

Your name violates the Naming Policy, please change it at Your Profile.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi kani
can i say that because null is a keyword the compiler didn't complain about can't resolve symbol, no other keyword can't be used other than the null.
am i right
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
hi paramaesh,

null, true and false are reserved word not keywords.

u can use these three. no error will be occur.

If u give other than these three words compiler will complain.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
i beg your pardon kani can u clear my doubt..



if this is getting error because of the ambigious state then,

String t1 = null;

System.out.println(null);
System.out.println(t1);


why not this too....

Boolean s2 = new Boolean(false);

System.out.println(false);
System.out.println(s2);
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
hai paramesh,

----------------------
String t1 = null;
System.out.println(t1);
-----------------------
No error in the above code. Because we already defined t1 as String.

But in case of following code,

-------------------------------
Boolean s2 = new Boolean(false);
System.out.println(false);
System.out.println(s2);
-------------------------------

The println() method also has been overloaded with boolean value as,

public void println(boolean b)
public void println(int i) and etc.... [Refer API]

So the compiler will invoke the method of boolean. So there is no error.

hope u got it..
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
i am agreeing that the overloaded methods are selected based on the type of the variable.

my doubt is what will be the type of the null reserved word.why compiler is choosing char for the null reserved word only.
Sakthi Kani
Ranch Hand

Joined: Mar 29, 2005
Posts: 98
hai paramesh,

null is default value of objects. It has no specific datatype. So it can be given to String, char array, or object reference.

hope u understand...
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Parameshwaran,
U can have:
char[] s = null;
String s1 = null;
Object o = null;

So u can see that all these can have null as the value. being null means nothing. So from above u can make out that the refrence variable s of type char[] is pointing to nothing , same applies for other declarations above
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about null
 
Similar Threads
toString() and null
wait() and notifyAll()
equals()
RuntimeException
Consider this code