Java 如何将整数转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3571352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Integer to int?
提问by
I am working on a web application in which data will be transfer between client & server side.
我正在开发一个 Web 应用程序,其中数据将在客户端和服务器端之间传输。
I already know that JavaScript int != Java int. Because, Java int cannot be null, right. Now this is the problem I am facing.
我已经知道 JavaScript int != Java int。因为,Java int 不能为空,对吧。现在这就是我面临的问题。
I changed my Java int variables into Integer.
我将 Java int 变量更改为 Integer。
public void aouEmployee(Employee employee) throws SQLException, ClassNotFoundException
{
Integer tempID = employee.getId();
String tname = employee.getName();
Integer tage = employee.getAge();
String tdept = employee.getDept();
PreparedStatement pstmt;
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/general";
java.sql.Connection con = DriverManager.getConnection(url,"root", "1234");
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
pstmt = (PreparedStatement) con.prepareStatement("REPLACE INTO PERSON SET ID=?, NAME=?, AGE=?, DEPT=?");
pstmt.setInt(1, tempID);
pstmt.setString(2, tname);
pstmt.setInt(3, tage);
pstmt.setString(4, tdept);
pstmt.executeUpdate();
}
My problem is here:
我的问题在这里:
pstmt.setInt(1, tempID);
pstmt.setInt(3, tage);
I cant use the Integer variables here. I tried with intgerObject.intValue();
But it makes things more complex. Do we have any other conversion methods or conversion techniques?
我不能在这里使用整数变量。我试过intgerObject.intValue();
但它使事情变得更加复杂。我们还有其他转换方法或转换技术吗?
Any fix would be better.
任何修复都会更好。
采纳答案by user85421
As already written elsewhere:
正如在别处已经写过的:
- For Java 1.5 and later you don't need to do (almost) anything, it's done by the compiler.
- For Java 1.4 and before, use
Integer.intValue()
to convert from Integer to int.
- 对于 Java 1.5 及更高版本,您(几乎)不需要做任何事情,它由编译器完成。
- 对于 Java 1.4 及更早版本,使用
Integer.intValue()
将 Integer 转换为 int。
BUT as you wrote, an Integer
can be null, so it's wise to check that before trying to convert to int
(or risk getting a NullPointerException
).
但是正如您所写, anInteger
可以为空,因此在尝试转换为之前检查它是明智的int
(或冒着获得 a 的风险NullPointerException
)。
pstmt.setInt(1, (tempID != null ? tempID : 0)); // Java 1.5 or later
or
或者
pstmt.setInt(1, (tempID != null ? tempID.intValue() : 0)); // any version, no autoboxing
*using a default of zero, could also do nothing, show a warning or ...
*使用默认值零,也可以什么都不做,显示警告或...
I mostly prefer not using autoboxing (second sample line) so it's clear what I want to do.
我最喜欢不使用自动装箱(第二个示例行),所以很清楚我想要做什么。
回答by ColinD
Since you say you're using Java 5, you can use setInt
with an Integer
due to autounboxing: pstmt.setInt(1, tempID)
should work just fine. In earlier versions of Java, you would have had to call .intValue()
yourself.
由于您说您使用的是 Java 5,因此您可以使用setInt
with a Integer
due to autounboxing:pstmt.setInt(1, tempID)
应该可以正常工作。在 Java 的早期版本中,您必须调用.intValue()
自己。
The opposite works as well... assigning an int
to an Integer
will automatically cause the int
to be autoboxed using Integer.valueOf(int)
.
反之亦然……将 an 分配int
给 anInteger
将自动导致int
使用自动装箱Integer.valueOf(int)
。
回答by spbfox
Java converts Integer to int and back automatically (unless you are still with Java 1.4).
Java 会自动将 Integer 转换为 int 并返回(除非您仍然使用 Java 1.4)。
回答by Jim Tough
Perhaps you have the compiler settings for your IDE set to Java 1.4 mode even if you are using a Java 5 JDK? Otherwise I agree with the other people who already mentioned autoboxing/unboxing.
即使您使用的是 Java 5 JDK,也许您的 IDE 的编译器设置也设置为 Java 1.4 模式?否则我同意已经提到自动装箱/拆箱的其他人。
回答by Parth mehta
Another simple way would be:
另一种简单的方法是:
Integer i = new Integer("10");
if (i != null)
int ip = Integer.parseInt(i.toString());