有关Integer.parseInt()和强制转换的Java初学者问题 [英] Beginner Java Question about Integer.parseInt() and casting

查看:368
本文介绍了有关Integer.parseInt()和强制转换的Java初学者问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当像下面的语句中那样进行转换时:-

so when casting like in the statement below :-

int randomNumber=(int) (Math.random()*5)

它会导致随机数。

另外,我刚遇到Integer.parseInt()的方法也是如此!

Also there's this method I just came across Integer.parseInt() which does the same !

即返回整数

为什么用两种不同的方式将值设为int?

Why two different ways to make a value an int ?

我也进行了搜索,它说parseInt()将字符串作为参数。.这是否意味着parseInt()仅将String转换为整数?

Also I made a search and it says parseInt() takes string as an argument.. So does this mean that parseInt() is ONLY to convert String into integer ?

那么(int)的铸造呢?

What about this casting then (int) ?? Can we use this to convert a string to an int too ?

对不起,如果它听起来像是一个愚蠢的问题,那么。我只是感到困惑并试图理解

sorry if it sounds like a dumb question..I am just confused and trying to understand

帮助?

推荐答案

Integer.parseInt与演员表的功能不同。

Integer.parseInt does not do the same thing as a cast.

让我们看一下您的第一个示例:

Let's take a look at your first example:

int randomNumber=(int) (Math.random()*5)

Math.random返回一个双精度数,当您将一个双精度数乘以一个int时,Java会认为结果成为双倍。因此,表达式Math.random()* 5具有double类型。您想要做的就是将该值分配给int类型的变量。默认情况下,如果您未明确告知编译器可以这样做,则Java不允许您将double值分配给int类型的变量。基本上,您可以考虑将一个double转换为一个int,告诉编译器,我知道这个int变量不能容纳这个double值的小数部分,但是没关系,只需截断它即可。

Math.random returns a double, and when you multiply a double by an int Java considers the result to be a double. Thus the expression Math.random()*5 has a type of double. What you're trying to do is assign that value to a variable of type int. By default Java will not allow you to assign a double value to a variable of type int without your explicitly telling the compiler that it's ok to do so. Basically you can think of casting a double to an int as telling the compiler, "I know this int variable can't hold the decimal part of this double value, but that's ok, just truncate it."

现在来看一下字符串到int的转换:

Now take a look at the conversion of a String to an int:

int value = Integer.parseInt("5");

字符串 5不能立即转换为整数。与双精度字符不同,双精度字符可以定义为可以通过删除小数部分将其转换为整数,而字符串则不能轻易或始终如一地转换为int。 5, 042和 1,000都具有整数表示形式,但是类似 Hello,World!之类的东西。才不是。因此,没有用于将String转换为int的一阶语言功能。而是使用一种方法来解析String并返回值。

The string "5" is not immediately convertible to an integer. Unlike doubles, which by definition can be converted to an integer by dropping the decimal part, Strings can't be easily or consistently converted to an int. "5", "042", and "1,000" all have integer representations, but something like "Hello, World!" does not. Because of this there is no first order language feature for converting a String to an int. Instead, you use a method to parse the String and return the value.

因此,要回答所有问题:

So to answer all your questions:

为什么用两种不同的方法将值转换为int?

Why two different ways to make a value an int ?

您必须考虑什么您要转换的值的类型是。如果要将基本类型转换为int类型,则可以使用强制类型转换;如果要转换对象,则需要使用特定于该类型的某种转换方法。

You have to take into account what the type of the value you are converting is. If you're converting a primitive to an int you can use a cast, if you're converting an Object you'll need to use some sort of conversion method specific to that type.


我也进行了搜索,它说parseInt()将字符串作为参数。.这是否意味着parseInt()仅将String转换为整数?

Also I made a search and it says parseInt() takes string as an argument.. So does this mean that parseInt() is ONLY to convert String into integer ?

正确。您不能将任何其他类型传递给parseInt方法,否则会出现编译器错误。

Correct. You cannot pass any other type to the parseInt method or you will get a compiler error.


那么,这种类型的转换(int)如何?

What about this casting then (int) ?? Can we use this to convert a string to an int too ?

不,强制转换为int仅适用于原始值,在Java a String不是基元。

No, casting to int will only work for primitive values, and in Java a String is not a primitive.

这篇关于有关Integer.parseInt()和强制转换的Java初学者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆