Skip to content

Commit 932b96a

Browse files
author
Ram swaroop
committed
added content
1 parent a049cb0 commit 932b96a

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

_posts/2015-05-14-variables-and-literals.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@ class Casting {
273273
}
274274
{% endhighlight %}
275275

276-
There is another rule which you must be aware of, i.e, __the result of an expression involving anything int-sized or
277-
smaller is always an `int`__. Check this out:
276+
#### There are some rules which you must be aware of:
277+
278+
* The result of an expression involving anything int-sized or smaller is always an `int`, so we must explicitly cast
279+
it. Check this out:
278280

279281
{% highlight java %}
280282
byte a = 3; // No problem, 3 fits in a byte
@@ -301,6 +303,13 @@ byte c = (byte) (a + b);
301303

302304
solves the issue.
303305

306+
* In case of compound assignment operators, explicit cast isn't required. The below code compiles just fine:
307+
308+
{% highlight java %}
309+
byte b = 3;
310+
b += 7; // No problem - adds 7 to b (result is 10)
311+
{% endhighlight %}
312+
304313
#### What happens when you cast a large value to store it in a small container
305314

306315
When you do a explicit cast, the compiler just keeps __the number of bits (from right)__ that the variable type can hold

0 commit comments

Comments
 (0)