【JavaSE 第二天】
一、 初识数据类型
1. 数据类型
(1) 基本数据类型
- 四类八种基本数据类型
- 整数(byte,short,int,long)
- 浮点数(float,double)
- 字符(char)
- 布尔(boolean)
(2) 引用数据类型
- 类
- 接口
- 数组
(3) 常量(constant)
- 自定义常量:通过
final
关键字来定义 - 字面值常量:
- 字符串常量(要用""双引号括起来)
- 整数常量(默认是 int 类型 超过int类型范围末尾加入 L 就以长整型范围输出)
- 浮点常量(默认是 double 类型,加上 F 表示以 float 类型输出)
- 字符常量(要用’ '单引号括起来)
- 布尔常量
- 空常量
(4) 变量(variable)
- 写法:
数据类型 变量名 值
使用之前必须初始化
变量有作用域
public class Demo{
public static void main(String[] args){
// byte 类型
byte b=25;
System.out.println(b);
// short 类型
short c=15;
System.out.println(c);
// long 类型
long d=200000;
System.out.println(d);
// int 类型
int e=40;
System.out.println(e);
// float 类型
float f=666.66f;
System.out.println(f);
// double 类型
double g=100000000.5465;
System.out.println(g);
// boolean 类型
boolean flag=true;
System.out.println(flag);
// char 类型
char h='x';
System.out.println(h);
// String 类型
String str="hello";
System.out.println(str);
}
}
二、 两种常见的输出语句
- 带有换行效果的输出:换行输出语句
System.out.println("");
- 不带有换行效果的输出:直接输出语句
System.out.print("");
-
System.out.println();
什么都不写可以用作换行 - 直接输出语句什么也不写就会报错
- 如果有多项内容需必须要用
+
连接起来System.out.println("age="+age);
三、 进制的表示
- 十进制:正常数
- 二进制:
0b
或0B
开头 - 八进制:
0
开头 - 十六进制:
0x
或0X
开头
public class Demo{
public static void main(String[] args){
//十进制
System.out.println(10);
//二进制
System.out.println(0B10);
//八进制
System.out.println(010);
//十六进制
System.out.println(0X10);
}
}
- 字节有八位
- 以 byte 字节类型为例
- 最高位是符号位1是负号 0是正号
- 负数的范围:
1000 0001 是 -1
1000 0000 是 -128(特殊规定) - 正数的范围:
0000 0000 是 0
0111 1111 是 127
1. 补码与符号位
- 计算机存储以补码形式
- 最高位是符号位
- 1是负数 0是正数
- 规定:
- 正数的补码与反码,原码一样,称为三码合一;
- 负数的补码与反码,原码不一样;
- 负数的原码:把十进制转换为二进制,然后把最高位设置为一;
- 负数的反码:在原码的基础上,最高位不变,其余取反;
- 负数的补码:反码加一;
- 计算机只有加法
- 以补码的加法算实际的减法
2. 转义字符
\n; | 换行 |
\r; | 回车 覆盖最前方 |
\t; | Tab 键 输出"按列对齐"的效果 制表位 |
\; | \ 斜杠 |
\" | " 双引号 |
\’ | ’ 单引号 |
\b | 删除键 Backspace |
\u | 转为字符的 Unicode 编码值的十六进制型 ‘\u5c1a’ |
public class Demo{
public static void main(String[] args){
System.out.println("名字\t性别\t年龄");
System.out.println("张三\t男\t66");
System.out.println("赵武\t男\t88");
}
}
- 直接给 char 类型变量赋值十进制的 0~65535 之间的 Unicode 编码值就可以出现字符
char upperCaseA = 65;
char upperCaseZ = 95;
char lowerCaseA = 97;
char lowerCaseZ = 122;
System.out.println(upperCaseA);
System.out.println(upperCaseZ);
System.out.println(lowerCaseZ);
System.out.println(lowerCaseZ);
四、 基本数据类型转换
1. 自动类型转换(隐式类型转换)
- 将取值类型小的自动提升为取值范围大的类型
类型 | 占用内存 |
byte | 1字节 |
short | 2字节 |
char | 2字节 |
int | 4字节 |
long | 8字节 |
float | 4字节 |
double | 8字节 |
- 当 byte,short,char 数据类型进行算术运算时,按照 int 类型处理
2. 强制类型转换(显式类型转换)
- 将取值类型大的转换为取值范围小的类型
byte byteValue = (byte)100L;
long longValue = 100;
int intValue = (int)longValue;
// 小数类型转换为整数类型时会直接舍弃小数部分
double doubleValue = 3.1415926;
int convertDouble = (int)doubleValue;
System.out.println(convertDouble);
// 超出数据存储范围的强制转换会溢出
int intOriqinalValue = 300;
byte byteCurrentValue = (byte)intOriqinalValue;
System.out.println(byteCurrentValue);
int x = 1;
int y = 2;
// 先把 x 转换为 double,y 会自动升级成更大的 double 类型,所以最终时按照 double 类型做运算
// 否则就是 int 类型先计算在转换为 double 类型损失数据
double result = (double)x/y;
System.out.println(result)
3. 特殊的数据类型转换
// 先做数学的加法运算,然后再做字符串连接
System.out.println(5 + 10 + "abc");
// 否则就会变为字符串的连接
System。out.println("" + 5 + 10);
// String 类型 想转换为基本数据类型不能强制类型转换
int x = Integer.parseInt("123");
System.out.println(x);
// 需要调用包装类型的方法来转换