Home > Back-end >  First problem: Java zero based data type conversion, every brother for help
First problem: Java zero based data type conversion, every brother for help

Time:12-01

Int I=10.0/5 the compiler error, because 10.0 is type double; Instead of this is possible: int I=(int) compiled by 10.0/5,

My question: why the following can also compile?
Int I=(int) (10.0/5), I think there is something wrong with the 10.0/5 or in counting, belong to the type double to 10.0 strong around alone, but why is ok? Compile without question, the output is correct

thank you

CodePudding user response:

After duoble type divided by five is duoble type, and receive value is an int type, this is an error, int I=(int) (10.0/5) in this step you speak of type int value transformation, not an error, this transformation as the results will be lost behind the decimal values

CodePudding user response:

Small type conversion for large type for implicit conversion, can automatically turn, while large type into small type need to specify the transformation type, otherwise you won't receive, int I=(int) (10.0/5), computing result for double, receiving type to int, you need to type coercion, type won't be able to receive, and turn you doubt the operation of belong to small to big, do not need to display the specified,

CodePudding user response:

It is the result of the operation was a double 2.0

CodePudding user response:

Thank you for reply to you

CodePudding user response:

Strong, show that is ok

CodePudding user response:

The above I asked I want to ask:

10.0/5: this form is a double type divided by an integer type, it is ok? Of course the result is a double type, the number of different types of operation what are the rules?

Must be int I=1 + 2 we know that 1 and 2 are int type is ok, the 10.0/5 in 10.0 as the appearance of the floating-point defaults to double type, then why not int? Though he looks like a double type

Thank you

CodePudding user response:

references to 6th floor magic honour person reply:
above I asked I want to ask:

10.0/5: this form is a double type divided by an integer type, it is ok? Of course the result is a double type, the number of different types of operation what are the rules?

Must be int I=1 + 2 we know that 1 and 2 are int type is ok, the 10.0/5 in 10.0 as the appearance of the floating-point defaults to double type, then why not int? Though he looks like a double type

Thank you
operation types, first into operation again, same type transformation according to the accuracy of low to high precision conversion, so the result is duoble

CodePudding user response:

Understand, thank you

CodePudding user response:

Assignment statements lvalue and rvalue type should agree,

CodePudding user response:

I also understand that even though it's omega

CodePudding user response:

Assignment statements on the left side of the replication type need greater than or equal to the scope of the assignment of the right side type, this can happen automatic type conversion, if the copy type on the left need to be less than or equal to the scope of the right type of assignment, you need to use strong
  • Related