This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interesting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interesting " + "" Watch "Interesting " + "" New topic
Author

Interesting " + "

Vegad Arvind
Ranch Hand

Joined: Jan 10, 2001
Posts: 42
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

Joined: Jan 05, 2001
Posts: 167
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.

Have you tried this Mock Exam Testing Engine yet?<br /><a href="http://www.mycgiserver.com/~nan111/index.html" target="_blank" rel="nofollow">www.mycgiserver.com/~nan111/index.html</a>
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
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

Joined: Feb 11, 2001
Posts: 25
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


shashank
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
shahshank:
the println(true+null) gives a compile time error.
and the RHE statement is still unexplained!
shashank hiwarkar1
Greenhorn

Joined: Feb 11, 2001
Posts: 25
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

Joined: Dec 29, 2000
Posts: 37
Sahshank ir right. The output is "truenull".
pankaj agarwal
Ranch Hand

Joined: Oct 13, 2000
Posts: 39
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

Joined: Feb 11, 2001
Posts: 25
hi Pankaj,

You are not doing any arithmatic operation there .
+ is the overloaded operator for string concatanation.
Shashank
Yogesh Mhatre
Greenhorn

Joined: Feb 15, 2001
Posts: 20
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

Joined: Feb 11, 2001
Posts: 25
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

Joined: Feb 14, 2001
Posts: 234
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")

sona<br />SCJP
nan sh
Ranch Hand

Joined: Jan 05, 2001
Posts: 167
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

Joined: Feb 01, 2001
Posts: 130
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
***********************************************


Sun Certified Programmer for Java 2 Platform
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
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

Joined: Jan 29, 2001
Posts: 39
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

Joined: Feb 22, 2001
Posts: 399
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

Joined: Feb 23, 2001
Posts: 51
Hi!
System.out.println(true + nul);
Gives truenull as o/p and doesn't generate any compiler error on jdk1.3...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interesting " + "