This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interesting " + "

 
Vegad Arvind
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
System.out.println(true + null); //compile error
System.out.println("test" + true);//print testtrue
System.out.println("test" + null);//print testnull
Can anybody explain.
Thnaks
Avi
 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here,
System.out.println(true + null); //compile error
"true" is value of boolean, System can not find right overload println(boolean) for it, that is why you have compile error.
System.out.println("test" + true); //print testtrue
for this, system found println(String) for it, and "+" converts the boolean value true to string, because its first operand is string "test", that is why you have "testtrue".
Java overload "+" for string, but only for string.
same thing happened to thelast println() , null is treat as reference variable.
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RHE says that:
if both operands are not String objs, the non-String obs are converted to a String and then concatenated.
true and null r two objects that r not String...
so why the compile error?
 
shashank hiwarkar1
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mansoor,
+ operator is overloaded for string concatanation.
this produces result as one string followed by other.
But for that one should be string object, and then other if not string is converted as string object.
Let us consider
System.out.println(2+3);
it gives 5; and not 23
because out of two none is string object.
Now consider
System.out.println("true" +2 + 3);
o/p true 23
because 2 and 3 are converted as string object.
Now with
System.out.println(true+null)
where true/false and null(default value of String object) are reserved literals of java, (they are not primtive datatypes)
IT will NOT give the compilation error
but give o/p as truenull.
as null is default value of String literal'
with Syste.out.println("true"+null)
"true" is string and hence the other literal is converted as string and o/p truenull.

If I am wrong with my concepts I will welcome any suggestions.
shashank
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shahshank:
the println(true+null) gives a compile time error.
and the RHE statement is still unexplained!
 
shashank hiwarkar1
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mansoor,
try this

public class shift{
public static void main(String args[]) {
System.out.println(true+null);
}
}
o/p is truenull.
shashank
 
Rakesh Sharma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahshank ir right. The output is "truenull".
 
pankaj agarwal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mansoor
think of the expression in parantheses
(true+null)
Now, you know true is a boolean literal(a boolean can take only true and false, remember?).
No arithmatic operation is allowed on boolean types, and thats why you get compile time error. correct me if i am wrong
pankaj
 
shashank hiwarkar1
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Pankaj,

You are not doing any arithmatic operation there .
+ is the overloaded operator for string concatanation.
Shashank
 
Yogesh Mhatre
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shashank,
the program u mentioned below does not work on my system with jdk1.2.2 it gives compile time error saying
shift.java:3: Incompatible type for +. Can't convert boolean to int.
shift.java:3: Incompatible type for +. Can't convert null to int.
can u pls throw light on it agn as according to me if u r using the "+" operator either of the operand mst be String only then the other operand can be converted to String.
Yogesh
try this

public class shift{
public static void main(String args[]) {
System.out.println(true+null);
}
}
o/p is truenull.
shashank[/B]

[This message has been edited by yogesh mhatre (edited February 19, 2001).]
 
shashank hiwarkar1
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogesh,
I am working with jkd1.3, and it works without any error.
Probably(I am not sure) it is taking null as default value of String object and doing toString() for true.
I may Be wrong, Can Anybody throw some more light on this.
Shashank
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correct me if i am wrong
in a println statement if the first argument that is encountered is a string, it performs string concatenation
for e.g
Systeml.out.println("true" + null)
output is truenull
but for
System.out.println(true + null)
the output is compilation error
what would happen if we said
System.out.println(true + "null")
 
nan sh
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sona,
"what would happen if we said
System.out.println(true + "null")"

output will be truenull, because "null" is a string.

what is the output of following code?
public class Test {
public static void main(String[] args){
int i = 4, j = 7;
String k = "string";
System.out.println(i+j+k+i+j);
}
}
a. 47sting47
b. 11string11
c. 47string11
d. 11string47
e. Non of above is correct
Hope it helps
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the + concatenation operator to work, atleast one operand must be a string. otherwise it will throw compiler error.
------------------

***********************************************
Sun Certified Programmer for Java 2 Platform
***********************************************
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously there was something that changed bewteen the JDK 1.2.1 and the JDK 1.3 becuase I'm running 1.3 and I don't get an error when I use System.out.println( null + true ); ...
Joe
 
gayathri bhushan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
i was going thru ur discussion and got confused. I compiled and ran the program and the result i got was truenull....now my question is will java overload "+" only if one of the operand is string ..but in this case none of the operands null and true are sting yet it overloads "+" why is it so?
 
John Dale
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I've come up with in trying to understand why System.out.println(true + null) may be legal. Others have observed it seems to work under JDK 1.3, but not under JDK 1.2.2.
According to the Java Language Specification, 2nd edition (JLS), section 4.1:
"There is also a special null type, the type of the expression null, which has no name. ... The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is a special literal that can be of any reference type."
It seems that the JDK 1.3 compiler considers null to be of type String when it checks to see if either argument of the + is a String. Then it processes the expression per sections 5.4 and 15.18.1. Having said this, I don't know my way around the JLS enough determine whether the JLS allows or mandates the behavior of either 1.2.2 or 1.3.
There are other situations in which the compiler might consider a null type to be of some other type. For example, when matching method arguments:
class Junk4 {
static void test(Object o ) {
System.out.println("test(Object " + o + ")");
}
static void test(String s){
System.out.println("test(String " + s + ")");
}
public static void main(String[] args) {
test(null);
}
}
Here, the "null type" matches both Object and String, so the compiler uses String because it is a subtype of Object.
Just to show there is nothing special about String here:
class Junk3 {
static void test(Object o ) {
System.out.println("test(Object " + o + ")");
}
static void test(Integer i){
System.out.println("test(Integer " + i + ")");
}
public static void main(String[] args) {
test(null);
}
}
 
Tanveer Mehmood
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
System.out.println(true + nul);
Gives truenull as o/p and doesn't generate any compiler error on jdk1.3...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic