| Author |
Adding two byte in by using Different Syntax
|
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 88
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 14612
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Prakash Rai
Ranch Hand
Joined: Jan 10, 2011
Posts: 88
|
|
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
Joined: Jan 03, 2004
Posts: 3141
|
|
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
Joined: Jan 10, 2011
Posts: 88
|
|
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
Joined: Apr 06, 2010
Posts: 2687
|
|
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
Joined: Jan 10, 2011
Posts: 88
|
|
Thanks A lot Jeff Verdegan...
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3141
|
|
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:
|
 |
 |
|
|
subject: Adding two byte in by using Different Syntax
|
|
|