Java基础整理01
Exercise
Test 01
数据类型拓展
整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int a1 = 10;
int a2 = 010; //八进制
int a3 = 0x10; //十六进制 0~9 A~F 16
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println("-------------------------------------");
浮点数拓展: 银行业务怎么表示? 钱
BigDecimal:数学工具类
float:有限 离散 舍入误差 结果是大约、接近但不等于
double:
最好完全避免使用浮点数进行比较
float b = 0.1f;
double c = 1.0/10;
System.out.println(b==c);
float c1 = 1456846464654f;
double c2 = c1 + 1;
System.out.println(c1==c2);
System.out.println("-------------------------------------");
字符拓展:所有的字符本质还是数字
char d1 = 'a';
char d2 = '清';
System.out.println(d1);
System.out.println((int)d1); //强制转换成int类型
System.out.println(d2);
System.out.println((int)d2);
编码 Unicode 表:(97=a 65=A) 2字节 0-65536 Excel 2^16=65536
U0000 UFFFF
char d3 = '\u0061'; //\u不能出现在除转义代码以外的任何注释中
System.out.println(d3);
System.out.println("Hello\tWorld");
System.out.println("-------------------------------------");
String za = new String("hello world");
String zb = new String("hello world"); //注意区分二者区别,从内存级别分析
System.out.println(za==zb);
String zc = "hello world";
String zd = "hello world";
System.out.println(zc==zd);

运算符
(java中关系运算符返回的结果是布尔类型)
++ -- 自增,自减 一元运算符
int a = 3;
int b = a++; //先赋值,再自增
//a = a + 1;
System.out.println(a);
//a = a + 1;
int c = ++a; //先自增,再赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
幂运算 java里不允许出现2^3,但是2×2×2 = 8比较繁杂,很多运算会使用一些工具类操作。
double pow = Math.pow(2,3);
System.out.println(pow);
位运算(二进制、效率极高)
A = 0011 1100 B = 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001 (异或)
~B = 1111 0010 (取反)
2*8 = 16 2*2*2*2
<< 左移:*2
>> 右移:/2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
System.out.println(2<<3);
字符串连接符 + ,String
int d = 10;
int e = 20;
System.out.println(""+d+e); //字符串在前是拼接
System.out.println(d+e+""); //字符串在后是运算
x ? y : z 三元运算符(如果x == true,则结果为y,否则结果为z)
int score = 80;
String type = score<60 ? "不及格" : "及格";
System.out.println(type);

For循环(可以结合断点调试进一步理解)
九九乘法表、三角形案例
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i+"*"+j+"="+(i*j)+"\t");
}
System.out.println();
}
System.out.println("----------------------------------------------------");
for (int a = 0; a <= 5; a++) {
for (int b = 5;b >= a;b--) System.out.print(" ");
for (int b = 1;b <= a;b++){
System.out.print("*");
}
for (int b = 1;b < a;b++){
System.out.print("*");
}
System.out.println();

本文来自博客园,by:{rubp},转载请注明原文链接:https://www.cnblogs.com/RRubp/p/RRubp.html
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术