Java Base API中最令人误解的方法是什么? [英] What is the most misleading method in the Java Base API?

查看:51
本文介绍了Java Base API中最令人误解的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,当方法

I was recently trying to convert a string literal into a boolean, when the method boolean Boolean.getBoolean(String name) popped out of the auto-complete window. There was also another method (boolean Boolean.parseBoolean(String s)) appearing right after, which led me to search to find out what were the differences between these two, as they both seemed to do the same.

事实证明,Boolean.getBoolean(String name)的真正作用是检查是否存在具有给定名称的System属性(!),并且其值是否为true .我认为这很容易引起误解,因为我绝对不希望Boolean方法实际上在调用

It turns out that what Boolean.getBoolean(String name) really does is to check if there exists a System property (!) of the given name and if its value is true. I think this is very misleading, as I'm definitely not expecting that a method of Boolean is actually making a call to System.getProperty, and just by looking at the method signature, it sure looks (at least to me) like it should be used to parse a String as a boolean. Sure, the javadoc states it clearly, but I still think the method has a misleading name and it is not in the right place. Other primitive type wrappers, such as Integer also have a similar method.

此外,归属于基本API似乎并不是一种非常有用的方法,因为我认为拥有-Darg=true之类的东西并不常见.对于Java职位面试来说,也许是一个好问题:"Boolean.getBoolean("true")的输出是什么?".我相信对于这些方法而言,更合适的地方应该是 System 类,例如getPropertyAsBoolean;但是再次,我仍然认为没有必要在基本API中使用这些方法.将它们放在类似Properties类的位置是很有意义的,在这种情况下,进行这种类型的转换是很常见的.

Also, it doesn't seem to be a very useful method to belong in the base API, as I think it's not very common to have something like -Darg=true. Maybe it's a good question for a Java position interview: "What is the output of Boolean.getBoolean("true")?". I believe a more appropriate place for those methods would be in the System class, e.g., getPropertyAsBoolean; but again, I still think it's unnecessary to have these methods in the base API. It'd make sense to have them in something like the Properties class, where it's very common to do this type of conversions.

您怎么看待这一切?另外,如果您知道还有另一种尴尬"的方法,请发布.

What do you think of all this? Also, if there's another "awkward" method that you're aware of, please post it.

NB 我知道我可以使用Boolean.valueOfBoolean.parseBoolean将字符串文字转换为boolean,但是我只是在讨论API设计.

N.B. I know I can use Boolean.valueOf or Boolean.parseBoolean to convert a string literal into a boolean, but I'm just looking to discuss the API design.

推荐答案

URL equals()方法比较IP地址,使用网络连接并且是阻止操作!

The URL equals() method compares IP addresses, uses a network connection and is a blocking operation!

来自javadocs:

From the javadocs:

如果两个主机名都可以是两个主机名,则认为这两个主机是等效的 解决 进入相同的IP地址;否则,如果任何一个主机名都不能 解析后,主机名必须相同,且不区分大小写;或两者 主机名等于空.

Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can't be resolved, the host names must be equal without regard to case; or both host names equal to null.

由于主机比较需要名称解析,因此此操作是 阻止操作.

Since hosts comparison requires name resolution, this operation is a blocking operation.

注意:已知的等于行为定义为 与HTTP中的虚拟主机不一致.

Note: The defined behavior for equals is known to be inconsistent with virtual hosting in HTTP.

改为使用URI.

这篇关于Java Base API中最令人误解的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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