2.JAVA基础语法

rt.jar ---- javaSE部分要学的内容

1. 8种基本类型

类型分类

  1. 基本类型

    1. 整型
    2. 浮点型
    3. 字符型
    4. 布尔型
  2. 对象类型

1.1 基本整型

类型
byte字节8位(bit) -128 ~127文件大小最小单位,网络传送中的最小单位
short短整型2字节 16bit 公式淘汰:16位计算算计时代的
int整型(字面量(代码中的常量值)-默认值)4byte 32位字面量!
long长整型8byte 64位时间的毫秒数。 赋值的值加l
float单精度浮点型32位浮点型>整型, 有效8位数字,小数后8位
double双精度浮点型64位字面量!16
boolean布尔型1bittrue|false; 没有和1,0的转换
char字符2byte 16bitchar单个字符, String字符串对象类型
char型和整数隐式转换, 参与运算变成整数
半角ascII, 全角:中文等语言Unicode ‘\uFFFF’
ASCII码:
‘0’ ==== 48
‘A’=====65
’a’=====97
// 字节类型
		byte a = -128; // 字面量32->byte 隐式转换
		System.out.println(a);
		
		// 大转小要强制转换
		byte b = (byte) 129; // -127????
		System.out.println(b);
		
		// 字面量10000
		int c = 10000;
		System.out.println(c);
		
//		int d = 111111111111111111111;

//		小转大  隐式转换
		c = a;
//		a = c; // c 32bit a 8bit
		long d = 1; // 1 32bit 
		System.out.println(d);

		// c int = d long
		c = (int) d; // d 64位  也需要强转

		System.out.println(123456789012L); // 字面量 32bit 

		long d1 = 12321L;
		System.out.println(d1);

		// 超过64位时如何处理
		BigInteger b1 = new BigInteger("13224564654654657498786135465487984131465749879874913516548789798");
		System.out.println(b1);
		BigInteger r = b1.add(new BigInteger("1"));
		System.out.println(r);
//		b1.add(val); // 加法
//		b1.subtract(val) // 减法
//		b1.multiply(val) // 乘法
//		b1.divide(val)  // 除法
		
		
		// float
		float e1 = 1;
		
		e1 = 1L;
		System.out.println(e1);
		float e2 = 0.1F; // 小数的字面量是doule
		System.out.println(e2);
		float e3 = 0.123456789012345678f;// 1111111.123456789012345678F;
		System.out.println(e3);
		
		// 字面量
		double e4 = 0.123456789012345678;
		System.out.println(e4);
		// 浮点型不允许运算(规约) BigDecimal
		System.out.println(0.1 + 0.2 == 0.3);
		
		// 布尔型
		boolean f1 = true;
		System.out.println(f1);
		
		f1 = false;
		System.out.println(f1);
		
		// char
		char g1 = 'a';
		System.out.println(g1);
//		char g1 = 'ab'; 单个字符 半角  8位 ascII
		char g2 = '我'; // 全角 ---16位 -- Unicode
		
		// char 和整型 隐式转换的, 可以参与运算.
		char g3 = 97; // ascII码
		System.out.println(g3);
		System.out.println(g2 + 0); //  参与运算转成数值
		System.out.println(Integer.toHexString(g2 + 0)); // 16进制数 Hex
		char g4 = '\u6211'; // \\u unicode码
		System.out.println(g4);
		char g5 = 'あ';
		System.out.println(g5 + 0);
		
		// 转义字符
		System.out.println('\\');
		System.out.println('\'');
		System.out.println('\t'); // 制表符
		System.out.println("aaaa\r\nbbb"); // \r回车  \n换行
		

1.2 进制定义:

		// 0b二进制
		int h1 = 0b10;
		System.out.println(h1);
		// 0x十六进制
		h1 = 0x10;
		System.out.println(h1);		
		// 0开头 八进制
		h1 = 010;
		System.out.println(h1);

1.3 转换问题:

大 = 小 隐式

小 = (小类型)大 强制

运算 结果 按大类型算

		// 0
		System.out.println(1 / 2);
		// 0.5
		System.out.println(1 / 2.0);
		// Infinity
		System.out.println(1 / 0.0);
		// System.out.println(1 / 0); 异常了

1.4 超范围场景

大整型 BigInteger

大浮点型 BigDecimal (金额运算必须用BigDecimal)

2 运算符

2.1 算术运算符:

运算符说 明
一元-一元减,即取负
++**自增,如:++a 等价于a = a + 1
自减,如:–a等价于a = a– 1
二元+加法,返回两个操作数的和
-减法,返回两个操作数的差
*****乘法,返回两个操作数的积
/除法,返回两个操作数的商
%取模,返回两个操作数整除后的余数

2.1.1 自加自减

变量在前,整体是之前值

变量在后,整体是之后值

		int a1 = 10;
		System.out.println(a1++); // 10  a1 = 11
		System.out.println(++a1); // 12  a1 = 12
		
		a1 = 10;
		System.out.println(a1+++ ++a1); // 22
		a1 = 10;
		System.out.println(a1+++a1++); // 21
		a1 = 10;
		System.out.println(--a1+a1--- ++a1); // 9,
		a1 = 10;
		System.out.println(--a1+a1--- --a1); // 11
		// % 求余
		System.out.println(3 % 5); // 3
		System.out.println(5 % 3); // 2
	

2.2 关系运算符: 结果都是布尔值

运算符说 明
==等于,检查两个操作数是否相等
!=不等于,检查两个操作数是否不相等
>大于,检查左操作数是否大于右操作数
>=大于等于,检查左操作数是否大于或等于右操作数
<小于,检查左操作数是否小于右操作数
<=小于等于,检查左操作数是否小于或等于右操作数
		// 关系运算符:: 布尔型
		System.out.println(-1 > -2);
		System.out.println(1 > 2);
		
		// 基本类型  判断值相等
		System.out.println(0 == 0.0); // true
		System.out.println(1 == 1.0); // true

2.3 逻辑运算符

!非 !=
&与 and (位运算符)
|或 or (位运算符)
&&短路与
||短路或

在这里插入图片描述

2.4 位运算

常见的位运算符有:

  • & 、 | 、 ^ 、 ~

还有移位运算符

  • <<、>> 、>>>

<< 左位移,

>> 右位移 , 正数补零, 负数补一

>>> 无符号位移 。 都补零

2.5 三元表达式

a ? b : c

a是true结果是b

a是false结果是c

2.6 赋值运算

=赋值,将右侧的值赋给左侧的变量
+=相加并赋值,如:a += b等价于****a = a + b
-=相减并赋值,如:a -= b等价于****a = a b
*=相乘并赋值,如:a *= b等价于****a = a * b
/=相除并赋值,如:a /= b等价于****a = a / b
%=取模并赋值,如:a %= b等价于****a = a % b

a = 1;
a += 1; // a 2
a -= 2; // a 0
...

2.7 运算符表达式

优先级运 算 符
1括号: ()和[]
2一元运算符:-、++(前置)、–(前置)、!
3算术运算符: * / + -
4关系运算符:> >= < <= != ==
5逻辑运算符:& | && || (and > or)
6条件运算符:? :
7赋值运算符:=、*=、/=、%=、+=和-=

if ( a + b > c && XXX)

一元>二元>三元>赋值

前置)、! |
| 3 | 算术运算符: * / + - |
| 4 | 关系运算符:> >= < <= != == |
| 5 | 逻辑运算符:& | && || (and > or) |
| 6 | 条件运算符:? : |
| 7 | 赋值运算符:=、*=、/=、%=、+=和-= |

if ( a + b > c && XXX)

一元>二元>三元>赋值

posted @ 2021-04-10 12:10  剑心空明  阅读(2)  评论(0编辑  收藏  举报  来源