本文章为个人笔记积累
注释单行注释://
多行注释:
文档注释(javaDoc):
注释不会被执行
标识符 关键字| abstract | assert | boolean | break | byte |
|---|---|---|---|---|
| case | catch | char | class | const |
| continue | default | do | double | else |
| enum | extends | final | finally | float |
| for | goto | if | implemments | import |
| instanceof | int | interface | long | native |
| new | package | private | protected | public |
| return | strictfp | short | static | super |
| switch | synchronized | this | throw | throws |
| transient | try | void | volatile | while |
-
强类型语言
-
所有变量都必须先定义后才能使用
-
基本类型
- 数值类型→整数类型{byte(1字节)、short(2字节)、int(4字节)、long(8字节)}
→浮点类型{float(4字节)、double(8字节)}
→字符类型{char(2字节)}
- boolean类型→1位只有true和false
引用类型
- 类
- 接口
- 数组
扩展public class Demo02 {
public static void main(String[] args) {
//整数扩展 进制 二进制0b 十进制 八进制0 十六进制0x
int num1 = 10;
int num2 = 010; //八进制0
int num3 = 0x10; //十六进制0x 0~9 A~F 15
System.out.println(num1);
System.out.println(num2);
System.out.println(num3);
System.out.println("===========================");
//=================================================
//浮点数扩展
//float 有限、离散、舍入误差、大约、接近不等于
// double
//最好完全避免使用浮点数进行比较
//BigDecimal 数学工具类
float f = 0.1f; //0.1
double d = 1.0/10; //0.1
System.out.println(f==d); //false
float f1 = 123123123123f;
float d1 = f1 +1;
System.out.println(f1==d1); //true
System.out.println("===========================");
//=================================================
//字符扩展
char c1 = 'A';
char c2 = '字';
System.out.println(c1);
System.out.println((int)c1);
System.out.println(c2);
System.out.println((int)c2);
System.out.println("===========================");
//所有的字符本质还是数字
//编码 Unicode 表(97=a 65=A)2字节 0~65536 2^16
//U0000 UFFFF
char c3 = 'u0061';
System.out.println(c3); //a,c3中的61为十六进制,转化成十进制为97
//=================================================
//转义字符
// t 制表符
// n 换行
// r 回车
// a 响铃
// b 退格
// f 换页
// t 水平制表(HT)
// v 垂直制表(VT)
// 空字符(null)
System.out.println("Hellotworld");
System.out.println("Hellonworld");
System.out.println("===========================");
//=================================================
//内容与地址
String s1 = new String("hello world");
String s2 = new String("hello world");
System.out.println(s1==s2); //false,s1与s2比较的地址,new一个内存会创建一块空间
String s3 = "hello world";
String s4 = "hello world";
System.out.println(s3==s4); //true,s3与s4比较的是值
//=================================================
//布尔值扩展
//if中的布尔值默认为true,可以省略
boolean flag = true;
if (flag==true){}
if (flag){}
}
}
类型转换
低--------------------------------------------->高
byte,short,char -> int -> long -> float -> double
//强制转化 格式:(类型)变量名 高→底
int i1 = 128;
byte b1 = (byte) i1;
System.out.println(i1); //128
System.out.println(b1); //-127,内存溢出,byte最大127
System.out.println("========================");
//自动转化 低->高
int i2 = 128;
double b2 = i1;
System.out.println(i2); //128
System.out.println(b2); //128.0
System.out.println("========================");
System.out.println((int)23.3); //23
System.out.println((int)-33.3f); //-33
System.out.println("========================");
char a = 'a';
int b = a+1;
System.out.println(b);
System.out.println((char) b); //在ASCII编码中a=97 b=98
注意:
-
不能对布尔值进行转换
-
不能把对象类型转换为不相干的类型
-
在把高容量转换到低容量的时候强制转换
-
.转换的时候可能存在内存溢出,或者精度问题
/操作比较大的数时候,注意溢出问题 //JDK7新特性,数字之间可以用下划线分割 int money = 10_0000_0000; int year = 20; int sum1 = money*year; System.out.println(sum1); //-1474836480,计算的时候溢出了 long sum2 = money*year; System.out.println(sum2); //-1474836480,默认是int,转换之前已经存在问题 long sum3 = money*((long)year); System.out.println(sum3); //20 0000 00000,先把一个数转换成long



