• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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 ]
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!