Question: When I try to check whether a string is an IPv4 address or not, I found out that the function gives the following results.

144.122.1 --> true

144.122.1.a -->false

144.122.1.333 -->false

Any idea why the first one is true?

The function is: `sun.net.util.IPAddressUtil.isIPv4LiteralAddress`

Answer: 
You can see why they did it this way from the [source](http://www.docjar.com/html/api/sun/net/util/IPAddressUtil.java.html) itself:

> 
> When a three part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right most two bytes of the network address. This makes the three part address format convenient for specifying Class B net- work addresses as 128.net.host.
> 
> 
> 

So it looks like `144.122.1` equates to `144.122.0.1`.

The full set of comments regarding the number of parts of the textual IP address is extracted below:

> 
> When only one part is given, the value is stored directly in the network address without any byte rearrangement.
> 
> 
> When a two part address is supplied, the last part is interpreted as a 24-bit quantity and placed in the right most three bytes of the network address. This makes the two part address format convenient for specifying Class A network addresses as net.host.
> 
> 
> When a three part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right most two bytes of the network address. This makes the three part address format convenient for specifying Class B net- work addresses as 128.net.host.
> 
> 
> When four parts are specified, each is interpreted as a byte of data and assigned, from left to right, to the four bytes of an IPv4 address.
> 
> 
>
Or use a regex as shown below if you want to check if IP addresses are of the format 0-255.0-255.0-255.0-255

```
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author ragavan
 */
public class MatchIPv4Address {
    public static void main(String[] args) {
        String range = "(([01][0-9][0-9])|(2([0-4][0-9]|5[0-5]))|([0-9]?[0-9]))";
        String pattern = range+"\\."+range+"\\."+range+"\\."+range;
        System.out.println(pattern);
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher("144.229.1.99");
        System.out.println(m.matches());
    }

}

```