[JavaSE]基础回顾04
数据类型问题
- 整型的数值范围? 整型的进制
- 浮点数的比较问题,BigDecimal的使用
- 字符串比较的不同
- 布尔类型的使用
public class Demo03 {
public static void main(String[] args){
int i = 10;
int i2 = 010;
int i3 = 0x10;
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("=============================================");
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);
float d1 = 23231232312355f;
float d2 = d1 + 1;
System.out.println(d1 == d2);
System.out.println("=============================================");
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);
System.out.println(c2);
System.out.println((int)c2);
char c3 = '\u0061';
System.out.println("Hello\nWorld");
System.out.println("=============================================");
String hl = new String("hello world");
String hll = new String("hello world");
System.out.println(hl == hll);
String hl1 = "hello world";
String hll1 = "hello world";
System.out.println(hl1 == hll1);
System.out.println("=============================================");
boolean flag = true;
if (flag) System.out.println("老鸟");
}
}
类型转换
- 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换。
byte,short,char -> int -> long ->float -> double
- 运算中,不同类型的数据线转化为同一类型,然后进行运算。
- 强制类型转换
- 自动类型转换
public class Demo04 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;
double c = i;
boolean d = i ;
System.out.println(i);
System.out.println(b);
System.out.println(c);
System.out.println("=====================");
System.out.println((int)23.7);
System.out.println((int)-45.89f);
System.out.println("=====================");
char c = 'a';
int d = c+1;
System.out.println(d);
System.out.println((char)d);
System.out.println("=====================");
int money = 10_0000_0000;
int year = 20;
int total = money * year;
long total1 = money * year;
long total2 = money * year;
System.out.println(total);
System.out.println(total1);
System.out.println(total2);
}
}
变量、变量作用域和常量
变量
- 变量就是可以变化的量。
- Java是强类型语言,声明变量必须指定其类型
- Java变量是程序中最基本能的存储单元,其要素包括变量名,变量类型和作用域。
type varNmae [=value] [{,varNName[=value]}];
- 注意事项:
- 每个变量都有类型,类型可以是基本类型,也可以是引用类型。
- 变量名必须是合法的标识符。
- 变量声明是一条完整额度语句,因此每一个声明都必须以分号结束
变量作用域和常量
public class Variable{
static int allClicks = 0;
String str = "hello world";
public void method(){
int i = 0;
}
}
public class Demo08 {
static double salary = 2500;
static final String zz = "狗子";
String name;
public static void main(String[] args) {
int i = 10;
System.out.println(i);
demo08.name = "猴子";
}
}
变量的命名规范
- 所有变量、方法、类名:见名就知
- 类成员边纳凉:首字母小写和驼峰原则:monthSalary
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母及下划线:MAX_VALUE
- 类名:首字母大写和驼峰原则:Man,GoodMan
- 方法名:首字母小写和驼峰原则:run(),runRun()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!