java短路现象程序解读

来源:百度知道 编辑:UC知道 时间:2024/07/07 10:25:11
public class ShortCut
{
public static boolean test(int a)
{
System.out.println("come in and a=" + a);
return a > 1;
}

public static void main(String[] args)
{
if( test(0) && test(1) )
{
System.out.println("result is true");
}
else
{
System.out.pringln("result is fasle");
}
}
}
能否帮我把每句话的意思和作用说明下
麻烦一下,我是每句话什么意思看不懂,并非短路的原理..比如test(int a),比如return a>1,,
比如if(test(0)&&test(1)),都是什么意思.最后在每句后面写明汉字,谢谢

串联电路中,只要有一处断开,整条路就是不通的。
if (test(0) && test(1)) //&&相当于串联,test(0)为false,整条路就是不通的,test(1)也就不会再判断了,无论它为true还是false都不会执行。所以你会看到,System.out.println("come in and a=" + a); 只执行了一次。
---------------------------------------------------------------
实在很无语,不过秉承认真回答的原则,我还是答一下:
public class ShortCut //定义一个类,类名ShortCut
{
public static boolean test(int a) //定义这个类的静态方法,方法名test
{ //这个方法可以传一个int(即整型)的参数
System.out.println("come in and a=" + a);//输出文字: come in and a=
//(a的值)
return a > 1; //如果 输入的参数>1 成立,返回true,否则返回false
}

public static void main(String[] args) //程序入口,程序从这里开始执行
{
if( test(0) && test(1) ) //调用test(int a),并将返回值作为条件来判断
{
System.out.println("result is true"); //条件成立时输出result is true
}
else
{
//条件不成立时输出result is fasle
System.out.pringln("result is fasle");
}
}
}
//要学java的话,还是要好好看看教程,或者有其它语言的基础。
//这个程