Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding two byte in by using Different Syntax

 
Prakash Rai
Ranch Hand
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please Help me ::

Q.Why 1st one does not give any error and 2nd is giving error ?
Thanks in Advance:
Note:
1.also can be written b+=b1 or b=b+b1;
 
Henry Wong
author
Marshal
Pie
Posts: 21219
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prakash Rai wrote:

Please Help me ::

Q.Why 1st one does not give any error and 2nd is giving error ?
Thanks in Advance:
Note:
1.also can be written b+=b1 or b=b+b1;


The Java language specification has an implicit cast in the expanding expression. See section 15.26.2 ....


http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html


Henry
 
Prakash Rai
Ranch Hand
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Henry But I did't my ans? please tell me the point...

Here In one case it is doing Implicit casting but why not it is doing in 2nd case.?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prakash Rai wrote:Sorry Henry But I did't my ans? please tell me the point...

Here In one case it is doing Implicit casting but why not it is doing in 2nd case.?


He did give you the answer: It's because the JLS says so.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.2:
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.


http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.2:
Binary numeric promotion is performed on the operands (§5.6.2).
 
Prakash Rai
Ranch Hand
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Prakash Rai wrote:Sorry Henry But I did't my ans? please tell me the point...

Here In one case it is doing Implicit casting but why not it is doing in 2nd case.?


He did give you the answer: It's because the JLS says so.


What IS JLS.. Please tell me some Appropriate ans...
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS is the Java Language Specification.

It says (in the section that Henry referenced) that:
is equivalent to

It's this cast (implicit, because it's added automatically) that makes the difference. Any arithmetic operation between two integer types that are smaller than int results in an int. So a cast is needed to be able to assign it back to a byte.
 
Prakash Rai
Ranch Hand
Posts: 106
Eclipse IDE MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Prakash Rai wrote:Sorry Henry But I did't my ans? please tell me the point...

Here In one case it is doing Implicit casting but why not it is doing in 2nd case.?


He did give you the answer: It's because the JLS says so.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.2:
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.


http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.2:
Binary numeric promotion is performed on the operands (§5.6.2).




Thanks A lot Jeff Verdegan...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prakash Rai wrote:
Jeff Verdegan wrote:
Prakash Rai wrote:Sorry Henry But I did't my ans? please tell me the point...

Here In one case it is doing Implicit casting but why not it is doing in 2nd case.?


He did give you the answer: It's because the JLS says so.


What IS JLS.. Please tell me some Appropriate ans...


Please take the time to carefully read the answers that people give you, and to follow links that they provide. Henry said:

The Java language specification has an implicit cast in the expanding expression. See section 15.26.2 ....

http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic