In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use the java keyword". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
We often use void when writing code, and we all know that it represents the method that does not return anything, but this is only superficial, and we will often ask during the interview that it is necessary to conduct an in-depth analysis of it.
1. What is the type of void keyword?
Unlike weakly typed languages such as php, java is strongly typed, which means that our method must have a return value that determines the type, for example.
Public String test () {}
The above test method has a return value of type String, but we can also return a value of the underlying type such as int. No matter what is returned, a certain type is returned.
Now! There is a problem, our method can also return void, so void must also be a data type. But java seems to provide only two data types: the basic data type and the reference data type. So what on earth is this void? In fact, you can understand it as a special data type or as a method modifier.
2. Look at void from Void
It seems that all of our basic types have a wrapper class, for example, the wrapper class of the int basic type is Integer,char, and the wrapper class of the Integer,char basic type is Character,void, and it also has a wrapper class called Void, which is right to replace the "v" with an uppercase V. You can understand Void like this:
In fact, the Void class is a placeholder class that cannot be instantiated to hold a Class object that references the Java keyword void.
Void types cannot be inherited and instantiated. And null must be returned when decorating the method.
Next, let's study this Void.
(1) determine the type: Void is a class and void is a basic type.
Public class Test {
Public static void main (String [] args) {
System.out.println (Void.class)
System.out.println (void.class)
}
}
/ / output
/ / class java.lang.Void
/ / void
(2) basic use: must and can only return null
Public class Test {
/ / return void,return is optional
Public void A1 () {
Return
}
/ / must and can only return null
Public Void a2 () {
Return null
}
}
(3) use scenario: determine the return type of a function in reflection
Public class Test {
/ / define two methods here:
/ / (1) A method returns void
/ / (2) b method returns int
Public void a () {}
Public int b () {
Return 1
}
Public static void main (String args []) {
For (Method method: Test.class.getMethods ()) {
If (method.getReturnType () .equals (Void.TYPE)) {
System.out.println ("the method to return void is:" + method.getName ())
}
Else if (method.getReturnType () .equals (Integer.TYPE)) {
System.out.println ("the method to return int is:" + method.getName ())
}
}
}
}
/ / output
/ / the method to return void is: main
/ / the method to return int is: B
/ / the method to return void is: a
(4) usage scenarios: used in generics
Future is used to save the results. The get method of Future returns the result (type T). But what if the operation does not return a value? In this case, it can be expressed as Future. When the result is calculated and returned after calling get, null will be returned.
Void is also used in worthless Map. Map such as Map will have the same function as Set.
This is the end of the content of "how to use the java keyword". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.