[Logo]
Forums Register Login
Aricthmetic operator cast problem
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 ??
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.
yes but here is 1st the conversation from long to int. Inside the arithmetic expresion?
(1 like)
2 is an int and x is a long so 2 gets converted to a long and then the multiplication occurs.
Aha so the rule is that in case you need to cast to lower type you get compilation error?
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.
Aha so the rule if you make a compiler cast it to lower type you get compilation error.
Aha why doesn't this compile?
(1 like)
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 +/
Because when bytes are used in arithmetic they are automatically converted to ints so the result of a + b is an int.
and if long is used in arithmetic operation it is not converted to int?
 

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.
 

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
I was actually looking for this:
https://www.w3resource.com/java-tutorial/java-arithmetic-operators.php
 

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.
I was looking for this part and I don't think it is uncorrect?
 

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
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);
 

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.
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.
Please show us the page with the good explanation.
The one I pasted above.
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.
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS


This thread has been viewed 4568 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 24, 2018 09:23:00.