DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Java += and implicit casting

Java += and implicit casting

Peter Lawrey user avatar by
Peter Lawrey
·
Dec. 20, 12 · Interview
Like (0)
Save
Tweet
Share
4.01K Views

Join the DZone community and get the full member experience.

Join For Free
this is from two popular answers to the question java += operator

question


until today i thought that for example:

i += j;

is just a shortcut for:

i = i + j;

but what if we try this:

int i =5;long j =8;

then i = i + j; will not compile but i += j; will compile fine.
does it mean that in fact i += j; is a shortcut for something like this i = (type of i) (i + j) ?
i've tried googling for it but couldn't find anything relevant.

answers

as always with these questions, the jls holds the answer. in this case §15.26.2 compound assignment operators . an extract:

a compound assignment expression of the form e1 op= e2 is equivalent to e1 = (t)((e1) op (e2)), where t is the type of e1, except that e1 is evaluated only once.

and an example:

for example, the following code is correct:

short x =3;
x +=4.6;
 
and results in x having the value 7 because it is equivalent to:

short x =3;
x =(short)(x +4.6);

in other words, your assumption is correct.



jesusfreke

lukas eder

a good example of this casting is using *= or /=

byte b =10;
b *=5.7;system.out.println(b);// prints 57

or

byte b =100;
b /=2.5;system.out.println(b);// prints 40

or

char ch ='0';
ch *=1.1;system.out.println(ch);// prints '4'

or

char ch ='a';
ch *=1.5;system.out.println(ch);// prints 'a'
Java (programming language)

Published at DZone with permission of Peter Lawrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Explainer: Building High Performing Data Product Platform
  • The Enterprise, the Database, the Problem, and the Solution
  • Type Variance in Java and Kotlin
  • Playwright vs. Cypress: The King Is Dead, Long Live the King?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: