第一章 基本数据类型与运算符
一、基本数据类型
byte字节 | 1byte=8bit | 网络传输,文件大小的最小单位 | -128~127 |
short短整型 | 2byte=16bit | 已淘汰16位计算机用 | -2的n-1次方~2的n-1次方-1 |
int整型 | 4byte=32位 | 字面量 | |
long长整型 | 64位 | 1970-1-1零晨到现在的毫秒数 long a1 = 1l | |
float单精度浮点型 | 32位 | 精确数字8位,默认比所有的整型大 | |
double双精度浮点 | 64位 | 字面量, 财务金额运算不可用 | |
char | 2byte-16位 | ASCII码==对应键盘或半角 unicode码 解决不同系统乱码问题 运算时默认转换int型 | |
boolean | 1位 (bit) | 1==true js可以,但是java不行 true、false不可用和0,1转换 |
100! 注意超出了long, 对象BigInteger来做!
double 运算的话用 BigDecimal类型来做!
数值型 布尔型 字符型
short s = -128;
byte b = (byte)s;
final int age = 23;
long l;
float f = 3.6415f;
double d = 3.1415926;
char c ;
boolean b1 = true;
public static void main(String[] args){
//数值型
//1、整形 byte short int long
Test01 t = new Test01();
System.out.println("b=" + t.b);
t.l = (long)t.f;
//2、浮点型 float double
System.out.println("f=" + t.f);
System.out.println("-------");
System.out.println("l=" + t.l);
//字符型 char 2个字节 '\u1234'
System.out.println("c=" + t.c);
t.c = '\u0061';
//t.i = t.c;
System.out.println("c=" + t.c);
System.out.println("******");
String s1 = "中软国际";
char[] ch = s1.toCharArray();
for(int j=0;j<ch.length;j++){
//int i2 = ch[j];
System.out.println("\\u" + Integer.toString(ch[j], 16));
}
char[] ch2 = {'\u4e2d',
'\u8f6f',
'\u56fd',
'\u9645'
};
for(int m=0;m<ch2.length;m++){
System.out.print(String.valueOf(ch2[m]));
}
//布尔型 boolean
System.out.println("b1=" + t.b1);
}
}
二、标识符
规则:
1、数字、字母、下划线、$组成
2、严格区分大小写
3、不可以用关键字
4、不能以数字开头
5、不能有空格
6、不能用特殊字符 #
7、同区域内不能重复
三、运算符
1、算术运算符 + - * / % ++ –
int b = 7;
int c = a%b;
int n = 547;
//1、算术运算符
//取余
System.out.println("a%b=" + c);
//++ -- 自增 自减
//++a 先自增,再运算
System.out.println("a++=" + a++);
System.out.println("b++=" + b++);
System.out.println(++a + ++b);
//求三位数的各个位
System.out.println("n个位数是:"+n%10);
System.out.println("n十位数是:"+n/10%10);
System.out.println("n百位数是:"+n/100);
System.out.println("************");
2、比较运算符 == != > < >= <=
String s1 = new String(“Hello”);
String s2 = s1;
String s3 = new String(“Hello”);
System.out.println("s1s2" + (s1s2));
System.out.println("s1s3" + s1s3);
int n = 10;
if (n > 10 || n > 100) {}
3、//位运算符 & | ^ ~ << >>
/*15 9
00001111
00001001
00000110
*/
System.out.println(15&9);
System.out.println(15|9);
System.out.println(15^9);
//x取反=-(x+1)
System.out.println(~9);
System.out.println(8<<2);
4、//逻辑运算符 & | && ||
// true & true true
// true && true true 短路与
// false | false false
// false || false false 短路或
System.out.println("~~~~~~~~~~~~~");
System.out.println(4>5 | a>b );
System.out.println(4>5 || a>b); //短路或
System.out.println("~~~~~~~~~~~~~");
String ss1 = null;
System.out.println((ss1==null)||ss1.length()>0);
//System.out.println((ss1==null)|ss1.length()>0);
int n = 10
if (n++ > 10 && n++ > 11) {
}
sysout(n); // 11
5、条件运算符 ? :
int m = a<b?a:b;
System.out.println(m);
6、赋值运算符 = += -= *= -= %=
//a+=b a=a+b
System.out.println(a+=b);
}
四、优先级
由高到低:
0、( )
1、++ –
2、算术运算符
3、比较运算符 == != > <
4、逻辑运算符
5、三元运算符 ?:
6、赋值运算符