Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt??

 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
patrick J. d'cousta
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't compile in Java 5.0 either. What are you using Microsoft J++ ?
 
Kevin Lam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic