注释在Java中有单行注释、多行注释、文档注释
- 单行注释 //
- 多行注释
- 文档注释
标识符命名的规则:
- 必须使用字母、下划线(_)、和美元符($)进行开头
- 除第一个元素后剩余组成可以是字母、下划线、美元符号的组合
- 严格区分大小写
- 不可与关键字重名
Java中的数据类型属于强数据类型,必须先定义后使用
Java中的数据类型分为了两种:基本数据类型和引用数据类型
基本数值类型分为:数值类型和boolean类型
数值类型:
- 整数类型
- byte 1个字节
- short 2个字节
- int 4个字节
- long 8个字节
- 小数类型
- float 4个字节
- double 8个字节
boolean类型:
true 和 false 占1位
引用数据类型
- 类
- 接口
- 数组
1 进制问题
public class Demo01 {
public static void main(String[] args) {
int s1 = 10;
int s2 = 010;
int s3 = 0x10;
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
输出结果如下:
解析:八进制是以0开头的,而十六进制是以0x开头的,往十进制去换算时,八进制010就是0x8的0次方加上1x8的1次方,十六进制同上。
2 浮点数问题
public class Demo01 {
public static void main(String[] args) {
float x1 = 0.1f;
float x2 = 1/10;
System.out.println(x1==x2);
x1 = 1212122212;
x2 = x1 + 1;
System.out.println(x1 == x2);
}
}
输出结果
因此,浮点数具有舍入误差,是离散的,所以不可以用来比大小!!!
3 字符问题
public class Demo01 {
public static void main(String[] args) {
char s1 = 'a';
char s2 = 'A';
System.out.println(s1);
//使用强制转换 将S1转换成整数类型
System.out.println((int)s1);
System.out.println(s2);
System.out.println((int)s2);
}
}
输出结果
字符的本质还是整形!!!
转义字符:如**t表示制表符,如n**代表换行等等
类型转换类型转换分为了自动类型转换和强制类型转换
- 自动类型转换
精度由低->高,可以实现自动转换
public class Demo02 {
public static void main(String[] args) {
int i = 12;
float j =0.4f;
System.out.println(i+j);
//计算时,i是int型,j是浮点型,则i先由低精度转换成高精度浮点型后再与j进行计算
}
}
但是会产生内存溢出等问题
public class Demo02 {
public static void main(String[] args) {
int e = 128;
byte ai = (byte)e;
System.out.println(e);
System.out.println(ai);
//明显的内存溢出问题 因为高转低,且byte最多存到127
}
}
输出结果:
128
-128
- 强制类型转换
强制类型转换一般是由高精度像低精度去转换,可能会存在精度缺失的问题
public class Demo02 {
public static void main(String[] args) {
float a = 1.0f;
float b = 2.3f;
System.out.println((int)(a+b));
//强制类型转换 高->低 对应问题:精度损失
}
}
- 低精度->高精度
byte,short,char->int->long->float->double - 操作大数值数时需要注意的问题
public class Demo02 {
public static void main(String[] args) {
int money = 10_0000_0000;
int years = 20;
long total1 = money * years;
long total = money * (long)years;
System.out.println(total1);
System.out.println(total);
}
}
输出结果:
这个例子其实就反映了计算机在执行语句时的一些细节,比如运算时,两个变量间先运算后,再去赋给定义的变量,但是两个int运算时就已经出了问题,所以会出现错误,可以多运行几次这个demo



