• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

chars using the + operator

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how would this statement result:
char temp = 'a' + 1 ;
??? I'm thinking it will be 'b', or will it concatinate 1 on the end of 'a' and throw an error? Thanks in advance!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is b. No compile errors and no runtime errors.
 
Greg Mehlhose
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on the concept a little more. The expression undergoes two stages of implicit conversion.
1 - Arithmetic promotion: The right hand side of the equation undergoes arithmetic promotion where everyting is promoted to an int.
2 - Assignment promotion: The equals conversion has really no effect on the primative type of the left hand side of the equation. Assigning an int to a char will compile as long as the int value (located on the left hand side) is in the legal range of a character value (located on the right hand side).
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that what Greg said is true only because you were using literal constants in the right hand side expression. If any or both of the operands were variables, a compile time error would have occurred.
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in that case would this work?
char temp = 'a' + 1 ;
if (temp == ('a' + 1))
{
// Do something
}
In this case, would the program "Do something"?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one way to find out man, give it a spin with your JVM
[This message has been edited by JUNILU LACAR (edited June 20, 2001).]
 
Conrad Kirby
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well OK...
 
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The '+' is an overloaded operator. It can act in 2 different ways depending on the operands. If one of the operands is a String it will concatenate. If there are no Strings and just primitives, including char, it will add. If there is a primitive and any other non String object it will not compile. This is all assuming 2 operands. If we have more operands than we apply the same rules left to right.
For example : 3 + 4 + String;
the 3 and 4 should be added first then concatenated to the String.
Regarding the earlier commnet about whether literals or variables is used is totally false which you can test easily enough.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by herb slocomb:
> Regarding the earlier commnet about whether literals or
> variables is used is totally false which you can test
> easily enough.
Herb, you're wrong. It does make a difference if you have variables in the expression rather than literal constants. The compiler chokes on this code:
<pre>
char a = 'a';
char c = a + 1;

C:\java2\Test.java:12: possible loss of precision
found : int
required: char
char c = a + 1;
^
1 error
</pre>
Whereas it compiles cleanly with
char c = 'a' + 1;
BTW, in this case, the overloading of "+" for Strings does not come into play. The operands are converted into ints.
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, yes JUNILU is right in this specific context. I was trying to give general rules but didn't give examples... Here's some examples to illustrate what I was saying...
System.out.println(1 + 2 ) ---> 3
Sytem.out.println("1" + 2) ---> 12
System.out.println( 1 + 2 + "3") ---> 33
Using variables works too :
int i = 1; char c = 'c'; String s = "Hi"
System.out.println(s + c)
i = i + c; // addition will occur OK if result variable is
int,float, or double, but not char or smaller
s = s + c // concatenation
As I said, "+" is overloaded and how it operates depends on the context. Sorry for not giving more details...

[This message has been edited by herb slocomb (edited June 22, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic