1. 变量介绍
-
变量是程序的基本组成单位
-
概念
- 变量相当于内存中一个数据存储空间的表示,可以通过变量名可以访问到变量(值)。
-
变量使用
-
声明变量 int a;
-
赋值 a = 20;
public class Var01 { //编写一个main方法 public static void main(String[] args) { //声明变量 int a; a = 100; System.out.println(a); //还可以这样使用 int b = 800; System.out.println(b); } }
-
2. 变量快速入门
public class Var02 {
//编写一个main方法
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);
}
}
3. 变量使用注意事项
- 变量表示内存中的一个存储区域,不同的变量,类型不同,占用的空间大小不同
- 该区域有自己的名称(变量名)和类型(数据类型)
- 变量必须先声明,后使用,即有顺序
- 该区域的数据/值可以在同一类型范围内不断变化
- 变量在同一个作用域内不能重名
- 变量三要素:变量名、值、数据类型
public class VarDetail {
//编写一个main方法
public static void main(String[] args) {
//变量必须先声明,后使用, 即有顺序
int a = 50;//int
System.out.println(a);//50
//该区域的数据/值可以在同一类型范围内不断变化
//a = "jack"; //error
a = 88; //ok
System.out.println(a);//88
//变量在同一个作用域内不能重名
//int a = 77;//error
}
}
class Dog {
public static void main(String[] args) {
int a = 666;//对
}
}
4. 程序中 “+” 的使用
-
当左右两边都是数值型时,则做加法运算
-
当左右两边有一方为字符串,则做拼接运算
-
运算顺序,从左到右
System.out.println(100 + 98); //198 System.out.println("100" + 98); //10098 System.out.println(100 + 3 + "hello"); //103hello System.out.println("hello" + 100 + 3); //hello1003
5. 数据类型
- 基本数据类型
- 数值型
- byte[1] short[2] int[4] long[8] float[4] double[8]
- 字符型
- char[2]
- 布尔型
- boolean[1]
- 数值型
- 引用数据类型
- 类(class)
- 接口(interface)
- 数组([])
6. 基本数据类型转换
-
自动类型转换
-
char----->int----->long----->float----->double
-
byte----->short----->int----->long----->float----->double
public class AutoConvert { //编写一个main方法 public static void main(String[] args) { //演示自动转换 int num = 'a';//ok char -> int double d1 = 80; //ok int -> double System.out.println(num);//97 System.out.println(d1);//80.0 } }
//自动类型转换细节 public class AutoConvertDetail { //编写一个main方法 public static void main(String[] args) { //细节1: 有多种类型的数据混合运算时, //系统首先自动将所有数据转换成容量最大的那种数据类型,然后再进行计算 int n1 = 10; //ok //float d1 = n1 + 1.1;//错误 n1 + 1.1 => 结果类型是 double //double d1 = n1 + 1.1;//对 n1 + 1.1 => 结果类型是 double float d1 = n1 + 1.1F;//对 n1 + 1.1 => 结果类型是 float //细节2: 当我们把精度(容量)大 的数据类型赋值给精度(容量)小 的数据类型时, //就会报错,反之就会进行自动类型转换。 // //int n2 = 1.1;//错误 double -> int //细节3: (byte, short) 和 char之间不会相互自动转换 //当把具体数赋给 byte 时,(1)先判断该数是否在byte范围内,如果是就可以 byte b1 = 10; //对 , -128-127 // int n2 = 1; //n2 是int // byte b2 = n2; //错误,原因: 如果是变量赋值,判断类型 // // char c1 = b1; //错误, 原因 byte 不能自动转成 char // // //细节4: byte,short,char 他们三者可以计算,在计算时首先转换为int类型 byte b2 = 1; byte b3 = 2; short s1 = 1; //short s2 = b2 + s1;//错, b2 + s1 => int int s2 = b2 + s1;//对, b2 + s1 => int //byte b4 = b2 + b3; //错误: b2 + b3 => int // //boolean 不参与转换 boolean pass = true; //int num100 = pass;// boolean 不参与类型的自动转换 //自动提升原则: 表达式结果的类型自动提升为 操作数中最大的类型 //看一道题 byte b4 = 1; short s3 = 100; int num200 = 1; float num300 = 1.1F; double num500 = b4 + s3 + num200 + num300; //float -> double } }
-
-
强制类型转换
-
自动类型转换的逆过程, 将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符 ( ),但可能造成精度降低或溢出,格外要注意。
public class ForceConvert { //编写一个main方法 public static void main(String[] args) { //演示强制类型转换 int n1 = (int)1.9; System.out.println("n1=" + n1);//1, 造成精度损失 int n2 = 2000; byte b1 = (byte)n2; System.out.println("b1=" + b1);//造成 数据溢出 } }
public class ForceConvertDetail { //编写一个main方法 public static void main(String[] args) { //演示强制类型转换 //强转符号只针对于最近的操作数有效,往往会使用小括号提升优先级 //int x = (int)10*3.5+6*1.5;//编译错误: double -> int int x = (int)(10*3.5+6*1.5);// (int)44.0 -> 44 System.out.println(x);//44 char c1 = 100; //ok int m = 100; //ok //char c2 = m; //错误 char c3 = (char)m; //ok System.out.println(c3);//100对应的字符, d字符 } }
-
7. 基本数据类型和String类型的转换
public class StringToBasic {
//编写一个main方法
public static void main(String[] args) {
//基本数据类型->String
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->对应的基本数据类型
String s5 = "123";
//会在OOP 讲对象和方法的时候回详细
//解读 使用 基本数据类型对应的包装类,的相应方法,得到基本数据类型
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);//123
System.out.println(num2);//123.0
System.out.println(num3);//123.0
System.out.println(num4);//123
System.out.println(num5);//123
System.out.println(num6);//123
System.out.println(b);//true
//怎么把字符串转成字符char -> 含义是指 把字符串的第一个字符得到
//解读 s5.charAt(0) 得到 s5字符串的第一个字符 '1'
System.out.println(s5.charAt(0));
}
}
注意事项
-
在将 String 类型转成 基本数据类型时, ,比如 我们可以把 "123" , 转成一个整数,但是不能把 "hello" 转成一个整数
-
如果格式不正确,就会 抛出异常,程序就会终止, 这个问题在异常处理章节中,会处理
/** * 演示字符串转基本数据类型的细节 */ public class StringToBasicDetail { //编写一个main方法 public static void main(String[] args) { String str = "hello"; //转成int int n1 = Integer.parseInt(str); System.out.println(n1); } }
8. HomeWork
1.程序阅读,判断输出结果Homework01
public class Homework01 {
//编写一个main方法
public static void main(String[] args) {
int n1;
n1 = 13;
int n2;
n2 = 17;
int n3;
n3 = n1 + n2;
System.out.println("n3 = " + n3);//30
int n4 = 38;
int n5 = n4 - n3;
System.out.println("n5 = " + n5);//8
}
}
2.程序编写 Homework02
public class Homework02 {
//编写一个main方法
public static void main(String[] args) {
//编程,保存两本书名,用+拼接,看效果。保存两个性别,
//用加号拼接,看效果。保存两本书价格,用加号拼接,看效果
String book1 = "天龙八部";
String book2 = "笑傲江湖";
System.out.println(book1 + book2);//天龙八部笑傲江湖
//性别应该使用char保存
char c1 = '男';
char c2 = '女';
System.out.println(c1 + c2);//得到 男 字符码值 + 女 字符码值
//保存两本书价格
double price1 = 123.56;
double price2 = 100.11;
System.out.println(price1 + price2);//就是 123.56+100.11
}
}
3.程序编写Homework03
public class Homework03 {
//编写一个main方法
public static void main(String[] args) {
/*
姓名 年龄 成绩 性别 爱好
xx xx xx xx xx
要求:
1) 用变量将姓名、年龄、成绩、性别、爱好存储
2) 使用+
3) 添加适当的注释
4) 添加转义字符, 使用一条语句输出
*/
//姓名
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);
}
}