如何根据使用方法正确读取java文档(how to read appropriately on java document in terms of using methods)

编程入门 行业动态 更新时间:2024-10-25 16:19:28
如何根据使用方法正确读取java文档(how to read appropriately on java document in terms of using methods)

当我看着java文档来看看如何使用方法。 https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString()http://docs.oracle.com/javase/7/docs/api/java/lang /String.html#toString()我发现两个混淆我的方法,第一个是

static String valueOf(char[] data)返回char数组参数的字符串表示形式。

这是我在java文档中找到的信息,但是我找不到有关如何使用它的任何信息,以及如何知道我需要使用String.valueOf()而不是基于java文档的something.valueof() ?

第二种方法是

String toString()这个对象(它已经是一个字符串!)本身被返回。

我怎么知道什么时候根据java文档提供的信息来使用Integer.toString()或something.toString() 。

谁能告诉我如何提取这些信息? 这个问题困扰了我很长一段时间。 节日快乐^ _ ^

when I look at the java documents to see how to use methods. https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString() http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toString() I find two methods that confuse me, first one is

static String valueOf(char[] data) Returns the string representation of the char array argument.

that's the information I find on java docs, but I cannot find any information on how to use it, and how do I know I need to use String.valueOf() instead of something.valueof() based on java docs?

second method is

String toString() This object (which is already a string!) is itself returned.

How do I know when it is to use Integer.toString() or something.toString() based on the information provided by java docs.

Could anyone tell me how to extract those information? This issue has bothered me for a long time. Happy holidays^_^

最满意答案

您需要在类中有一个方法,然后才能调用它,无论是通过使用该类的实例还是在静态的情况下使用类本身。 在这里, valueOf()只能在String类中找到,它是静态的,所以你只能使用String.valueOf()来调用它。 但是这不是toString()的情况,因为它不是静态的,可以通过类似something.toString()的实例来调用。

基本上他们似乎执行相同的功能,但valueOf()被视为特殊的一点。 这样它可以取空值,但toString()会在使用null值的变量时抛出NullPointerException 。

You need to have a method in a class before you can call it whether by using an instance of that class or using the class itself if it is static. In the case here, valueOf() is only found in the String class and it is static so you can only call it using String.valueOf(). But that is not the case of toString() because it is not static and can be called through an instance like something.toString().

Basically they seem to perform the same function but valueOf() is treated special a bit. Such that it can take null values but toString() will throw a NullPointerException if variable of null value is used.

更多推荐

本文发布于:2023-08-08 00:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用方法   正确   文档   java   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!