'URI.getHost() returns null
This prints null:
System.out.println(new URI("http://a.1a/").getHost());
But this prints a.1a:
System.out.println(new URL("http://a.1a/").getHost());
If all URLs are URIs (but not all URIs are URLs) shouldn't a valid URL that has a host component also have the same host component (instead of null) as a URI?
Solution 1:[1]
Look at the Javadoc:
https://docs.oracle.com/javase/8/docs/api/java/net/URI.html:
Returns: The host component of this URI, or null if the host is undefined"
OK, so why is the host part of your particular URI ("http://a.1a/") undefined? Look at the RFC:
https://www.ietf.org/rfc/rfc2396.txt
Hostnames take the form described in Section 3 of [RFC1034] and
Section 2.1 of [RFC1123]: a sequence of domain labels separated by
".", each domain label starting and ending with an alphanumeric
character and possibly also containing "-" characters. The rightmost domain label of a fully qualified domain name will never start > with a digit... To actually be "Uniform" as a resource locator, a URL hostname should be a fully qualified domain name.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | paulsm4 |
