Java: URL or String?
Is there any merit (coding style, OOP best practice, etc.) in working with an instance of java.net.URL
as opposed to java.lang.String
when I can be reasonably certain that a URL is valid anyway开发者_Go百科 (perhaps I've specified it statically in a properties file) and I will just be working with it in its string form anyway (for example printing it in a JSP file)?
java.net.URL
's equals is broken. You should use java.net.URI
. You should pass objects around whenever possible rather than Strings. It saves programmer errors, for example getting method parameters the wrong way round - the compiler won't pick it up if they're both Strings.
You will get many method support in case or URL. but if its just to display then go for String you can always build URL from valid String
Well the URL class has lots of methods that let you manipulate the url, for example get the host, the path and a dozen other things. You can also open a connection to the URL and fetch the document if you need.
精彩评论