• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about null

 
Sakthi Kani
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chit Ming Chong
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you , very clear explanation
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
skywalker :,

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

Nick
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic