基本数据类型
基本数据类型是cpu可以直接进行运算的数据类型,在JVM的栈中分配内存空间
基本数据类型分为六种:
数值型:
- 整数类型:byte(1字节),short(2字节),int(4字节),long(8字节)
- 浮点数类型:float(4字节),double(8字节)
非数值型:
- 字符类型:char(2字节)
- 布尔类型:boolean
整数运算
整数运算遵循四则运算规则,可以任意嵌套小括号
算数运算符包括:+(加) -(减) ×(乘) /(除) %(取余)
四则运算规则与数学运算规则一致
溢出
由于整数存在范围限制,如果计算结果超出了范围,就会产生溢出,而溢出不会显示出错,会得到一个奇怪的结果
例:
public class demo04 {
public static void main(String[] args) {
int a=2147483640;
int b=3456;
int sum=a+b;
System.out.println(sum);
//运算结果:-2147480200
}
}
整数溢出问题的解决:
解决上面的问题,可以将int转换为范围更大的long型,而当整数的范围仍超过long时,可以将这个数封装成Biginteger对象。但是,因为不是基本数据类型,所有不能使用四则运算法则,要使用Biginteger中的add(),substract(),mutiply(),divide()方法。
public class demo04 {
public static void main(String[] args) {
long a=2147483640;
long b=3456;
long sum=a+b;
System.out.println(sum);
//运算结果:2147487096
}
}
import java.math.BigInteger;
public class demo04 {
public static void main(String[] args) {
BigInteger a=new BigInteger("2147483640");
BigInteger b=new BigInteger("3456");
BigInteger sum=new BigInteger("0");
sum=a.add(b);
System.out.println(sum);
//运算结果:2147487096
}
}
位运算
位运算时按照整数的二进制进行移位,与,或,非,异或的运算。应用于整数类型和字节型等数据类型(浮点型不能进行位运算)。由于位运算直接采用二进制进行计算,所有往往可以计算性能的提升
常见的位运算使用场景
位运算符 | 运算规则 |
<<(左移运算) | 相当于乘以 |
>>(右移运算) | 相当于除以 |
&(与运算) | 两个数同时为1,结果为1 |
|(或运算) | 只要任意一个为1,结果为1 |
~(非运算) | 0和1互换 |
^(异或运算) | 两个数相同为1,不同为0 |
位运算实例:
public class demo04 {
public static void main(String[] args) {
int n=7;
//位运算:
int a=n<<1;//相当于7乘2的1次方
int b=n<<2;//相当于7乘2的2次方
int c=n<<27;//相当于7乘2的27次方
int d=n<<28;//相当于7乘2的28次方
System.out.println(a);//运算结果:14
System.out.println(b);//运算结果:28
System.out.println(c);//运算结果:939524096
System.out.println(d);//运算结果:1879048192
}
}
public class demo04 {
public static void main(String[] args) {
int n = 28;
// 位运算:
int a = n >> 1;// 相当于7除以2的1次方
int b = n >> 2;// 相当于7除以2的2次方
int c = n >> 3;// 相当于7除以2的3次方
System.out.println(a);// 运算结果:14
System.out.println(b);// 运算结果:7
System.out.println(c);// 运算结果:3
}
}
public class demo04 {
public static void main(String[] args) {
//&(与运算)
int i=167776589;//00001010 00000000 00010001 01001101
int k=167776512;//00001010 00000000 00010001 00000000
int x=i&k; //00001010 00000000 00010001 00000000
System.out.println(x);//167776512
//|(或运算)
int a=167776589;//00001010 00000000 00010001 01001101
int b=167776512;//00001010 00000000 00010001 00000000
int c=a|b; //00001010 00000000 00010001 01001101
System.out.println(c);//37273
//~(非运算)
int m=167776589;//00001010 00000000 00010001 01001101
int n=~m; //11110101 11111111 11101110 10110010
System.out.println(n);//-167776590
//^(异或运算)
int g=167776589;//00001010 00000000 00010001 01001101
int l=167776512;//00001010 00000000 00010001 00000000
int v=g^l; //00000000 00000000 00000000 01001101
System.out.println(v);//77
//用异或交换两数
int w=3;int e=4;
w=w^e;
e=w^e;
w=w^e;
System.out.println(w);//4
System.out.println(e);//3
}
}
浮点数运算
浮点数精度丢失
计算机使用二进制进行存储数据,而浮点数是由整数部分和小数部分组成,而小数部分无法由二进制精确表示。所以会出现浮点数精度丢失问题
解决方法
可以使用BigDecimal
注意:BigDecimal是有参构造方法,括号中可以传入float,double类型数据,也可以传入String类型,但是传入float,double类型数据还是会产生精度丢失,所以必须传入String类型参数,避免精度丢失
例:
public class demo03 {
public static void main(String[] args) {
BigDecimal a=new BigDecimal("3.2");
BigDecimal b=new BigDecimal("0.6");
System.out.println(a.add(b));//3.8
}
}