for循环中的条件保持空白,代码编译并运行.
for(int i=0; ; i++)
System.out.print(i); //this code does not execute
//code after this does not execute
Run Code Online (Sandbox Code Playgroud)
但是,我不明白这是如何以及为什么这是可能的.
我想创建一个包含一些文本的简单文本文件.
import java.io.*;
class TextFileWriter{
public static void writeTextFile(String fileName, String s) {
FileWriter output = null;
try {
output = new FileWriter(fileName);
BufferedWriter writer = new BufferedWriter(output);
writer.write(s);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
}
}
}
}
public static void main(String args[]){
writeTextFile("myText.txt","some text");
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我成功创建了文本文件,但是当我打开它时,我看不到内容("一些文本").我究竟做错了什么?
我不明白为什么这个方法正确编译.
public int doIt() throws Exception{
//code that does not include a return int statement
throw new Exception("something");
}
Run Code Online (Sandbox Code Playgroud)
我们声明了一个int的返回类型,为什么我没有收到类似的警告:"此方法必须返回int类型的结果"?