What does void do in java?

霸气de小男生 提交于 2019-11-26 09:58:59

问题


The return type—the data type of the value returned by the method, or void if the method does not return a value.

http://download.oracle.com/javase/tutorial/java/javaOO/methods.html

Okay, then.. Here is my question:

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY: System.out.println(\"Mondays are bad.\");
                         break;

            case FRIDAY: System.out.println(\"Fridays are better.\");
                         break;

            case SATURDAY:
            case SUNDAY: System.out.println(\"Weekends are best.\");
                         break;

            default:     System.out.println(\"Midweek days are so-so.\");
                         break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();


    }
}

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

The above code does not work without void.

Exception in thread \"main\" java.lang.Error: Unresolved compilation problems: 
    The method tellItLikeItIs() is undefined for the type EnumTest

What did I miss out? Why is there a void in there? And it does return a string?


回答1:


The reason the code will not work without void is because the System.out.println(String string) method returns nothing and just prints the supplied arguments to the standard out terminal, which is the computer monitor in most cases. When a method returns "nothing" you have to specify that by putting the void keyword in its signature.

You can see the documentation of the System.out.println here:

http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#println%28java.lang.String%29

To press the issue further, println is a classic example of a method which is performing computation as a "side effect."




回答2:


You mean the tellItLikeItIs method? Yes, you have to specify void to specify that the method doesn't return anything. All methods have to have a return type specified, even if it's void.

It certainly doesn't return a string - look, there are no return statements anywhere. It's not really clear why you think it is returning a string. It's printing strings to the console, but that's not the same thing as returning one from the method.




回答3:


Void doesn't return anything; it tells the compiler the method doesn't have a return value.




回答4:


void means it returns nothing. It does not return a string, you write a string to System.out but you're not returning one.

You must specify what a method returns, even if you're just saying that it returns nothing.

Technically speaking they could have designed the language such that if you don't write a return type then it's assumed to return nothing, however making you explicitly write out void helps to ensure that the lack of a returned value is intentional and not accidental.




回答5:


When the return type is void, your method doesn't return anything.

Look again at your code: There's no return in that method. You print to the console and exit.




回答6:


Void: the type modifier void states that the main method does not return any value. All parameters to a method are declared inside a prior of parenthesis. Here String args[ ] declares a parameter named args which contains an array of objects of the class type string.



来源:https://stackoverflow.com/questions/7367381/what-does-void-do-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!