java中integer valueOf,java中的Integer.valueOf()是否在C#中转换为int.Parse()?(Does Integer.valueOf() in java tra...

java中的Integer.valueOf()是否在C#中转换为int.Parse()?(Does Integer.valueOf() in java translate to int.Parse() in C#?)

我正在尝试从C#背景学习java! 我在旅程中发现了一些细微差别,比如C#没有Integer作为引用类型,它只有int作为基本类型; 这让我怀疑这个翻译是否正确!

String line ="Numeric string";//Java

string line = "Numeric string";//C#

int size;

size = Integer.valueOf(line, 16).intValue(); //In Java

size = int.Parse(line,System.Globalization.NumberStyles.Integer);//In C#

I am trying to learn java from a C# background! I have found some nuances during my journey like C# doesn't have an Integer as reference type it only has int as a primitive type; this lead me to a doubt if this translation would be correct!

String line ="Numeric string";//Java

string line = "Numeric string";//C#

int size;

size = Integer.valueOf(line, 16).intValue(); //In Java

size = int.Parse(line,System.Globalization.NumberStyles.Integer);//In C#

原文:https://stackoverflow.com/questions/8047267

更新时间:2019-11-23 01:57

最满意答案

不,这不是一个有效的翻译 - 因为Java代码中的16意味着它应该被解析为整数。 Java代码更接近:

int size = Convert.ToInt32(line, 16);

或者你可以使用:

int size = int.Parse("11", NumberStyles.AllowHexSpecifier);

我不喜欢名称“允许十六进制说明符”,因为它表明将接受“0x”前缀...但实际上它意味着它总是被解释为十六进制。

No, that's not quite a valid translation - because the 16 in the Java code means it should be parsed as an integer. The Java code is closer to:

int size = Convert.ToInt32(line, 16);

That overload of Convert.ToInt32 allows you to specify the base.

Alternatively you could use:

int size = int.Parse("11", NumberStyles.AllowHexSpecifier);

I'm not keen on the name "allow hex specifier" as it suggests that a prefix of "0x" will be accepted... but in reality it means it's always interpreted as hex.

2011-11-08

相关问答

有低编号的Integer对象的缓存实例,但没有任何更高价值的Integer对象。 如果您之前没有注意到,那么您正在比较对象,而不是整数。 There's cached instances of low numbered Integer objects but not any of higher valued Integer objects. If you didn't notice before, you are comparing objects, not ints.

我个人总是使用完整的类名称进行静态方法调用。 这强调了这样一个事实,即它们实际上是包含代码段的类,而不是别名所暗含的最简单可能的(原始)数据。 我总是使用变量的别名。 I personally always use full class names for static method calls. This underlines the fact that they are in fact classes that contain pieces of code instead of simples

...

Java的整数涵盖从-2 ^ 31到2 ^ 31-1(2147483647)的值。 您的值是(4281297749),十进制数对于java的整数来说太大了。 Java的长期覆盖范围从-2 ^ 63到2 ^ 63-1。 其中包含您的值,因此建议使用Long.valueOf(colorStr, 16)并切换为使用long。 (当您使用的值超出整数值范围时,这个建议会发挥作用)。 在我看来,你知道,但如果你不知道, 如果0x属于字符串值的一部分,则应将其删除,因为如果保留其中的值,将导致格式异常无效。

...

实际上, valueOf内部使用parseInt 。 区别在于parseInt返回一个int基元,而valueOf返回一个Integer对象。 来自Integer.class的来源: public static int parseInt(String s) throws NumberFormatException {

return parseInt(s, 10);

}

public static Integer valueOf(String s, int radix) throws Nu

...

这里有一个显着的区别。 valueOf返回一个Integer对象,它的值可以缓存在-128和127之间。这就是为什么第一个值返回true - 它被缓存 - 第二个值返回false - 128不是一个缓存的值,所以你是获取两个独立的Integer实例。 重要的是要注意 ,您正在将引用与Integer#valueOf进行比较,并且如果要比较大于缓存支持的值, 则不会将其求值为true ,即使解析的值相等(例如: Integer.valueOf(128) == Integer.valueOf(128)

...

不,这不是一个有效的翻译 - 因为Java代码中的16意味着它应该被解析为整数。 Java代码更接近: int size = Convert.ToInt32(line, 16);

Convert.ToInt32重载允许您指定基数。 或者你可以使用: int size = int.Parse("11", NumberStyles.AllowHexSpecifier);

我不喜欢名称“允许十六进制说明符”,因为它表明将接受“0x”前缀...但实际上它意味着它总是被解释为十六进制。 No, that

...

不,不应该使用new Integer() 。 如果你用它们的包装对象标识区分整数,那么你不可能做一些合理的事情。 No, there is no scenario where new Integer() should be used. If you are distinguishing integers by their wrapper object identity then you are not likely to be doing something reasonable.

任何明显的方法之间可能没有什么区别:因此可读性(其他答案中发布的LINQ风格方法之一)。 通过将输出列表初始化为所需容量,您可以获得非常大的列表的一些性能,但是您不太可能注意到差异,并且可读性将受到影响: List input = ..

List output = new List(input.Count);

... Parse in a loop ...

轻微的性能提升将来自于输出列表不需要随着其增长而重复重新分配的事实。 There is likely t

...

尝试在某种程度上调试它,并且,鉴于您的更新和您的堆栈跟踪(事实上您在工作线程上),我确信您在某种程度上混淆了您尝试调试多线程问题。 将您的代码更改为: private final AtomicInteger attemptCounter = new AtomicInteger(0);

void whateverYourMethodIsCalled(String responseString) {

int attemptId = attemptCounter.incrementAndGet

...

从文档 : 此方法将始终缓存-128到127(包括端点)范围内的值,并可以缓存此范围之外的其他值。 因此, Integer.valueOf(6)只有一个实例对象,而Integer.valueOf(1000)创建一个新的Integer 。 因此Integer.valueOf(6) == Integer.valueOf(6)和Integer.valueOf(1000) != Integer.valueOf(1000) From the documentation: This method will a

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值