运算符,位运算基础,字符串拼接,包机制
运算符
优先级尽量使用()
- 算术运算符 + - * / % ++ --
- 赋值运算符 =
- 关系运算符 > < >= <= == !=
- 逻辑运算符 && || !
- 位运算符 & | ^ ~ >> << >>>
- 条件运算符 ? :
- 拓展赋值运算符 += -= *= /=
基本运算符及其运算
Ctrl + d 复制当前行到下一行
public class Demo01 { public static void main(String[] args) { //二元运算符,两个变量之间计算结果 //Ctrl + d 复制当前行到下一行 int a = 10; int b = 20; int c = 25; int d = 25; System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.out.println((double)a/b); } }
public class Demo02 { public static void main(String[] args) { long a = 12321312321L; int b = 123; short c = 10; byte d = 8; //多种类型数字计算时,有一种为long类型则结果为long类型,否则默认为int类型 //cast 转换 System.out.println(a+b+c+d); //long System.out.println(b+c+d); //int System.out.println(c+d); //int } }
public class Demo03 { public static void main(String[] args) { //关系运算符 返回结果true false 布尔值 //与if配合使用 int a=10; int b=20; int c=21; //取余,模运算 System.out.println(c%a); System.out.println(a>b); System.out.println(a<b); System.out.println(a==b); System.out.println(a!=b); } }
自增自减运算符
public class Demo04 { public static void main(String[] args) { //++自增 --自减 一元运算符,一个变量自身的变化 int a=3; int b=a++; //a++表示执行完这行代码后,a再自增1,先给b赋值,再自增 //a++ a = a+1 System.out.println(a); //a++ a = a+1 int c=++a; //++a表示执行这行代码前,a先自增1再执行这行代码,先自增再给c赋值 System.out.println(a); System.out.println(b); System.out.println(c); //幂运算,使用内置Math函数,很多运算会使用工具类来操作 double pow=Math.pow(2,3); System.out.println(pow); } }
逻辑运算符与位运算符
package operator; //逻辑运算符 public class Demo05 { public static void main(String[] args) { //与(and) 或(or) 非(取反) boolean a=true; boolean b=false; System.out.println("a&&b:"+(b&&a)); //逻辑与运算,两者都为真才为真 System.out.println("a||b:"+(a||b)); //逻辑或运算,两者其一为真就为真 System.out.println("!(a&&b):"+!(a&&b)); //逻辑非运算,如果是真则为假,如果是假则为真 //短路运算 int c=5; boolean d=(c<4)&&(c++<4); //与运算时,前面的值为false,后面的值则不会进行计算和判断 System.out.println(d); System.out.println(c); } }
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 抑或,相同为0,不同为1 ~B = 1111 0010 非 2*8 = 16 2*2^3 位运算,效率极高,将初始数值转换为二进制,将初始二进制数的所有位置的1在二进制水平上向左或向右移动,来得到最终结果 << *2 左移,初始所有位置的1向左移动 >> /2 右移,初始所有位置的1向右移动 */ System.out.println(2<<3); //2<<3与1<<4的结果是相同的 System.out.println(1<<4); System.out.println(3<<3); //0000 0011 0001 1000 初始值转换为二进制数后,所有位置的1都会按位运算移动 System.out.println(5<<3); //0000 0101 0010 1000 System.out.println(7<<3); //0000 0111 0011 1000 } }
赋值运算符,字符串拼接与三元运算符
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); //字符串连接符 + , String System.out.println(""+a+b); //字符串在前则后面会进行拼接 System.out.println(a+b+""); //字符串在后则会先进行运算再拼接 } }
package operator; //三元运算符 public class Demo08 { public static void main(String[] args) { // x ? y : z // 如果x==true,则结果为y,否则结果为z int score=80; String result = score>60 ? "及格" : "不及格"; System.out.println(result); } }
包机制
- 一般利用公司域名倒置作为包名 例如:com.baidu.www
package com.wyd.operator; import com.wyd.base.Demo06; //导入包 import com.wyd.operator.*; //使用*可导入包下所有的类 import java.util.Date; //导入包 public class Demo02 { public static void main(String[] args) { long a = 12321312321L; int b = 123; short c = 10; byte d = 8; Date //多种类型数字计算时,有一种为long类型则结果为long类型,否则默认为int类型 //cast 转换 System.out.println(a+b+c+d); //long System.out.println(b+c+d); //int System.out.println(c+d); //int } }