Java基础学习3

时间:2025-03-07 20:33:20

Java语法学习3

基本运算符

Java基础学习3

关系运算符返回结果为布尔类型

%:取余 模运算

+、-、*、/、% :二元运算符 两个变量间的运算

++、-- 一元运算符

package Study;

public class demo01 {
public static void main(String[] args) {
//ctrl + d :复制当前行到下一行
int a=3;
int b=a++; //先赋值,再加加
int c=++a; //先加加,在赋值
System.out.println(b); //3
System.out.println(c); //5
double pow = Math.pow(b,c); //调用Math类中的幂计算函数计算b的c次方
System.out.println(pow); }
}

逻辑运算符与位运算

package Study;

public class Demo02 {
public static void main(String[] args) {
boolean a=true;
boolean b=false;
System.out.println("a&&b:" + (a&&b)); //与运算 a&&b:false
System.out.println("a||b:" + (a||b)); //或运算 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
}
}

&:对应运算位间都是1即为1,其他情况为0: 00101&01011 ->00001

|:对应运算位间都是0即为0,其他情况为1: 00101&01011 ->01111

^:相同为0,相反为1: 00101&01011 ->01110 (异或)

~:取反运算: ~00101->11010

》:按位左移 :4》2->1

《:按位右移:2《3->16

字符连接符及三元运算符

package Study;
//字符串连接符 + 其中有String类型
public class Demo03 {
public static void main(String[] args) {
int a=10,b=20;
System.out.println(a+b+"");// 30 先运算再拼接
System.out.println(""+a+b);//1020
/*三元运算符
x ? y : z x为true,结果为y,否则为z
*/
}
}

包机制

Java基础学习3

eg: import com.xiaowei.nb

​ import com.xiaowei.* :导入这个包下所有类

javaDoc

Java基础学习3