注释与数据结构(三)
注释与数据类型
注释
- 单行注释 //
- 多行注释 /*
- 文档注释 /**
八大数据类型
public class Hello { public static void main(String[] args) { //单行注释 // /* 多行注释 /* 多行注释 */ /** * 文档注释 /** */ //八大数据类型 //整型 Byte num1 = 10; int num2 = 20; short num3 = 30; long num4 = 40L; //long型需要在后面加一个L表明 //浮点型 float num5 = 10.1F; //float型需要在后面加一个F表明 double num6 = 3.1415; //字符 char c = 'A'; //布尔型 boolean flag = true; boolean flag2 = false; } }
位与字节
位bit:即二进制中的0和1
字节byte:1字节 = 8位
1024B = 1KB
1024KB = 1M
1024M = 1GB
拓展
import java.math.BigDecimal; public class Demo1 { public static void main(String[] args) { //整型拓展 int a = 10; int b = 010; //8进制0 int c = 0x10; //16进制0x System.out.println(a); System.out.println(b); System.out.println(c); System.out.println("===================="); //浮点数拓展 float f = 1.01f; //float是有精度损失的 double d = 1.01; System.out.println(f); System.out.println(d); System.out.println(f==d); //为false float f2 = 11111111111111111f; float f3 = f2 + 1; System.out.println(f2==f3); //为true //最好完全避免使用浮点数进行比较,可以使用BigDecimal,例子如下 BigDecimal b1 = new BigDecimal(f2); BigDecimal b2 = new BigDecimal(f3); System.out.printf("b1==b2的结果是%b\n", b1==b2); System.out.println("===================="); char c1 = 'A'; char c2 = '中'; System.out.println((int)c1); System.out.println((int)c2); //字符的本质其实还是数字 System.out.println((char)65); //将数字转换为字符 System.out.println("===================="); //字符拓展 System.out.println("Hello\nWorld"); //换行 System.out.println("Hello\tWorld"); //空格 System.out.println("===================="); //布尔拓展 boolean f1 = true; if (f1==true) {} //等价于下面的简写 if (f1) {} } }
类型转换
Byte, short,char --> int --> long --> float --> double
低--------------容量--------------->高
从低到高转换会自动转换,从高到低需要强制转换
public class Demo3 { public static void main(String[] args) { int i = 128; float f = i; //容量从低到高会自动转换-->128.0 System.out.println(f); double d = 3.1415926; int i2 = (int) d; //容量从高的切换到低的需要强制转换-->3 System.out.println(i2); double c = -99999.9999; short i3 = (short) c; //内存溢出情况-->31073 System.out.println(i3); /* 注意点: 1. 不能对布尔型进行转换 2. 不能把对象类型转换为不相关的类型 3. 高容量类型转换为低容量类型,需要强制转换 4. 转换的时候可能存在内存溢出或精度问题 */ } }
【推荐】FlashTable:表单开发界的极速跑车,让你的开发效率一路狂飙
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步