java “方法的结果被忽略” - 这意味着什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38967366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Result of method is ignored"- what does this imply?
提问by suku
For some methods I get the warning. This is what it says when expanded.
The following code (mkDirs()
) gives the warning
对于某些方法,我收到警告。这是它在扩展时所说的。下面的代码 ( mkDirs()
) 给出了警告
if (!myDir.exists()) {
myDir.mkdirs();
}
Reports any calls to specific methods where the result of that call is ignored. Both methods specified in the inspection's settings and methods annotated with org.jetbrains.annotations.Contract(pure=true) are checked. For many methods, ignoring the result is perfectly legitimate, but for some methods it is almost certainly an error. Examples of methods where ignoring the result of a call is likely to be an error include java.io.inputStream.read(), which returns the number of bytes actually read, any method on java.lang.String or java.math.BigInteger, as all of those methods are side-effect free and thus pointless if ignored.
报告对忽略该调用结果的特定方法的任何调用。检查设置中指定的方法和用 org.jetbrains.annotations.Contract(pure=true) 注释的方法都被检查。对于许多方法,忽略结果是完全合法的,但对于某些方法,它几乎肯定是错误的。忽略调用结果可能是错误的方法示例包括 java.io.inputStream.read(),它返回实际读取的字节数,java.lang.String 或 java.math.BigInteger 上的任何方法,因为所有这些方法都没有副作用,因此如果被忽略就毫无意义。
What does it mean? How to to avoid it? How should it be addressed?
这是什么意思?如何避免呢?应该如何解决?
回答by Riyaz Parasara
that the (compilation) warning message really says this:
(编译)警告消息确实是这样说的:
Result of File.mkdir() is ignored
File.mkdir() 的结果被忽略
something like that. It is telling you that you are ignoring the result of the mkdir() call that tells you whether or not a directory was created.
类似的东西。它告诉您您忽略了 mkdir() 调用的结果,该调用告诉您是否创建了目录。
One way to avoid the warning would be to test the result and act appropriately. Another would be to simply assign the result to a temporary variable, ignore it, and (potentially) crash later because the directory wasn't created when it should have been.
避免警告的一种方法是测试结果并采取适当的行动。另一种方法是简单地将结果分配给一个临时变量,忽略它,然后(可能)崩溃,因为目录没有在应该创建的时候创建。
Feel free to modify the code if there is any other mistake.
如果有任何其他错误,请随时修改代码。
Since you asked ... it is BAD STYLE to use Hungarian notation for Java variable names. Java is a strongly typed language where all variables have a clear declared types. You should not need the mental crutches of some ghastly identifier convention to tell you what a variable's type is intended to be.
既然你问了......对Java变量名使用匈牙利表示法是不好的风格。Java 是一种强类型语言,其中所有变量都有明确的声明类型。您不应该需要一些可怕的标识符约定的心理拐杖来告诉您变量的类型是什么。
you can handled in these ways
你可以通过这些方式处理
1)
1)
boolean isDirectoryCreated= path.mkdirs();
and ignore 'isDirectoryCreated'
并忽略“isDirectoryCreated”
2) (Recommended)
2) (推荐)
boolean isDirectoryCreated= path.exists();
if (!isDirectoryCreated) {
isDirectoryCreated= path.mkdirs();
}
if(isDirectoryCreated) {
// do something
}
回答by Jay Shah
This method suggests that you are creating directory with your specified path if directory is not already created then this function will create a new one for you.It is returning true/false weather directory exists or created.Perhaps in some situation due to low storage it not created at specified path and you are trying to write contents into file within that directory it will throw ioException.So you should utilise if condition
此方法建议您使用指定的路径创建目录,如果目录尚未创建,则此功能将为您创建一个新目录。它返回真/假天气目录存在或创建。也许在某些情况下由于存储空间不足未在指定路径创建,并且您尝试将内容写入该目录中的文件,它将抛出 ioException。因此您应该利用 if 条件
if(myDir.mkdirs())
{
//execute what ever you want to do
}
else
{
// show error message for any failure.
}