class Test
{
public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0)
String s1 = s.toUpperCase();//这样提示错误:Test.java:9: 不是语句
// String s1 = s.toUpperCase();
// ^
//Test.java:9: 需要 ';'
//String s1 = s.toUpperCase();
^
}
}但是下面这样写没有错误class Test
{
public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0){
String s1 = s.toUpperCase();
} }
}有人说String s1 = s.toUpperCase();相当于2条语句String s1 = null ; s1 = s.toUpperCase();如果不加{}就会找不到s1.
这相当于下面哪种写法呢(如下),这两种写法又真的有区别吗?迷惑了。if(1>0) {
String s1 = null ;
}
s1 = s.toUpperCase();
if(1>0)
String s1 = null ;
s1 = s.toUpperCase();
如:if(1>0) {
String s1 = null ;
}这个if语句只有一个单句,可以省略{},就成为了
if(1>0)
String s1 = null ;因此这两个是没有区别的。
即你最后问的那个问题那两种写法是没区别的。
--------------------------------------------------------------------------------------
而有时候编译器会把你写的一句复杂的代码解释为多句简单的代码。因此,此时又要使用{}
如下: public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0){
String s1 = s.toUpperCase();
}
} 如果编译器把String s1=s.toUpperCase();解释为:String s1=null;s1=s.toUpperCase();的话,这段代码就可以写成: public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0){
String s1 =null;
s1=s.toUpperCase();
}
} 而代码: public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0)
String s1 =null;
s1=s.toUpperCase();
}
<==>
public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0){
String s1 =null;
}
s1=s.toUpperCase();
}
这个s1在出了if语句的作用域后就不可以使用了。
因此上边这个代码会报s1未定义.....
如:if(1>0) {
String s1 = null ;
}这个if语句只有一个单句,可以省略{},就成为了
if(1>0)
String s1 = null ;因此这两个是没有区别的。
即你最后问的那个问题那两种写法是没区别的。
--------------------------------------------------------------------------------------
而有时候编译器会把你写的一句复杂的代码解释为多句简单的代码。因此,此时又要使用{}
如下: public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0){
String s1 = s.toUpperCase();
}
} 如果编译器把String s1=s.toUpperCase();解释为:String s1=null;s1=s.toUpperCase();的话,这段代码就可以写成: public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0){
String s1 =null;
s1=s.toUpperCase();
}
} 而代码: public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0)
String s1 =null;
s1=s.toUpperCase();
}
<==>
public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0){
String s1 =null;
}
s1=s.toUpperCase();
}
这个s1在出了if语句的作用域后就不可以使用了。
因此上边这个代码会报s1未定义.....
情况一:报错是 Test.java:9: 不是语句
String s1 = s.toUpperCase();
^
Test.java:9: 需要 ';'
String s1 = s.toUpperCase();class Test
{
public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0)
String s1 = s.toUpperCase();
//{
//String s1 = null ;
//}
s1 = s.toUpperCase();
}
}情况二:报错是 Test.java:11: 找不到符号
符号: 变量 s1
位置: 类 Test
System.out.println(s1);
情况二的报错我知道,不需要说明class Test
{
public static void main(String[] args)
{
String s = "hsfdsfds" ;
if(1>0)
//String s1 = s.toUpperCase();
{
String s1 = null ;
}
s1 = s.toUpperCase();
}
}也就是说:
if(1>0)
String s1 = s.toUpperCase();
跟
if(1>0)
String s1 = null ;
s1 = s.toUpperCase();
根本不等价那又该怎么解释呢?
// String s1 = s.toUpperCase();
// ^
//Test.java:9: 需要 ';'
//String s1 = s.toUpperCase();非常明显是你的分号写错了(英文的分号你写成了中文的分号)
呵呵
我测试过了
我的结论是如果if语句后面不加{}的话,那么 if语句后不能直接跟定义一个变量的语句
可以是非定义变量语句,
所以即使你这样写:
if(1>2)
String s=null;
也会报错
至于为什么这么规定?
我还没仔细研究 ...
也许就是这样规定的吧
只是以前都没怎么注意而已...
如果谁知道详细的答案,还希望详细解释一番....