(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b: "+(b&&a));//逻辑与运算:两个变量都为真,结果才为true //a && b: false
System.out.println("a || b: "+(a||b));//逻辑或运算:两个变量有一个为真,则结果才为true //a || b: true
System.out.println("! (a && b): "+! (a&&b));//如果是真,则变为假,如果是假则变为真 //! (a && b): true
//短路运算
int c = 5;
boolean d = (c<4)&&(C++<4);
System.out.println(d); //false
System.out.println(c); //5
}
}
public class Demo06 {
public static void main( String[] args) {
/*
A = 0011 1100
B = 0000 1101
---------------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~B = 1111 0010
2*8 = 16 2*2*2*2
效率极高!!!
<< *2
>> /2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3); //16
}
}
Java基础10:三元运算符及小节
public class Demo07 {
public static void main( string[] args) {
int a = 10;
int b = 20;
a+=b; //a = a+b
a-=b; //a = a-b
System.out.println(a); //10
//字符串连接符 + , String
System.out.println(""+a+b); //1020
System.out.println(a+b+""); //30
}
}
//三元运算符
public class Demo08 {
public static void main( String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 50;
String type = score < 60?"不及格":"及格";//必须掌握
//if
System.out.println(type); //不及格
int score = 80;
String type = score < 60?"不及格":"及格";//必须掌握
//if
System.out.println(type); //及格
}
}
Java运算符优先级列表
优先级 |
运算符 |
简介 |
结合性 |
1 |
[ ] 、 . 、( ) |
方法调用,属性获取 |
从左向右 |
2 |
!、~、 ++、 -- |
一元运算符 |
从右向左 |
3 |
* 、/ 、% |
乘、除、取模(余数) |
从左向右 |
4 |
+ 、 - |
加减法 |
从左向右 |
5 |
<<、 >>、 >>> |
左位移、右位移、无符号右移 |
从左向右 |
6 |
< 、<= 、>、 >=、 instanceof |
小于、小于等于、大于、大于等于, 对象类型判断是否属于同类型 |
从左向右 |
7 |
== 、!= |
2个值是否相等,2个值是否不等于。 下面有详细的解释 |
从左向右 |
8 |
& |
按位与 |
从左向右 |
9 |
^ |
按位异或 |
从左向右 |
10 |
| |
按位或 |
从左向右 |
11 |
&& |
短路与 |
从左向右 |
12 |
|| |
短路或 |
从左向右 |
13 |
?: |
条件运算符 |
从右向左 |
14 |
=、 += 、-= 、*= 、/=、 %=、 &=、 |
混合赋值运算符 |
从右向左 |
14续 |
|=、 ^=、 <、<= 、>、>= 、>>= |
混合赋值运算符 |
从右向左 |
Java基础11:包机制
-
为了更好地组织类,Java提供了包机制,用于区别类名的命名空间。
-
包语句的语法格式为:
package pkg1[. pkg2[. pkg3...]];
-
一般利用公司域名倒置作为包名; www.baidu.com>com.baidu.www
-
为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包。使用"import"语句可完成此功能
import package1[.package2...].(classname|*);
阿里巴巴 Java 开发手册
阿里巴巴 Java 开发手册 下载
Java基础12:JavaDoc
- 参数信息
- @author作者名
- @version版本号
- @since指明需要最早使用的jdk版本
- @param 参数名
- @return返回值情况
- @throws异常抛出情况
package com.kuang.base;
/**
*@author Kuangshen
*@version 1.0
*since 1.8
*/
public class Doc {
string name;
/**
*@author Kuangshen
*@param name
*@return
*@throws Exception
*/
public String test(String name) throws Exception{
return name;
}
}