• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding two byte in by using Different Syntax

 
Ranch Hand
Posts: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 107
MyEclipse IDE Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
reply
    Bookmark Topic Watch Topic
  • New Topic