Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

+= or =+

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was testing with operators, and I found my self
in this situation:
public class testArea1{
public static void main(String s[]){
char c = 'c';
int i = 10;
c+=i;//1- this will compile Ok
c=+i;//2- this will not (possible loss of precision:int required char).
}
}
why does line 1 compile fine, and line 2 fail?
what is the difference between += and =+
I thought they do the same operation??
please advice.
thanks
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an interesting problem that I don't have the answer.
The second operation is =+ which to me says assign a positive value. In this case its simple assignment so no arithmetic promotion occurs and it does not compile because it tries to assign an int to a char without an explicit cast.
The first operation has me stumped. My explanation would be that the first operator (+=) is add and assign. This can be rewritten c = c + i; In this case arithmetic promotion occurs so the ch is promoted to an int. But to store the result should require a cast to an char since this is a narrowing conversion. Can anyone shed some light on why this succeeds without the cast.
 
Maitham H
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So when I try this code
c+=i; //this will compile ok
c= c+i; //this will not
WHY???
I thought that c+=i is another way of writing c = c+i;
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c=+i is only saying c equals positive i. c+=i is not the same as c=c+i because the += operator does an automatic narrowing conversion so you dont have to cast to char.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
char c = 'c';
I thought that c+=i is another way of writing c = c+i;
Nope. Actually, c+=i means c = (char)(c+i);
c = c+i would return int values which will cause error as char can't hold int value.
As in Java all return value of arithmetic operation made on type smaller than int is widening to int value.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Khalid Mughal book:
"An extended assignment operator has the following syntax:
[variable][op]=[expression]
and the following semantics:
[variable]=([type])([variable][op]([expression]))...
x *= a evaulated as x = (T)(x * (a))
x /= a evaulated as x = (T)(x/(a))
( so on)
Given T as numeric type of x."
p.53
[This message has been edited by Vladimir Kositsky (edited January 07, 2001).]
[This message has been edited by Vladimir Kositsky (edited January 07, 2001).]
[This message has been edited by Vladimir Kositsky (edited January 07, 2001).]
 
This one time, at bandcamp, I had relations with a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic