假设我有:
p.size = packetLine[0]; // where packetLine is of type String[] and element at that position is number represented by String
我不想一直写
Integer.parseInt
or reverse
String.valueOf
Eclipse 给出了纠正错误的命题,我可以把它变成建议来转换值吗?
目前它建议更改类型。我想要第三个命题 'Convert to Int' 或 'Convert to String;
这在重复数千次时特别烦人,我可能会介绍我自己的转换方法,如 toInt 或 toString2,但在构建解决方案中会更好。
最佳答案
是的,您无法使用类中的方法(如您建议的 toInt())或静态实用程序方法。 (这很好,如果你不想有很多 try/catch,你可以选择为格式错误的整数返回 0。或者任何合适的。我喜欢 0 而不是异常。)
唯一的“内置”转换是在串联期间将任何内容转换为字符串(使用其 toString() 方法)。喜欢,
String s = "as a string, it is: " + anything; // and null becomes "null".
有时你会看到这个:
String s = "" + something; // shorthand
(数字类型也为您隐式转换。但大多数情况下您应该是类型安全的等等。)
关于java - Eclipse如何自动转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9865277/