004ConditionalsInjavaJAVA中的条件句

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

For example:

//Test004.javapublicclassTest004 {publicstaticvoidmain(String[] args) {inta =8;intb =8;if(a==b){

System.out.PRintln("a is equal to b.");

}else{

System.out.println("a is not equal to b.");

}/** == and equals

*TheOperator == works a bit different on objects than on primitives.

When we are using objects and want to check if they are equal, the

operator == wiil say if they are the same, if you want to check if

they are logically equal, you should use the equals method on the object.

For example:*/String person_a=newString("gavin");

String person_b=newString("gavin");

String person_c=person_a;

System.out.println(person_a==person_b);//这里会输因为false,//因为person_a和person_b不是一个对象,分配的物理空间的地址不一样System.out.println(person_a.equals(person_b));//这里会输出true,//因为person_a和persion_b的逻辑值一样,都是gavinSystem.out.println(person_a==person_c);//这里会输出true,//因为person_a和person_b实际上是同一个对象}

}

上述代码中的第6行中,a=b就是条件句。在java中,条件句返回的结果必须是boolean类型的值。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航