Java中布尔类型(boolean)作为一种数据类型,其可以是值true或false;
布尔值(Boolean)表示以下两个值之一:true或false,常用于表示逻辑判断条件的真假。
boolean类型变量声明:
boolean isGreater = 4 > 2; //true
boolean isFunction = false;
System.out.println(isFunction); //输出false
1.布尔运算
布尔运算作为一种关系运算,包括了:
比较运算符:>,>=,<=,==,!=;
逻辑运算符:&&,||,!;
上述关系运算符的优先级为:(从高到低)
第一级:!
第二级:>,>=,<,<=
第三级:==,!=
第四级:&&
第五级:||
1.1短路运算
布尔运算的一大特点即对于逻辑运算符存在短路运算。
对于&&运算,当在运算过程中确定第一个值为false,后续不再进行计算,返回false。
对于||运算,当在运算过程中确定第一个值为true,后续不再进行计算,返回true。
1.2三元运算符
形式如:a?x:y,首先计算a,若a结果为true,则只计算x;若a结果为false,则只计算y。
注意:x与y必须是同类型。
问题:此时x,y类型对返回值的影响?
2.if语句嵌套使用实例:闰年的计算
2.1闰年基本判断方法:
对于整百年(如:2000年,1900年):能被400整除是闰年:(下列语句实际上隐含了paraYear % 100 == 0,做了一个简化)
(paraYear % 400 == 0)
对于非整百年(如:2003年,2004年):能被4整除是闰年。
(paraYear % 4 == 0) && (paraYear % 100 != 0) //判断非整年为闰年
因此在利用Java判断输入的某个年份是否为闰年时,应该首先注意讨论其是否是整百年或非整百年,然后再按照不同的方法进行判断,注意if语句嵌套与||运算符的使用。
整个过程代码描述为:(未运用boolean)
package com.pancake;
public class TEST {
public static void main(String[] args){
int parayear = 2004;
if (parayear % 400 == 0 ){
System.out.println("The year " + parayear +" is both a full year and a leap year.");
} else if (parayear % 4 == 0){
System.out.println("The year " + parayear +" is a leap year,but not a full year.");
}
}//of main
}//of class TEST
实际上,先判断是否能整除400,再判断是否能整除4,可简化代码。(若先判断是否能整除4,此时应同时满足其为非整年的条件)
2.2实例代码:
package com.pancake;
/**
* @Auther:CubicPancake
* @Date:2023-10-09
* @Description:{The complex usage of the if statement.}
*/
public class LeapyearJudge {
/**
* ************************
* The entrance of the program.
*
* @param args Not used now.
* ***********************
*/
public static void main(String[] args) {
// Test isLeapYear
int tempYear = 2021;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
/*
上述代码行中的'isLeapYear(tempYear)'是一个方法的调用,用于判断给定的年份是否为闰年
该方法具体声明见后续代码
‘!’表示对上述返回的布尔值取反
*/
System.out.print("Not ");
} // Of if
System.out.println("a leap year.");
tempYear = 2000;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("Not ");
} // Of if
System.out.println("a leap year.");
tempYear = 2100;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("Not ");
} // Of if
System.out.println("a leap year.");
tempYear = 2004;
System.out.print("" + tempYear + " is ");
if (!isLeapYear(tempYear)) {
System.out.print("NOT ");
} // Of if
System.out.println("a leap year.");
//Test isLeapYearV2
System.out.println("Now use the second version.");
tempYear = 2021;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("Not ");
} // Of if
System.out.println("a leap year.");
tempYear = 2000;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("Not ");
} // Of if
System.out.println("a leap year.");
tempYear = 2100;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("Not ");
} // Of if
System.out.println("a leap year.");
tempYear = 2004;
System.out.print("" + tempYear + " is ");
if (!isLeapYearV2(tempYear)) {
System.out.print("Not ");
} // Of if
System.out.println("a leap year.");
} //of main
/**
********************
* Is the given year leap?
*
* @param paraYear The given year.
*********************
*/
public static boolean isLeapYear(int paraYear){
if ((paraYear % 4 == 0)&&(paraYear % 100 != 0) || (paraYear % 400 ==0)) {
return true;
} else {
return false;
} // Of if
} // Of isLeapYear
/**
*********************
* Is the given year leap? Replace the complex condition with a number of it.
*
* @param paraYear The given year.
*********************
*/
public static boolean isLeapYearV2(int paraYear){
if (paraYear % 4 != 0){
return false;
} else if (paraYear % 400 == 0) {
return true;
} else if (paraYear % 100 == 0) {
return false;
} else {
return true;
}//Of if
}// Of isLeapYearV2
} // Of class LeapYear
最终编译结果: