java.lang.Void and void and the use of comparative methods described _java - JAVA

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

void is not a function, a method of modifiers, void means that this method does not return value, meaning that the method will run method statements, but does not return anything.

java.lang.Void is a type. For example Void references to assign null.

Void nil = null;

Void can be seen by the class code, and not inherit Void type instantiation.

public final
class Void {
  /**
   * The {@code Class} object representing the pseudo-type corresponding to
   * the keyword {@code void}.
   */
  @SuppressWarnings("unchecked")
  public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");
  /*
   * The Void class cannot be instantiated.
   */
  private Void() {}
}

Void function and returns the result represents the function returns null (in addition to other types of returns can not null).
 

Void function(int a, int b) {
  //do something
  return null;
 }

Before generics, Void generally used in reflection. For example, the following code printing a void return type of the method name.

public class Test {
  public void print(String v) {}
  public static void main(String args[]){
    for(Method method : Test.class.getMethods()) {
      if(method.getReturnType().equals(Void.TYPE)) {
        System.out.println(method.getName());
      }
    }
  }
}

After generics, some scenes will be used Void type. E.g. Future <T> to save the results. Future's get method will return results (of type T).

But if the operation does not return value it? In this case can be represented by the Future <Void>. When the result of the calculation is returned after completion of call to get returns null.
Further Void Map No value is also used, for example Map <T, Void> map so having the Set <T> has the same function.

So when you use a generic function does not need to return a result or value of an object does not need to be used when it is java.lang.Void type representation.

reference:

Compare java.lava.Void and void, and

http://stackoverflow.com/questions/10839042/what-is-the-difference-between-java-lang-void-and-void

How to determine the function to return void,

http://stackoverflow.com/questions/1924253/how-to-determine-by-reflection-if-a-method-returns-void

to sum up

That's all for this article on java.lang.Void and void and the use of comparative methods described, if you want to learn more about java.lang.Void, where there is a "java.lang.Void source category analysis" is very appropriate, I hope for your help. Interested friends can see: to use Java AtomicInteger Detailed category, marking and labeling process Discussion on secondary objects such as Java objects recovered, what problems can leave a message at any time, to share the discussion.

The original address is: http: //www.piaodoo.com/thread-13251-1-1.html stockings control www.txdah.com 131 outside www.buzc.org enjoyable learning can help to learn better!

Guess you like

Origin www.cnblogs.com/txdah/p/12093806.html