简体   繁体   English

编译器不会抱怨返回类型。 为什么?

[英]Compiler doesn't complain about return type. Why?

I wrote the following java code, and i expected the compiler would complain about it. 我编写了以下Java代码,并希望编译器对此有所抱怨。 but i didn't get any errors. 但我没有得到任何错误。 Why is this ? 为什么是这样 ?

    public static void main(String[] args) {
        Ba ba = new Ba();

        ba.fetchSomeValues();

    }

    public String fetchSomeValues(){
        return "Hello";     
    }

}

I am calling the method fetchSomeValues() which should return "Hello" (which is a string), and in the main method i have included ba.fetchSomeValues(); 我正在调用方法fetchSomeValues() ,该方法应返回“ Hello”(这是一个字符串),并且在主要方法中,我包括了ba.fetchSomeValues(); without initializing it to a String variable. 而不将其初始化为String变量。 The compiler doesn't complain Why is this ? 编译器没有抱怨这是为什么?

You don't have to assign return values to variables. 您不必为变量分配返回值。 You don't have to do anything at all with them. 无需对它们做任何事情。

Although it is usually not recommended to just drop some return value of a method. 尽管通常不建议仅丢弃方法的某些返回值。

A counter example might be the Map 's put method which returns the previous value associated with the key. 一个计数器示例可能是Mapput方法,该方法返回与键关联的先前值。 If you don't care whether there was a previous value or not you just simply ignore the return value. 如果您不在乎是否有先前的值,则只需忽略返回值即可。

Map<Integer, String> map = new HashMap<Integer, String>();
test.put(1, "one"); // we don't assign the return value since we don't care

So in a nutshell the compiler cannot tell whether you care about return values or not. 简而言之,编译器无法判断您是否关心返回值。 It is only a problem if the value is significant in the context you are using the method and you ignore it . 如果该值在您使用该方法的上下文中有意义,而您忽略它,则这只是一个问题

忽略方法的返回值绝对有效(尽管不总是建议)。

在Java中,您可以忽略刚发现的返回值。

This behavior is not specific to Java, it is in fact the norm for all the mainstream (and even those less so) languages of today. 这种行为不是Java特有的,事实上,这是当今所有主流(甚至更少的)语言的规范。 Even in FP-languages, where the focus is on side-effect-free functions whose only point is the return value, this is allowed. 即使在FP语言中,重点是无副作用的函数,其唯一点就是返回值,这也是允许的。

You should really ask yourself, Do I want to use a language that forces me to assign every return value? 您应该真正问自己:我是否要使用一种强制我分配每个返回值的语言? Would that be a convenient language to use? 那是一种方便使用的语言吗?

ba.fetchSomeValues(); does return the string "Hello" , but since you ain't got any left var (for example String s = ba.fetchSomeValues(); ), the object of string, that has the "Hello" value, created in the fetchSomeValues() method, just get unused . 不返回字符串 "Hello" ,但因为你是不会得到任何留给VAR(例如String s = ba.fetchSomeValues(); ),字符串的对象,具有"Hello"值,在创建fetchSomeValues()方法, 不用了

There's nothing wrong in this code. 这段代码没有错。 fetchSomeValues() returns a string but you don't have to assign the value. fetchSomeValues()返回一个字符串,但您不必分配该值。 Normally you can write String returnedValue = a.fetchSomeValues() but it is not necessary 通常您可以编写String returnValue = a.fetchSomeValues(),但这不是必需的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM