• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Assignment operator and narrowing type cast

 
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:
18

The above code snippet produces an output which I thought would throw an error. My understanding on the assignment operator was it will behave like val = val + x so val would also type cast to int and addition would be performed resulting in int value. So, when it tries to assign that to a short value it would throw an error.

Could anyone please explain how this works internally in java?
 
Marshal
Posts: 72926
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When did you think the error would become evident? And which line did you expect it on? Did you write the question yourself, or did you find it somewhere? If the latter, please always tell us where.

Two relevant Java® Language Specification (=JLS) sections:- About constant expressions, but I can't find where they can be used without a cast: 1. About assignment: 2.
 
Akshayyha Krishnamurthy
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie The question was written by me since I had the doubt. I have added some additional details as well to show how I got a query.
 
Creator of Enthuware JWS+ V6
Posts: 3392
312
Android Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 I have added some additional details as well to show how I got a query.


Can you please put those details in a new post?
 
Campbell Ritchie
Marshal
Posts: 72926
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshayyha Krishnamurthy wrote:. . . I have added some additional details . . . .

As Frits said, please don't edit old posts like that. Please post the new information as a new question. I have reverted the changes.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With an “operator followed by assignment” operator, the compiler throws in an implicit cast of the RHS value to the LHS type.  (You can sort of reason why why it should or why it should not be that way, but the bottom line is, as usual, “thems are the specs.”)

Joe Sock - i hope that helps!
 
Campbell Ritchie
Marshal
Posts: 72926
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, the details are in the link I posted earlier (No 2). It means the variable on the left retains its type and allows a += or -= or something= expression always to compile without needing a cast. It also means you can get confusion if you start mixing types:-The example in the link I posted shows similar potential for confusion, by adding 4.6.
 
straws are for suckers. tiny ads are for attractive people.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic