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 Video Library
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
View Events Video Library
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Get and Set PDF Form Fields in Java
  • Microfrontends for Quarkus Microservices
  • Building AI Applications With Java and Gradle
  • Demystifying Project Loom: A Guide to Lightweight Threads in Java

Trending

  • Generative AI: A New Tool in the Developer Toolbox
  • Best Practices for Writing Clean Java Code
  • Causes and Remedies of Poison Pill in Apache Kafka
  • Exploring Edge Computing: Delving Into Amazon and Facebook Use Cases
  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.10K 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.

Related

  • How To Get and Set PDF Form Fields in Java
  • Microfrontends for Quarkus Microservices
  • Building AI Applications With Java and Gradle
  • Demystifying Project Loom: A Guide to Lightweight Threads in Java

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: