简体   繁体   English

整数值给出意想不到的结果

[英]Integer value of gives unexpected result

While working on a project of mine, I came across a problem. 在处理我的项目时,我遇到了一个问题。

When I run the following program: 当我运行以下程序时:

public class test {
    public static void main(String[] args) {
        String x = "1234";
        System.out.println("Int value: " + Integer.valueOf(x.charAt(0)));
        System.out.println("String value: " + x.charAt(0));
    }
}

it gives me the result: 它给了我结果:

Int value: 49
String value: 1

which left me confused because x = "1234" , so when it runs the forth line the result should be the same as: 这让我感到困惑,因为x = "1234" ,所以当它运行第四行时,结果应该是相同的:

System.out.println("Int value: " + Integer.valueOf(1));

which should in theory should give me: Int value: 1 理论上应该给我: Int value: 1

So what caused the result to be 49 instead? 那导致结果是49的原因是什么呢?

There's a difference between 两者之间有区别

System.out.println("Int value: " + Integer.valueOf(1));

and

System.out.println("Int value: " + Integer.valueOf(x.charAt(0)));

In the second line what's really happening is this - 在第二行,真正发生的是这个 -

System.out.println("Int value: " + Integer.valueOf('1'));

Notice that '1' is being passed as a Character not as an Integer. 请注意,'1'作为字符而不是整数传递。 As a result the Integer.valueOf return the ASCII value of character '1', which is 49. 结果,Integer.valueOf返回字符'1'的ASCII值,即49。

Refer to the ASCII table to see for yourself. 请参阅ASCII表以了解自己。

this is because following return UTF-16 value, rather than value itself 这是因为下面返回UTF-16值,而不是值本身

        System.out.println("Int value: " + Integer.valueOf(x.charAt(0)));

try using int x = Integer.parseInt("1234"); 尝试使用int x = Integer.parseInt("1234"); instead 代替

49 is the decimal value that corresponds to the ASCII character '1'. 49是对应于ASCII字符“1”的十进制值。 Take a look at this chart, and look at the "Dec" (decimal value) column that corresponds to Chr (character value) '1'. 看一下这个图表,看看与Chr(字符值)'1'对应的“Dec”(十进制值)列。

A link to an ASCII table that shows you how a given character maps to a given decimal 指向ASCII表的链接,显示给定字符如何映射到给定的小数

You used the function valueOf(String s) , not valueOf(int i) . 您使用函数valueOf(String s) ,而不是valueOf(int i) Documentation says, that it "returns an Integer object holding the value of the specified String.". 文档说,它“返回一个包含指定String值的Integer对象。”。 It's mean, that function returns ASCII code of sign "1", which is 49. 这意味着,该函数返回符号“1”的ASCII码,即49。

This is because you were printing the ASCII value of 1. 这是因为您打印的ASCII值为1。

Try Character.getNumericValue(char) 试试Character.getNumericValue(char)

Try this (with minimal change from your implementation): 试试这个(你的实现变化很小):

package general;

class CharToInt
{
public static void main(String[] args) {
      String x = "1234";

      System.out.println("Int value: " + Character.getNumericValue(x.charAt(0)));
      System.out.println("String value: " + x.charAt(0));
    }
}

你需要做的是将字符串“1”传递给方法“Integer.valueOf()”然后它将它解析为整数1.为此,你可以这样做

String x = "1234"; System.out.println("Int value: " + Integer.valueOf(s.substring(0,1));

one thing is how you see the argument passing to the function, but the second thing is what is the type of this argument. 一件事是你如何看到传递给函数的参数,但第二件事是这个参数的类型是什么。

x.charAt(0) returns '1' 

but must remember that the type is character . 但必须记住,类型是字符

So, in fact it returns the binary data like: 00110001 which one can read like 0x31(hex) what is 49(dec) what is the UTF-16 representation of character '1' . 所以,实际上它返回二进制数据,如: 00110001 ,其中一个可以读取像0x31(hex) 49(dec)是什么49(dec)什么是字符'1'的UTF-16表示。

Now... the function: 现在......功能:

Integer.valueOf( )

It expectes the number as argument, so it takes those binary data 00110001 and reads it like a normal number... to it is value of 49 它将数字视为参数,因此它需要那些二进制数据00110001并将其读取为正常数字......它的值为49

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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