Search code examples
javanetwork-programminginetaddress

InetAddress.getAddress() always returns a null, but somehow still works


I have a String IP address that I need to convert to a byte array. For this, I used InetAddress.getByName(ip).getAddress(), and it all works great.

However, when I looked at the code of InetAddress.getAddress(), it looks like this:

public byte[] getAddress() {
    return null;
}

There is absolutely no operation being done here - however, I am still getting a byte array back, with the corerect values too. How is this working?


Solution

  • The method you use to get the address, InetAddress.getByName returns a subclass: either an Inet4Address or an Inet6Address. These 2 subclasses have the getAddress method implemented to return something useful.