文章目录
Tips:
1.IDEA小技巧
输入:psvm
idea按tab补全:
public static void main(String[] args) {
}
在public static void main(String[] args) {
}中键入sout按tab补全:
System.out.println();
ctrl+d 复制本行内容到下一行
System.out.println(f4 == f5);
System.out.println(f4 == f5);
2.注释
// 单行注释
/**/ 多行注释(*后面不加空格)
/** 文档注释(*后面加空格)
*
*
*/
标识符
JAVA中的所有关键字
标识符(identifier)
规则:
-
所有标识符都应该以"JAVA字母"(包含中文,日文,韩文等),美元符号($),或者下划线(_)开始
-
首字符后可以为数字
-
不能把java关键字作为标识符
-
标识符没有长度限制
-
标识符对大小写敏感
String llama = "yongyuan";
// 以数字开头会报错
// String 7llama = "xiangxin";
String __$ = "base64"; // 下划线也可
String $oops = "wooopos!";
// String @llama = "wooopos!"; // 这三个都报错
// String #llama = "wooopos!";
// String $@llama = "wooopos!";
String 你号 = "你好啊!"; // 可以中文开头
String 你1112233 = "纵使风华绝代,一朝勘定乾坤";
String u4f60 = "纵使风华绝代,一朝勘定乾坤";
System.out.println(__$);
System.out.println($oops);
System.out.println(llama);
System.out.println(你号);
// 必须对应类型
// String bujiatang = 111;
// String bujiatang = 所有;
String bujiatang = "所有";
// int 两种定义方法
Integer suoyou = 11;
int suoyou2 = 11;
int zuoyou = 22;
System.out.println(bujiatang);
System.out.println(suoyou);
System.out.println(zuoyou);
System.out.println(suoyou == suoyou2); // true
数据类型
1 基本类型(primitive type)
整数
// 整数
int num1 = 10;
byte num2 = 20;
short num3 = 30;
long num4 = 4000000L; // long类型后面要加L
浮点数
// 浮点数
float num5 = 50.1F; // float类型后要加F
double num6 = 3.141592653589793238462643;
字符
// 字符
char tr1 = 'l';
// char tr1 = 'llama';
char tr2 = '牛';
字符串
// 字符串
// String tr3 = 'llama';
String tr3 = "llama";
布尔值
// 布尔值
// boolean flag = 1; // 1 错
// boolean flag = True; // True 错
boolean flag = true;
boolean flag2 = false;
2 引用类型(reference type)
数据类型拓展
整数拓展
// 整数拓展:
// 进制 二进制0b 十进制 八进制0 十六进制0x
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 Expanded -----------");
// 浮点数拓展
// 浮点数计算问题
// float
float f1 = 5.66F;
// double
double f2 = 5.66;
double f3 = 5.666666666666666;
System.out.println(f1 == f2); // false
System.out.println(f1 == f3); // false
float f4 = 233333333333F;
float f5 = 233333333333.0F;
float f6 = f4 + 1;
System.out.println(f4 == f5); // true
System.out.println(f4 == f6); // true
//=============================================================
//所以避免使用浮点数比较 改用BigDecimal
//=============================================================
字符拓展
// 字符拓展
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((char)(97));
System.out.println((char)20013);
System.out.println((char)20014);
System.out.println((char)20012);
System.out.println((int)'火');
System.out.println((int)'鸡');
Unicode 编码转义
// unicode 编码转义
char c3 = '\u0061';
char c4 = '\u1111';
char c5 = '\uffff';
System.out.println(c3);
System.out.println(c4);
System.out.println(c5);
转义字符
所有转义字符:
// 转义字符
// \t..
System.out.println("Truthahn\tTechnology");
System.out.println("Truthahn\rTechnology");
System.out.println("Truthahn\bTechnology");
System.out.println("Truthahn\fTechnology");
System.out.println("Truthahn\nTechnology");
System.out.println("Truthahn\'Technology");
System.out.println("Truthahn\"Technology");
System.out.println("Truthahn\0Technology");
System.out.println("Truthahn\\Technology");
System.out.println("Truthahn\111Technology");
System.out.println("Truthahn\u1111Technology");
底层思维
// 底层思维
// new String()对象开辟了两个空间,所以不等
String sa = new String("llama");
String sb = new String("llama");
System.out.println(sa == sb); // false
// 相同字符串使用同一空间,属于常量池里的常量值,所以相等
String sc = "llama";
String sd = "llama";
System.out.println(sc == sd); // true
new String()对象开辟了两个空间,所以表达式sa == sb为false;
sc和sd为相同字符串使用同一空间,所以表达式sc == sd为true。
String(“llama”);
System.out.println(sa == sb); // false
// 相同字符串使用同一空间,属于常量池里的常量值,所以相等
String sc = “llama”;
String sd = “llama”;
System.out.println(sc == sd); // true
new String()对象开辟了两个空间,所以表达式sa == sb为false;
sc和sd为相同字符串使用同一空间,所以表达式sc == sd为true。