一.Java包含的数据类型
二.java数据溢出
class work
{
public static void main(String[] args)
{
int a = Integer.MAX_VALUE;
a+=1;
System.out.println(a);
}
}
显然会溢出。怎么避免呢?
(作为一个OIer难道不会估计一下问题的规模吗?非要搞什么强制转换,不怕给你自己转晕了?)
方案一:不让它溢出,有可能溢出就放大数据范围。
方案二:上高精,高精的高精的高精的……。
方案三:在不影响计算的条件下强制转换后计算。
三:基本类型及对应包装类。(https://blog.csdn.net/qq_37688023/article/details/85106894)
valueOf(X i)返回对象的原则(数据缓存)
Byte
直接取,数组范围为(-128,127),且byte值的范围也是(-128,127)
Short
(-128,127)之间在数组中取,否则new
Integer
(-128,127)之间在数组中取,否则new
Long
(-128,127)之间在数组中取,否则new
Float
直接new
Double
直接new
Boolean
直接返回,不new
Character
0-127之间从数组中取,否则new
public class test
{
public static void main(String[] args)
{
Integer a = 100;
Integer b = Integer.valueOf(100);
System.out.println(a==b);
}
}
输出为true,证明是同一个对象的引用。
public class test
{
public static void main(String[] args)
{
Integer a = 127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
System.out.println((a==b)+" , "+(c==d));
}
//true ,false
}
a,b直接调用已有的对象的引用,c,d则是new的新对象。
四.自动装箱与拆箱
装箱过程是通过调用包装器的valueOf方法实现的,而拆箱过程是通过调用包装器的 xxxValue方法实现的。(xxx代表对应的基本数据类型)。
5.int与integer
public static void main(String[] args)
{
Integer i = Integer.valueOf(10);
int n = i.intValue();
System.out.println(i+","+n);
int a = 10;
Integer b = a;
System.out.println(a+","+b);
}
//10,10
//10,10
}
public static void main(String[] args)
{
Integer i = Integer.valueOf(1050);
System.out.println(i.toString());
//真懒
Integer j = 1050;
System.out.println(i.equals(j));
//equals 找的是int值
System.out.println(i==j);
}
6.&与&&
在逻辑运算中,&&会短路,&不会
(不过我到现在还没见过除了在考试/面试题专门区分这俩的)
&还可以是位运算。
(啥是位运算?自己问百度去)
同理可解|与||;
7.java跳出多重循环
public class test
{
public static void main(String[] args)
{
int i = 0,j = 0;
lab:
for(i = 1;i<=8;i++)
{
for(j = 1;j<=5;j++)
{
if(i+j==11)
{
break lab;
}
}
}
System.out.println(i+","+j);
//这不就是goto吗?c++还专门建议通过bool和for的判断跳出,或者通过函数return,而不是goto。
}
}