在java中将字符串转换为整数的最有效方法 [英] Most efficient way of converting String to Integer in java

查看:31
本文介绍了在java中将字符串转换为整数的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多方法可以将 String 转换为 Integer 对象.以下哪个最有效:

There are many ways of converting a String to an Integer object. Which is the most efficient among the below:

Integer.valueOf()
Integer.parseInt()
org.apache.commons.beanutils.converters.IntegerConverter

我的用例需要创建包装 Integer 对象...意味着没有原始 int...并且转换后的数据用于只读.

My usecase needs to create wrapper Integer objects...meaning no primitive int...and the converted data is used for read-only.

推荐答案

不要浪费时间思考它.只需选择一个似乎适合其余代码的代码(其他转换是否使用 .parse__() 或 .valueOf() 方法?使用它来保持一致).

Don't even waste time thinking about it. Just pick one that seems to fit with the rest of the code (do other conversions use the .parse__() or .valueOf() method? use that to be consistent).

试图决定哪个是最好的"会分散您解决业务问题或实现功能的注意力.

Trying to decide which is "best" detracts your focus from solving the business problem or implementing the feature.

不要因为琐碎的细节而陷入困境.:-)

Don't bog yourself down with trivial details. :-)

附带说明一下,如果您的用例"正在为您的代码指定 java 对象数据类型 - 您的 BA 需要退出您的域.BA 需要定义业务问题",以及用户在解决问题时希望如何与应用程序交互.开发人员确定如何最好地使用代码将该功能构建到应用程序中 - 包括处理数据的正确数据类型/对象.

On a side note, if your "use case" is specifying java object data types for your code - your BA needs to step back out of your domain. BA's need to define "the business problem", and how the user would like to interact with the application when addressing the problem. Developers determine how best to build that feature into the application with code - including the proper data types / objects to handle the data.

这篇关于在java中将字符串转换为整数的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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