• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Aricthmetic operator cast problem  RSS feed

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have problem understanding this piece of code in Java


In my opinion the code above shoulnd't compile but it does.
It shouldn't compile because in line 2 there is an implicit conversation from long to int which shouldn't be allowed by compiler ??
 
Bartender
Posts: 3320
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Converting from an int to a long is a widening conversion and that is allowed because an int will always fit into a long without any losses.

It's explained in the JLS under widening conversions on page https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html but be warned the JLS can be difficult to read.
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes but here is 1st the conversation from long to int. Inside the arithmetic expresion?
 
Tony Docherty
Bartender
Posts: 3320
86
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 is an int and x is a long so 2 gets converted to a long and then the multiplication occurs.
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha so the rule is that in case you need to cast to lower type you get compilation error?
 
Tony Docherty
Bartender
Posts: 3320
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can explicitly cast to a narrower type but the compiler won't automatically do it for you. So if y was an int you would get a compilation error unless you explicitly cast x to an int.
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha so the rule if you make a compiler cast it to lower type you get compilation error.
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha why doesn't this compile?
 
Marshal
Posts: 60065
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the result of the arithmetic operation + is an int. You are only casting the left operand to a byte because cast has a higher precedence than +/
 
Tony Docherty
Bartender
Posts: 3320
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because when bytes are used in arithmetic they are automatically converted to ints so the result of a + b is an int.
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and if long is used in arithmetic operation it is not converted to int?
 
Sheriff
Posts: 23689
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Docherty wrote:You can explicitly cast to a narrower type but the compiler won't automatically do it for you. So if y was an int you would get a compilation error unless you explicitly cast x to an int.



Yes, that's what Tony said.
 
author
Sheriff
Posts: 23586
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:and if long is used in arithmetic operation it is not converted to int?



The long primitive type is wider than the int primitive type. And Java does not support the implicit narrowing of a long to an int -- if you want the result of the arithmetic operation to be converted to an int, you will need to explicitly cast it.

Henry
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was actually looking for this:
https://www.w3resource.com/java-tutorial/java-arithmetic-operators.php
 
Campbell Ritchie
Marshal
Posts: 60065
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:. . . https://www.w3resource.com/java-tutorial/java-arithmetic-operators.php

I hope the rest of that tutorial is better than that page, which is very unclear, and I believe contains errors. I think the errors are all near the beginning.
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking for this part and I don't think it is uncorrect?
 
Henry Wong
author
Sheriff
Posts: 23586
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:I was looking for this part and I don't think it is uncorrect?



Okay, so the statements that you posted are correct ... but ... how do these statements lead you to  believe that the code in your original post shouldn't compile?

Henry
 
Campbell Ritchie
Marshal
Posts: 60065
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That bit is correct. The earlier bits, e.g.
float div = 20 / 100;
are at best ambiguous, and I can imagine readers being very surprised about what they get from
System.out.println(div);
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:It shouldn't compile because in line 2 there is an implicit conversation from long to int



No, it doesn't.

John Losty wrote:I was looking for this part and I don't think it is uncorrect?



Read the third statement there carefully to understand why your previous affirmation is wrong.
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I the begging of this thread I didn't completely understand this operator towards the end I found a page on the internet that expains it well.
 
Campbell Ritchie
Marshal
Posts: 60065
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the page with the good explanation.
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The one I pasted above.
 
Campbell Ritchie
Marshal
Posts: 60065
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the part you copied and pasted is good. It shows you the same information about types as the Java® Language Specification (=JLS), It is the first part of that link that is unclear and shows incorrectly evaluated expressions. I challenge you to work out what each assignment actually does, and which of the solutions shown is incorrect.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!