• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Question on concatenation

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to of rules of concatination of operands that not primitive numeric type "One operand must be a string object,otherwise the expression is illegal"
But the following code compiles & runs fine

Char is neither a primitive numeric type nor it is String object.Then how the above expression is valid?
Thanks
Veena
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
probably the reason is that a char is an integer
under the hood.
Rattan
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena,
I think what has happened here is an integer addition, not a concatenation.
char a ='b' + 'c' is treated as an arithmetic expression and is evaluated as
a = 98 + 99 = 197
The character value for 197 is stored in 'a'.
 
Ravi Anamalay
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention that char is an integral type (unsigned 16 bit) and hence you can perform arithmetic expressions on them.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.I read in R&H even char is numeric type.
Veena
 
Enthuware Software Support
Posts: 4885
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Veena Point:
Char is neither a primitive numeric type nor it is String object.Then how the above expression is valid?


char IS a primitive type. In addition, it is also a numeric type (byte, short, int, long). So what you are doing is not a concatination but simple addition.
Further, since 'a' and 'b' are constants and the summation fits into a char, the compiler is allowing you to assign the sum to a char without a cast. But try this:
char a = 'a';
char b = 'b';
char c = a + b;
Then try this:
char ch1 = '\uFFF0'+'\u000f';
char ch2 = '\uFFF0'+'\u000f';
ch2++;
char ch3 = '\uFFF0'+'\u001f';<== ??
System.out.println((int)ch1);
System.out.println((int)ch2);
What will be the output?
Have fun
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Anil:

char a = 'a';
char b = 'b';
char c = a + b;


I tried above code.Why it doesn't give error when a&b are constants,but gives error when they are variables?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char a = 'a';
char b = 'b';
char c = a + b;
"I tried above code.Why it doesn't give error when a&b are constants,but gives error when they are variables? "
because assignment with constants includes range-aware conversion to type. but when they are variables, then there's a widening conversion to an int (5.6.2 Binary Numeric Promotion) and therefore the expression will be an int - so that will require an explicit cast.
 
money grubbing section goes here:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic