文章目录
一、数据类型
数据类型分为了基本数据类型(简单类型)和引用数据类型,而基本数据类型包括数值型、字符型和布尔型,如int、long、char、boolean等;引用数据类型包括数组、类、接口。
1、常量和变量
变量:程序在运行期间可以改变值的;
常量:在程序运行期间不可改变值的(1、字面常量;2、被final修饰的变量;)
注:局部变量作用于自身所在{ }中;
2、基本数据类型
基本类型 | 字节大小 | 取值范围 | 用法 | 包装类 |
int | 4 | -2^31—2^31-1 | int a = 10; | integer |
long | 8 | -2^63—2^63-1 | long b = 15L; | long |
float | 4 | -1.4E-45—3.4028235E38 | float c = 1.5f; | float |
double | 8 | 4.9E-324—1.7977E308 | double d = 2.5; | double |
char | 2 | 0—65535 | char e = '高'; | character |
short | 2 | -32768—32767 | short a = 20; | short |
byte | 1 | -128—127 | byte g = 25; | byte |
boolean | 无规定 | true/false | boolean = ture; | boolean |
注 :java语言中不会由于系统或平台的不同而对数据类型字节大小产生影响,体现了Java的可移植性和跨平台性。
3、类型转换
(1)强制类型转换
例:
int a = 10;
float b = 10.5f;
System.out.println("输出结果为int类型:"+(int)(a+b));
//输出结果为int类型:20;
(2)隐式类型转换
例:
int a = 10;
float b = 10.5f;
System.out.println("输出结果为float类型:"+(a+b));
//输出结果为int类型:20.5;
二、运算符
1、= + - * / % ++ –
- = : 赋值运算符;
- +:加法运算符(当字符串在前时,+默认为拼接);
- -:减法运算符
- *:乘法运算符
- /:取整
- %:取余
- ++自增1
- –自减1
例:
int a = 10;
int b = 25;
int d = b - a;
int e = a*b;
int c = a + b;
System.out.printIn("c的值为:"+c);//此处+号为拼接;35
System.out.println(d);//15
System.out.println(e);//250
System.out.printIn(a/b);//0
System.out.printIn(a%b);//10
System.out.printIn(a++);//10
System.out.printIn(++a);//11
System.out.printIn(a--);//10
System.out.printIn(--a);//9
2、&& || !和 & | ^ ~
- && : 逻辑与
- ||:逻辑或
- !:逻辑非
- &:按位与
- |:按位或
- ^:按位异或
- ~:按位取反
例:
int a = 10;
int b = 15;
System.out.printIn(a > b&&a == 10);//ture
System.out.printIn(a > b||a == 10/0);//ture,可短路
System.out.printIn(!false);//ture
System.out.printIn(a > b&a == 10);//ture,boolean类型用法类似&&,但不可短路
System.out.printIn(a > b|a == 10);//ture,boolean类型用法类似||,但不可短路
& 的用法:
0010 0011(a)
0110 0101(b)
0010 0001(a&b)
| 的用法:
0010 0011(a)
0110 0101(b)
0110 0111(a|b)
^ 的用法:
0010 0011(a)
0110 0101(b)
0100 0110(a^b)
~ 的用法:
0010 0011(a)
1101 1100(~1)
3、>> << >>>
1.>>:右移
2.<<:左移
3.>>>无符号右移
>> 的用法:
0010 0011(a)
0001 0001(a>>1)
<< 的用法:
0010 0011(a)
01000110 (a<<1)
>>> 的用法:
1111 1111(a)
0111 1111(a>>>1)