URI/URL 和String 有什么区别? [英] URI/URL and String what is the difference?

查看:48
本文介绍了URI/URL 和String 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是出于好奇,不需要代码,我尝试使用世界上最好的搜索引擎来获得我的答案,但没有发现任何有价值的东西.

This is out of curiosity, no code required, I tried using the worlds best search engine to get my answers but figured out nothing worthwhile.

URI/URL 和表示该 URI/URL 的字符串有什么区别?

Whats the difference between a URI/URL and a string representing that URI/URL?

我们为什么要解析它?

将字符串解析为 URI/URL 对字符串有什么作用,以至于它被区别对待?

What does parsing a string into URI/URL do to the string such that it is treated differently?

为什么 HTTP 支持将 URL 输入为字符串?

Why does HTTP get support typing URL as a string?

推荐答案

URI/URL 受到限制.要有效,它必须遵循某种格式.

URI/URL is constrained. To be valid it has to follow a certain format.

说他们只接受一个 URI/URL 方法是说他们接受的字符串必须是那种格式.这也让一目了然"更清楚方法期望什么(URL),而不是潜在的任何字符串.

By saying that they only accept a URI/URL methods are saying that the string they accept has to be in that format. This also makes it clearer "at a glance" what the method is expecting (a URL) rather than potentially any String.

如果字符串不是那种格式,那么当您尝试创建 URL 对象时会立即检测到它,而不是在将来尝试在方法中使用该 URL 时的某个未知点.

If the String isn't in that format then it is detected immediately when you try and create the URL object rather than at some unknown point in the future when you try and use that URL in a method.

因此,这样做可以提高类型安全性,使代码更具可读性,并使故障发生在更接近故障根本原因的位置,而不是在未来某个未知点发生.

So by doing this it increases type safety, makes code more readable, and causes failures to happen closer to the root cause of the failure rather than at some unknown point in the future.

您还可以获得从其部分正确构造 URL、使用 URL 打开资源等的方法.因此,它不仅仅是一个字符串.看一看Javadoc,看看它提供了多少方法:

You also get methods to construct the URL properly from its parts, to open resources using the URL, etc. So it's a lot more than just a String. Take a look at the Javadoc to see how many methods it provides:

http://docs.oracle.com/javase/7/docs/api/java/net/URL.html

这篇关于URI/URL 和String 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆