---
title: "Java += operator"  
description: "Java += operator"  
author: "Anonymous User"  
published: 2015-05-02  
updated: 2015-05-02  
canonical: https://www.mindstick.com/forum/23178/java-operator  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# Java += operator

[Until today](https://answers.mindstick.com/qa/35860/until-today-what-are-the-greatest-advances-in-ai-artificial-inteligence) I thought that for example:\
i += j;is just a shortcut for:\
i = i + j;But what if we try this:\
[int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql) i = 5;[long](https://www.mindstick.com/articles/44565/white-wedding-dresses-long-prom-dresses) j = 8;Then i = i + j; will not [compile](https://www.mindstick.com/forum/2316/how-to-views-compile-in-mvc) but i += j; will compile fine.\
Does it [mean](https://yourviews.mindstick.com/view/80768/what-does-real-minority-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.

## Replies

### Reply by Anonymous User

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.An example cited from §15.26.2\
[...] 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.


---

Original Source: https://www.mindstick.com/forum/23178/java-operator

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
