看到一段程序代码这样
String userInputIp;
JTextField inputIp;

userInputIp =""+InetAddress.getByName(inputIp.getText());//注两个分号连在一起没有分开
userInputIp =userInputIp.substring(1);

我改成
userInputIp =InetAddress.getByName(inputIp.getText());
userInputIp =userInputIp.substring(0);
编译器说:
不兼容的类型 找到java.net.InetAddress 需要java.lang.String
指向userInputIp =InetAddress.getByName(inputIp.getText());
我想问 "" 在这里到底什么意思啊??为什么要这样写呀??
谢谢先~~~~~

解决方案 »

  1.   

    InetAddress.getByName(inputIp.getText()) returns an IntAddress instance, which cannot be assigned to a String variable directly.We can simply use InetAddress.toString() method to do this assignment.
    userInputIp = InetAddress.getByName(inputIp.getText()).toString();Alternatively, we can use "" + InetAddress.getByName(inputIp.getText()).
    "" is a String, which contains 0 characters. It is also called "empty String".
    When we concatenate an empty string and another object, this object's toString() is invoked automatically.