Kousha
Kousha

Reputation: 36219

Java URL constructor allows spaces

I'm confused with why this is valid and not throwing an exception:

final URL u = new URL("https:// google . c o m foo bar")

Isn't this an invalid URL?

Upvotes: 2

Views: 292

Answers (1)

VGR
VGR

Reputation: 44338

Yes. java.net.URL is a very old class which was part of Java 1.0. It does little to no checking of the syntax of its String argument.

Later, the java.net.URI class was added, which does all of the proper checks. And a valid URI can be converted to a URL with the toURL() method.

Upvotes: 1

Related Questions