This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes Adding two byte in by using Different Syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding two byte in by using Different Syntax " Watch "Adding two byte in by using Different Syntax " New topic
Author

Adding two byte in by using Different Syntax

Prakash Rai
Ranch Hand

Joined: Jan 10, 2011
Posts: 106



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: 19059
    
  40

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: 106

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: 6109
    
    6

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: 106

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: 4489
    
    8

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: 106

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding two byte in by using Different Syntax