package operator;//逻辑运算符publicclassDemon05{publicstaticvoidmain(String[] args){//与(and) 或(or) 非(取反)boolean a =true;boolean b =false;
System.out.println("a && b"+(a && b));//逻辑与运算:两个变量都为真,结果才为true
System.out.println("a || b"+(a || b));//逻辑或运算:两个变量有一个为真,结果为true
System.out.println("!(a && b)"+(!(a && b)));//逻辑非运算:如果为真,则变为假,如果为假则变为真//短路运算int c =5;boolean d =(c<4)&&(c++<10);//因为(c<4)为false,则不执行后面(c++<10)
System.out.println(d);
System.out.println(c);}}
逻辑运算符运行结果一:
D:\environment\java\jdk1.8\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=60102:D:\IntelliJ IDEA 2020.3.2\bin"-Dfile.encoding=UTF-8-classpath D:\environment\java\jdk1.8\jre\lib\charsets.jar;D:\environment\java\jdk1.8\jre\lib\deploy.jar;D:\environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\environment\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\environment\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\environment\java\jdk1.8\jre\lib\ext\localedata.jar;D:\environment\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunec.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\environment\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\environment\java\jdk1.8\jre\lib\javaws.jar;D:\environment\java\jdk1.8\jre\lib\jce.jar;D:\environment\java\jdk1.8\jre\lib\jfr.jar;D:\environment\java\jdk1.8\jre\lib\jfxswt.jar;D:\environment\java\jdk1.8\jre\lib\jsse.jar;D:\environment\java\jdk1.8\jre\lib\management-agent.jar;D:\environment\java\jdk1.8\jre\lib\plugin.jar;D:\environment\java\jdk1.8\jre\lib\resources.jar;D:\environment\java\jdk1.8\jre\lib\rt.jar;E:\桌面\java学习\production\基础语法 operator.Demon05
a && bfalse
a || btrue
!(a && b)truefalse5
Process finished with exit code 0
D:\environment\java\jdk1.8\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=60110:D:\IntelliJ IDEA 2020.3.2\bin"-Dfile.encoding=UTF-8-classpath D:\environment\java\jdk1.8\jre\lib\charsets.jar;D:\environment\java\jdk1.8\jre\lib\deploy.jar;D:\environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\environment\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\environment\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\environment\java\jdk1.8\jre\lib\ext\localedata.jar;D:\environment\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunec.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\environment\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\environment\java\jdk1.8\jre\lib\javaws.jar;D:\environment\java\jdk1.8\jre\lib\jce.jar;D:\environment\java\jdk1.8\jre\lib\jfr.jar;D:\environment\java\jdk1.8\jre\lib\jfxswt.jar;D:\environment\java\jdk1.8\jre\lib\jsse.jar;D:\environment\java\jdk1.8\jre\lib\management-agent.jar;D:\environment\java\jdk1.8\jre\lib\plugin.jar;D:\environment\java\jdk1.8\jre\lib\resources.jar;D:\environment\java\jdk1.8\jre\lib\rt.jar;E:\桌面\java学习\production\基础语法 operator.Demon06
16
Process finished with exit code 0
字符串连接符代码三:
package operator;publicclassDemon07{publicstaticvoidmain(String[] args){int a =10;int b =20;//a+=b; //a=a+b//a-=b; //a=a-b//System.out.println(a);//字符串连接符: + ,String(在+运算符两边,如果有一方出现了字符串类型,就会把其他操作数都转换成String在链接)
System.out.println(a+b);
System.out.println(""+a+b);//1020 字符串在前边,后面会链接
System.out.println(a+b+"");//30 字符串如果在后面,前面的会计算}}
字符串连接符运行结果三:
D:\environment\java\jdk1.8\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=60142:D:\IntelliJ IDEA 2020.3.2\bin"-Dfile.encoding=UTF-8-classpath D:\environment\java\jdk1.8\jre\lib\charsets.jar;D:\environment\java\jdk1.8\jre\lib\deploy.jar;D:\environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\environment\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\environment\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\environment\java\jdk1.8\jre\lib\ext\localedata.jar;D:\environment\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunec.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\environment\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\environment\java\jdk1.8\jre\lib\javaws.jar;D:\environment\java\jdk1.8\jre\lib\jce.jar;D:\environment\java\jdk1.8\jre\lib\jfr.jar;D:\environment\java\jdk1.8\jre\lib\jfxswt.jar;D:\environment\java\jdk1.8\jre\lib\jsse.jar;D:\environment\java\jdk1.8\jre\lib\management-agent.jar;D:\environment\java\jdk1.8\jre\lib\plugin.jar;D:\environment\java\jdk1.8\jre\lib\resources.jar;D:\environment\java\jdk1.8\jre\lib\rt.jar;E:\桌面\java学习\production\基础语法 operator.Demon07
30102030
Process finished with exit code 0
三元运算符代码四
package operator;//三元运算符publicclassDemon08{publicstaticvoidmain(String[] args){// x ? y :z//如果x=true,结果为y,否则结果为zint score =50;
String tyep = score <60?"不及格":"及格";//必须掌握// if
System.out.println(tyep);}}
三元运算符运行结果四:
D:\environment\java\jdk1.8\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2020.3.2\lib\idea_rt.jar=60170:D:\IntelliJ IDEA 2020.3.2\bin"-Dfile.encoding=UTF-8-classpath D:\environment\java\jdk1.8\jre\lib\charsets.jar;D:\environment\java\jdk1.8\jre\lib\deploy.jar;D:\environment\java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\environment\java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\environment\java\jdk1.8\jre\lib\ext\dnsns.jar;D:\environment\java\jdk1.8\jre\lib\ext\jaccess.jar;D:\environment\java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\environment\java\jdk1.8\jre\lib\ext\localedata.jar;D:\environment\java\jdk1.8\jre\lib\ext\nashorn.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunec.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\environment\java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\environment\java\jdk1.8\jre\lib\ext\zipfs.jar;D:\environment\java\jdk1.8\jre\lib\javaws.jar;D:\environment\java\jdk1.8\jre\lib\jce.jar;D:\environment\java\jdk1.8\jre\lib\jfr.jar;D:\environment\java\jdk1.8\jre\lib\jfxswt.jar;D:\environment\java\jdk1.8\jre\lib\jsse.jar;D:\environment\java\jdk1.8\jre\lib\management-agent.jar;D:\environment\java\jdk1.8\jre\lib\plugin.jar;D:\environment\java\jdk1.8\jre\lib\resources.jar;D:\environment\java\jdk1.8\jre\lib\rt.jar;E:\桌面\java学习\production\基础语法 operator.Demon08
不及格
Process finished with exit code 0