Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
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
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# += and ++

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
How come the following work?
byte a=2,b;
b=a+1; // doesn't work - needs a cast
b=a++; // it works, why? - shouldn't it need a cast?
long L=1;
int i=0;
i=i+L; // not okay - needs a cast
i+=L; // okay, why? - shouldn't it need a cast also?

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
when +=,++ is used cast is automatic

Greenhorn
Posts: 10
• Number of slices to send:
Optional 'thank-you' note:
The statement b = a++ works not because there is an automatic cast, but because a is incremented as a byte and assigned to b, which is also a byte.
In the statement i = i + L the cast is automatic, and it is to a long type. A long won't fit into the int on the left side of the assignment operator, so you have to do an explicit cast to force the issue.
The statement i += L works because of the compound operator; i becomes a long because the largest data type in the expression is a long. I hope this hslps.
[This message has been edited by Barbara Dyer-Bennet (edited September 15, 2000).]

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
I tried the same with double i.e saying i+=d(where double d=1.0)and when i displayed it on console I'm getting int value and not as double as pointed out...(that the compound statement will be casted to largest data type in the expression) .

Anonymous
Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
hi,
i tried with bytes byte b1=1,int i=1;and when i perform b1+=1;
it is giving explicit casting is required...i could not get to
final conclusion on this..is there any version problem..

Ranch Hand
Posts: 41
• Number of slices to send:
Optional 'thank-you' note:
Hi Satish,
I agree with barbara. it is that when the += operator is used its equivalent to the operator and cast before the right side. whereas its not the case with + operator.

Cheers
Satish

jayanthi
Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
with +=, ++ cast is automatic
i think so...
jayanthi

Anonymous
Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
OOPs!! I posted it wrongly, b1+=1 is working fine.
Sorry for the mistake..

 A feeble attempt to tell you about our stuff that makes us money Free, earth friendly heat - from the CodeRanch trailboss https://www.kickstarter.com/projects/paulwheaton/free-heat