aspose file tools*
The moose likes Beginning Java and the fly likes char + String = ok? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "char + String = ok?" Watch "char + String = ok?" New topic
Author

char + String = ok?

Avander Be
Greenhorn

Joined: Oct 16, 2007
Posts: 16


To my surprise this compiles nicely, does the char gets promoted implicitly to a String or what kind of magic spell gets casted here?


Steve Jobs: "Nobody uses Java anymore" ( Who the f*ck is Steve Jobs?)
dave green
Greenhorn

Joined: Oct 15, 2007
Posts: 10
nothing changes, the char is just being printed as a char next to the string. So on the screen it will look like it is part of the string when in fact you still have 1 String and 1 char.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

Originally posted by Avander Be:

To my surprise this compiles nicely, does the char gets promoted implicitly to a String or what kind of magic spell gets casted here?


Basically, when you are performing addition, and at least one of the two operands is a string. The compiler will convert the operation to ... create a stringbuffer, append the operands, and get the string result.

It is the string buffer (or string builder class) that is overloaded to take a char, and does the conversions necessary to append the char to the string.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Azad Bajaj
Greenhorn

Joined: Oct 25, 2007
Posts: 6
Originally posted by Henry Wong:
Basically, when you are performing addition, and at least one of the two operands is a string. The compiler will convert the operation to ... create a stringbuffer, append the operands, and get the string result.


Yes, As String is an immutable class, compiler uses Stringbuffer or Stringbuilder and shares 'the String' to append the values where as
System.out.println( s+ c );
System.out.println( c+ s );


c+s or s+c will not bring any change in the value of s anyways. No doubt two new string objects will be there in the memory other than s.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Henry Wong:

Basically, when you are performing addition, and at least one of the two operands is a string. The compiler will convert the operation to ... create a stringbuffer, append the operands, and get the string result.

It is the string buffer (or string builder class) that is overloaded to take a char, and does the conversions necessary to append the char to the string.


In other words

System.out.println(c + s);

is fully equivalent to (and will likely produce the exactly same byte code as)

System.out.println(new StringBuilder().append(c).append(s).toString());

This translation is hard coded into the compiler. So it really is "compiler magic".


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Avander Be
Greenhorn

Joined: Oct 16, 2007
Posts: 16
Thanks guys, have a beer!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: char + String = ok?