简体   繁体   中英

What is the difference between "int x = (int) x" and "x = (int) x" in java if I assign "double x = 2.3"?

if I assign "double x = 2.3", what is the difference between "int x = (int) x" and "x = (int) x" ? I'm new to java and use python before, in python, if I execute "x = 2.3; x = int(x); print(x)", x is 2.

java:

在此处输入图像描述

python:

在此处输入图像描述

You're re-declaring x , so in the line

int x = (int)x

The x on the right is not the same x you previously declared. So its value defaults to 0 so (int)x is 0. You normally wouldn't be able to have two x variables. That's a thing jshell lets you do, but in normal Java you couldn't do.

See this:

jshell> double x = 2.3
x ==> 2.3

jshell> int y = (int)x
y ==> 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM