This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

doubt??  RSS feed

 
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...
 
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 ]
 
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
 
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..
 
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++ ?
 
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
 
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!