>朋友们我很困惑,在编码时我不小心在方法中出现了左大括号和右大括号
List<EmpQualificationLevelTo> fixedTOs = employeeInfoFormNew.getEmployeeInfoTONew().getEmpQualificationFixedTo();
if(fixedTOs != null && !fixedTOs.isEmpty())
{
Iterator<EmpQualificationLevelTo> it = fixedTOs.iterator();
while(it.hasNext())
{
EmpQualificationLevelTo fixedTO = it.next();
FormFile eduDoc = fixedTO.getEducationDoc();
if((eduDoc != null && eduDoc.getFileName() != null && !eduoc.getFileName().isEmpty()) && (fixedTO.getQualification() != null && !fixedTO.getQualification().isEmpty())) {
errors.add("error", new ActionError( "knoledgepro.employee.education.uploadWithoutQualification"));
}
{
}
}
}
你可以在 while-loop 内的 if 块下面看到它。任何人都可以提供帮助,为什么它没有给出任何编译时错误或它是什么?
这不是实例初始值设定项。实例初始值设定项在类或枚举主体中声明,而不是在方法中声明。
这只是一个空块:不必要,但仍然合法。
可以安全地删除空块和空语句:
{
;
;;
//this block compiles successfully
;{}
}
[更新]:从技术上讲,块可用于分隔范围。例如:
{
String test = "test";
//do something with test
}
{
String test = "test2";
//do something with test
}
在这种情况下,具有相同名称的变量将在单独的作用域中声明。
你在这里所做的只是创建一个新范围的块。在方法中,每对{}
定义一个作用域。在一个作用域中定义的变量不能在该作用域之外使用。例如,这个 if 语句:
if (a == b) {
int c = 10;
// here I can access c
}
// but here, I cannot
没有if
或while
或任何其他控制流结构的{}
也是一个范围。它将无条件执行:
System.out.println("Hello");
{
System.out.println("Hello");
} // prints 2 "Hello"s
这些作用域中的变量的行为也相同:
int a = 10;
{
int b = 20;
// can access a and b
}
// can only access a
这有什么用?
我认为这是完全不必要的,我从未在生产代码中使用它。