视频链接:https://www.bilibili.com/video/BV1fh411y7R8
一. 变量
变量原理
变量
一个程序就是一个世界
![image-20220508152241409](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
![image-20220508152541414](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
![image-20220508152603437](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
变量概念
**变(变化)量(值)的介绍 **
变量相当于内存中一个数据存储空间
的表示,你可以把变量看做是一个房间的门牌号,通过门牌号我们可以找到房
间,而通过变量名可以访问到变量(值)。
声明变量
int a;
赋值
a = 60; //应该这么说: 把 60 赋给 a
使用 System.out.println(a);
//也可以一步到位 [int a = 60; 通常我们是一步完成]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class Var01 {
public static void main(String[] args) {
int a; a = 100; System.out.println(a);
int b = 800; System.out.println(b); } }
|
变量入门
变量快速入门
变量使用入门案例
看演示并对代码进行说明, 演示记录 人的信息的代码
![](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Var02 {
public static void main(String[] args) { int age = 30; double score = 88.9; char gender = '男'; String name = "king"; System.out.println("人的信息如下:"); System.out.println(name); System.out.println(age); System.out.println(score); System.out.println(gender); } }
|
变量细节
![image-20220508154442205](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public class VarDetail {
public static void main(String[] args) { int a = 50; System.out.println(a); a = 88; System.out.println(a);
} }
class Dog { public static void main(String[] args) { int a = 666; } }
|
加号使用
![](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12
| public class Plus {
public static void main(String[] args) { System.out.println(100 + 98); System.out.println("100" + 98);
System.out.println(100 + 3 + "hello"); System.out.println("hello" + 100 +3);
} }
|
二. 数据类型
数据类型
每一种数据都定义了明确的数据类型,在内存中分配了不同大小的内存空间(字节)。
![image-20220508160100958](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
java 数据类型分为两大类 基本数据类型, 引用类型
**基本数据类型有 8 种 数值型 [byte , short , int , long , float ,double] char , boolean
**
引用类型 [类,接口, 数组]
整型
整数类型
Java 的整数类型就是用于存放整数值的,比如 12 , 30, 3456 等等
byte n1 = 10;
short n2 = 10;
int n3 = 10;//4 个字节
long n4 = 10; //8 个字节
![image-20220508161536496](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
![image-20220508161227727](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
bit:计算机中的最小存储单位
;byte:计算机中基本存储单位
![image-20220508174900285](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12
| public class IntDetail {
public static void main(String[] args) {
int n1 = 1; long n3 = 1L;
} }
|
浮点数
浮点类型
Java 的浮点类型可以表示一个小数,比如 123.4 ,7.8 ,0.12 等等
![image-20220508175600469](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
![image-20220508175638655](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
关于浮点数在机器中存放形式的简单说明,浮点数=符号位+指数位+尾数位
尾数部分可能丢失,造成精度损失
(小数都是近似值)。
浮点数细节
![image-20220508175933221](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public class FloatDetail {
public static void main(String[] args) {
float num2 = 1.1F; double num3 = 1.1; double num4 = 1.1f;
double num5 = .123; System.out.println(num5); System.out.println(5.12e2); System.out.println(5.12E-2);
double num9 = 2.1234567851; float num10 = 2.1234567851F; System.out.println(num9); System.out.println(num10);
double num11 = 2.7; double num12 = 2.7; System.out.println(num11); System.out.println(num12); if( num11 == num12) { System.out.println("num11 == num12 相等"); } if(Math.abs(num11 - num12) < 0.000001 ) { System.out.println("差值非常小,到我的规定精度,认为相等..."); } System.out.println(Math.abs(num11 - num12)); } }
|
![image-20220508181900845](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
Java API文档
Java API 文档
![image-20220508185115534](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
字符型
字符类型(char)
字符类型可以表示单个字符
,字符类型是 char,char 是两个字节(可以存放汉字),多个字符我们用字符串 String(我们后面详细讲解 String)
1 2 3 4 5
| #代码 char c1 = 'a'; char c2 = '\t'; char c3 = '韩'; char c4 = 97;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public class Char01 {
public static void main(String[] args) { char c1 = 'a'; char c2 = '\t'; char c3 = '韩'; char c4 = 97;
System.out.println(c1); System.out.println(c2); System.out.println(c3); System.out.println(c4); } }
|
字符型细节
![](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class CharDetail {
public static void main(String[] args) {
char c1 = 97; System.out.println(c1);
char c2 = 'a'; System.out.println((int)c2); char c3 = '韩'; System.out.println((int)c3); char c4 = 38889; System.out.println(c4);
System.out.println('a' + 10);
char c5 = 'b' + 1; System.out.println((int)c5); System.out.println(c5); } }
|
![image-20220508191250728](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
ASCII码对照表:
![ASCII码对照表](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
字符型本质
![image-20220508192303779](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
常用编码
![image-20220508193828402](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
![image-20220508193912565](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
![image-20220508193948746](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
布尔类型
布尔类型
![image-20220508195843226](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Boolean01 { public static void main(String[] args) { boolean isPass = true; if(isPass == true) { System.out.println("考试通过,恭喜"); } else { System.out.println("考试没有通过,下次努力"); } } }
|
基本数据类型转换
自动类型转
自动类型转换基础
![image-20220508200046295](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12
| public class AutoConvert {
public static void main(String[] args) { int num = 'a'; double d1 = 80; System.out.println(num); System.out.println(d1);
} }
|
自动类型转换细节
![image-20220508200129693](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| public class AutoConvertDetail {
public static void main(String[] args) { int n1 = 10; float d1 = n1 + 1.1F;
byte b1 = 10; byte b2 = 1; byte b3 = 2; short s1 = 1; int s2 = b2 + s1;
boolean pass = true;
byte b4 = 1; short s3 = 100; int num200 = 1; float num300 = 1.1F;
double num500 = b4 + s3 + num200 + num300; } }
|
强制类型转换
强制类型转换
自动类型转换的逆过程,将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符 ( ),但可能造成 精度降低或溢出,格外要注意
。
![image-20220508203534000](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class ForceConvert {
public static void main(String[] args) { int n1 = (int)1.9; System.out.println("n1=" + n1);
int n2 = 2000; byte b1 = (byte)n2; System.out.println("b1=" + b1); } }
|
![image-20220508204000221](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class ForceConvertDetail {
public static void main(String[] args) { int x = (int)(10*3.5+6*1.5); System.out.println(x);
char c1 = 100; int m = 100; char c3 = (char)m; System.out.println(c3);
} }
|
类型转换练习
![image-20220508204706715](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
基本数据类型和String类型的转换
基本数据类型和 String 类型的转换
![image-20220508205109331](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public class StringToBasic {
public static void main(String[] args) { int n1 = 100; float f1 = 1.1F; double d1 = 4.5; boolean b1 = true; String s1 = n1 + ""; String s2 = f1 + ""; String s3 = d1 + ""; String s4 = b1 + ""; System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);
String s5 = "123"; int num1 = Integer.parseInt(s5); double num2 = Double.parseDouble(s5); float num3 = Float.parseFloat(s5); long num4 = Long.parseLong(s5); byte num5 = Byte.parseByte(s5); boolean b = Boolean.parseBoolean("true"); short num6 = Short.parseShort(s5);
System.out.println("==================="); System.out.println(num1); System.out.println(num2); System.out.println(num3); System.out.println(num4); System.out.println(num5); System.out.println(num6); System.out.println(b);
System.out.println(s5.charAt(0)); } }
|
![image-20220508212639235](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
String转基本类型细节
案例演示: StringToBasicDetail.java
在将 String 类型转成 基本数据类型时,要确保 String 类型能够转成有效的数据
,比如 我们可以把 “123” , 转成一个整数,但是不能把 “hello” 转成一个整数
如果格式不正确,就会 抛出异常,程序就会终止, 这个问题在异常处理章节中,会处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public class StringToBasicDetail {
public static void main(String[] args) { String str = "hello"; int n1 = Integer.parseInt(str); System.out.println(n1); } }
|
本章作业
![image-20220508213940898](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Homework01 {
public static void main(String[] args) { int n1; n1 = 13; int n2; n2 = 17; int n3; n3 = n1 + n2; System.out.println("n3 = " + n3); int n4 = 38; int n5 = n4 - n3; System.out.println("n5 = " + n5);
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Homework02 {
public static void main(String[] args) { char c1 = '\n'; char c2 = '\t'; char c3 = '\r'; char c4 = '\\'; char c5 = '1'; char c6 = '2'; char c7 = '3'; System.out.println(c1); System.out.println(c2); System.out.println(c3); System.out.println(c4); System.out.println(c5); System.out.println(c6); System.out.println(c7); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Homework03 {
public static void main(String[] args) { String book1 = "天龙八部"; String book2 = "笑傲江湖"; System.out.println(book1 + book2);
char c1 = '男'; char c2 = '女'; System.out.println(c1 + c2);
double price1 = 123.56; double price2 = 100.11; System.out.println(price1 + price2); } }
|
![image-20220508220036706](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class Homework04 {
public static void main(String[] args) {
String name = "jack"; int age = 20; double score = 80.9; char gender = '男'; String hobby = "打篮球"; System.out.println("姓名\t年龄\t成绩\t性别\t爱好\n" + name + "\t" + age + "\t" + score + "\t" + gender + "\t" + hobby); } }
|
![image-20220508220526449](https://cdn.jsdmirror.com/npm/mycpen-blog@0.0.0-eiatuounvz/img/loading.png)