• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

doubt??

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't compile in Java 5.0 either. What are you using Microsoft J++ ?
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic