Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char + String = ok?

 
Avander Be
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


To my surprise this compiles nicely, does the char gets promoted implicitly to a String or what kind of magic spell gets casted here?
 
dave green
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Azad Bajaj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Avander Be
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, have a beer!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic