A uri
could be created in two ways:
URI uri = new URI("https://www.google.com/");
Or,
URI uri = URI.create("https://www.google.com/");
I was wondering which is a better practice. I haven't noticed any performance differences and I've read the documentation, however it was a bit difficult to understand. Any insight on this is appreciated.
Reading the docs, it differs in the usage.
Creates a URI by parsing the given string.This convenience factory
method works as if by invoking the {@link
URI(String)} constructor; any {@link URISyntaxException} thrown by the constructor is caught and wrapped in a new {@link
IllegalArgumentException} object, which is then thrown.
This method is provided for use in situations where it is known that
the given string is a legal URI, for example for URI constants
declared within in a program, and so it would be considered a
programming error for the string not to parse as such. The
constructors, which throw {@link URISyntaxException} directly, should
be used situations where a URI is being constructed from user input or
from some other source that may be prone to errors.
@param str The string to be parsed into a URI
* @return The new URI
*
* @throws NullPointerException
* If {@code str} is {@code null}
*
* @throws IllegalArgumentException
* If the given string violates RFC 2396
*/
public static URI create(String str) {
try {
return new URI(str);
} catch (URISyntaxException x) {
throw new IllegalArgumentException(x.getMessage(), x);
}
}