aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt??" Watch "doubt??" New topic
Author

doubt??

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
class Agg{
public static void main(String argv[])
{
System.out.println(null + true);
System.out.println(true + null);
System.out.println(null + null);
}
}

this is compiling and printing the values then why in the mock exam it has been given as erro...


Thanks & Regards<br /> <br />-Srikanth
patrick J. d'cousta
Greenhorn

Joined: Oct 25, 2005
Posts: 24
The compiler does optimisation with string constants.the same is evaluated by compiler as
new StringBuffer().append(null).append(null);
[ October 28, 2005: Message edited by: patrick J. d'cousta ]
premraj cheguri
Greenhorn

Joined: Feb 20, 2005
Posts: 21
Hi Srikanth,
which version of java ur using. i tried ur code on java 1.4. it is giving compile error. i think the book from which u collected this sample is also for 1.4 i hope.

---Prem
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi..

Am using jdk1.4.2_04..and it isnt compiling in mine..

Dont know how it is compiling in yours..
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Don't compile in Java 5.0 either. What are you using Microsoft J++ ?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kevin Lam
Ranch Hand

Joined: Oct 27, 2005
Posts: 68
I am stupid, but can I ask what's wrong with it?

Is it because null and true aren't compatible? would it compile if null + "true"?


Kev
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Originally posted by Kevin Lam:
I am stupid, but can I ask what's wrong with it?

One is the null reference literal, one is a boolean literal. The + operator works with integer/floating point values and for String concatenation, not for adding unrelated literal types.
patrick J. d'cousta
Greenhorn

Joined: Oct 25, 2005
Posts: 24
Sorry for the incorrect answer in earlier post.See below code for what i was talking about.

class TestNull
{
static String s1,s2;
public static void main(String[] args)
{

System.out.println(s1+s2);
}
}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Kevin Lam:
I am stupid, but can I ask what's wrong with it?

Is it because null and true aren't compatible? would it compile if null + "true"?


Kev


1. No, Kevin, you are not stupid - stupid people do not ask questions or have doubts.
2. As Steve said above, + is only overloaded for String objects. If one of the operands of + is a String, then the operator attempts to convert the other operand to a String. null get converted to the String "null".

The best thing is to write code and compile and run it, at the same time ask questions as you go.

For instance I am wondering about what happens in the case of (String)null + true. Will that cast work? Will it produce "nulltrue"? I do not know, so let's try it...

Yes it does! But null + (String)true does not. Why not? Leave that to you...
[ October 28, 2005: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt??