数据类型学习

public class Demo02 {
public static void main(String[] args) {

//八大基本数据类型

// 整数

int num1 = 10; //最常用 占4个字节范围
byte num2 = 12; //占1个字节范围:-128-127
short num3 = 30; //占2个字节范围:-32768-32767
long num4 = 30L; //long类型要在数字后面加个L,用于区分类型 占8个字节范围

// 小数:浮点数
float num5 = 10.2F; //float类型要在数字后面加个F,用于区分类型
double num6 = 3.1415926;

System.out.println(num5);
System.out.println(num6);

// 字符:一个字
char name ='字';

// 字符串,String不是关键字,是类
// String namea = "小米";

// 布尔值:是 or 非
boolean flag = true;
// boolean flag = false;


//浮点数拓展 面试题:银行业务怎么表示? 钱
//BigDecimal 数学工具类

//float 有限 离散 舍入误差 大约 接近但不等于
//double

//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较
//最好完全避免使用浮点数进行比较

float f = 0.1F; //0.1
double d = 1.0/10; //0.1

System.out.println(f);
System.out.println(d);
System.out.println(f==d); //false

float f1 = 1234545655F;
float f2 = f1 + 1;

System.out.println(f1==f2); //true


//整数拓展: 进制   二进制 0b    八进制    十进制 0    十六进制 0x

int i = 10;
int i2 = 010; //八进制
int i3 = 0x10; //十六进制 0~9 A~F 16
int i4 = 0b10; //二进制 是数字 0 和 1

System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);

//==============================================================
//字符拓展
//============================================================

char a1 = 'a';
char a2 = '中';

System.out.println(a1);
System.out.println((int)a1); //强制转换

System.out.println(a2);
System.out.println((int)a2); //强制转换

//所有字的本质还是数字

//编码 Unicode 表:(97 = a 65 = A) 2字节 0 - 65536 Excel 2 16 = 65536
//U0000 UFFFF

char a3 = '\u0061';

System.out.println(a3); //a

//转义字符
// \t 制表符 Tab
// \n 换行
// ......

System.out.println("Hello\tWorld");
System.out.println("Hello\nWorld");

System.out.println("===============================");


String ab = new String("HelloWorld");
String bb = new String("HelloWorld");
System.out.println(ab==bb);

String ac = "HelloWorld";
String bc = "HelloWorld";
System.out.println(ac==bc);

//对象 从内存分析

//布尔值扩展
boolean flag = true;
if (flag==true){} //新手
if (flag){} //老手
//Less is More! 代码要精简易读


//类型转换
//低------------------------------------高
//byte,short,char----int---long---float---double

int i = 128;
byte b = (byte)i; //内存溢出
double c = i;

//强制转换 (类型)变量名 高----低
//自动转换 低----高

System.out.println(i);
System.out.println(b);

/*注意点:
* 1.不能对布尔值进行转换
* 2.不能把对象类型转换为不相干的类型
* 3.在把高容量转换到低容量的时候,强制转换
* 4.转换的时候可能存在内存溢出,或者精度问题!
**/

System.out.println("===============================");
System.out.println((int)12.7); //12 精度问题
System.out.println((int)-15.78F); //-15 精度问题

System.out.println("===============================");
char h = 'a';
int j = h+1;
System.out.println(j);
System.out.println((int)h);
System.out.println((char)j);


//操作比较大的数的时候吗,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割,对数字没有影响,方便看

int money = 10_0000_0000;
System.out.println(money);
System.out.println("=========================");

int money1 = 10_0000_0000;
int years = 20;
int total1 = money * years; //-1474836480,计算的时候溢出了
long total2 = money * years; //-1474836480,默认是int,转换之前已经存在问题了
long total3 = money * ((long)years); //20000000000 先把一个数转换为long
long total4 = ((long)money) * years; //20000000000 先把一个数转换为long

System.out.println(total1);
System.out.println(total2);
System.out.println(total3);
System.out.println(total4);



}
}
posted @ 2022-02-20 09:53  卿凌  阅读(34)  评论(0)    收藏  举报