一、原码反码补码
原码、反码、补码
正数:
原码:符号位不变,其他位取绝对值
反码、补码:都是原码
负数:
原码:符号位不变,其他位取绝对值
反码:符号位不变,其余位取反
补码:反码 + 1
二、字符——字符串——布尔
public class Test02 {
public static void main(String[] args) {
// 字符型
char c1 = 'a';
char c2 = 'B';
char c3 = '1';
char c4 = '中';
// 字符串
String s1 = "我是字符串";
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2);
System.out.println("c3 = " + c3);
System.out.println("c4 = " + c4);
System.out.println("s1 = " + s1);
}
}
public class Test03 {
public static void main(String[] args) {
// 布尔型 true真 false假
boolean f1 = true;
boolean f2 = false;
System.out.println(f1);
System.out.println(f2);
}
}
三、基础运算符
Java 语言支持如下运算符:
算术运算符: +,-,*,/,%,++,--
赋值运算符 =
关系运算符: >,<,>=,<=,==,!=, instanceof
逻辑运算符: &&,||,!
位运算符: &,|,^,~ , >>,<<,>>>
扩展赋值运算符:+=,-=,=,/=
public class Test04 {
public static void main(String[] args) {
int result = 5 / 2;
System.out.println("result = " + result);
// 自增、自减
// ++|--在前,先自增或自减再参与运算
// ++|--在后,先参与运算再自增或自减
int num = 3;
result = 2 + num++;
System.out.println("result = " + result);
// 逻辑运算符
// & 与|且(全真为真true,否则为假false)
boolean flag = 3 > 5 & 2 < 5;
//System.out.println(3 > 5);
System.out.println("flag = " + flag);
// 分析:左边如果为假,右边无论是真假结果都是假,使用短路与优化
int i;
flag = 3 > 5 && 2 < i;
System.out.println("flag = " + flag);
// | 或(有真为真,否则为假)
flag = 3 < 5 | 2 > 5;
System.out.println("flag = " + flag);
// 分析:左边如果为真,右边无论是真假结果都是真,使用短路或优化
flag = 3 < 5 || 2 > i;
System.out.println("flag = " + flag);
// ! 非,取反(真为假,假为真)
System.out.println(!true);
// ^ 异或
int x = 2; // 密码原文
int y = 8; // 加密因子
result = x ^ y;
System.out.println("result = " + result);
}
}
四、三目运算符
三目条件运算符,语法格式
x ? y : z
其中 x 为 boolean 类型表达式,先计算 x 的值,若为true,则整个三目运算的结果为表达式 y 的值,否 则整个运算结果为表达式 z 的值。
public class Test05 {
public static void main(String[] args) {
boolean flag = 3 > 5;
if (flag) {
System.out.println("条件表达式为true则执行我");
} else {
System.out.println("条件表达式为false则执行我");
}
// 三目运算符
String result = flag ? "条件表达式为true则执行我" : "条件表达式为false则执行我";
System.out.println(result);
}
}
五、类型转换
自动类型转换:容量小的数据类型可以自动转换为容量大的数据类型。在图中,黑色的实线表示无 数据丢失的自动类型转换,而红色的虚线表示在转换时可能会精度的损失。
特例: 可以将整型常量直接赋值给byte, short, char等类型变量,而不需要进行强制类型转换,只要不超出其表数范围。
强制类型转换(Cast):强制类型转换,又被称为造型,用于显式的转换一个数值的类型. 在有可 能丢失信息的情况下进行的转换是通过造型来完成的,但可能造成精度降低或溢出。
强制类型转换的语法格式: (type)var ,运算符“()”中的type表示将值var想要转换成的目标数据类型。
public class Test06 {
public static void main(String[] args) {
// 类型转换
// 小转大,自动转
byte b = 2;
int i = b;
System.out.println(i);
// 大转小,强制转
i = 300;
b = (byte) i; // int类型强制转换为byte
System.out.println(b);
// char与整数
char c1 = 'a';
char c2 = 'A';
System.out.println(c1 + 0);
System.out.println(c2 + 0);
// 当运算数值过大时,必须提前声明使用Long类型
int num = 1000000000; // 十亿
long l = num * (long) 10; // 式子中有long类型数值,则不会按照int规则去运算
System.out.println(l); // 1410065408
}
}
六、Scanner对象
键盘录入可以允许用户在程序运行过程中向程序输入数据,达到人机交互的效果,提高用户体验度。 但是需要进行一些额外的操作,后面会具体解释
-
导包 ( import java.util.Scanner; ) 告诉程序这个工具在何处
-
创建对象( Scanner sc = new Scanner(System.in); ) 创建工具
-
获取数据 ( int num = sc.nextInt(); ) 使用工具获取数
// 第一步导包,寻找资源
import java.util.Scanner;
public class Test07 {
public static void main(String[] args) {
// 键盘录入对象
// 类和对象的关系,类是抽象的,对象是具体的
// 对象是通过new关键字来定义
// 第二步:new对象(new+无参构造方法)
// 需要调用有参构造方法,实现键盘监听录入
Scanner scan = new Scanner(System.in);
// 第三步:调用方法
// nextInt(),监听键盘输入的整数,不是整数会报错
// 程序阻塞等待键盘输入,输入以后回车结束,并接收返回值
System.out.print("请输入一个整数:");
int result = scan.nextInt();
System.out.println("您输入的整数为:" + result);
}
}
七、练习Scanner
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
// 第二步创建 Scanner 对象,调构造方法(无参、有参)
Scanner scan = new Scanner(System.in);
// 第三步调用方法 nextInt 返回整数
System.out.print("请输入一个整数");
int result = scan.nextInt();
System.out.println("您输入的整数为:" + result);
// next 返回的是字符串
System.out.print("请输入一个字符串:");
String s = scan.next();
System.out.println("您输入的字符串为:" + s);
}
}
八、练习Math
public class MathDemo {
public static void main(String[] args) {
// public static 全项目公开,不需要new对象即可直接访问
// random 不需要入参,返回值是 double 类型数值,大小范围[0,1)左闭右开,左边包含右边不包含
double result = Math.random();
System.out.println("result = " + result);
}
}
九、选择结构if
import java.util.Scanner;
public class IfDemo {
public static void main(String[] args) {
// 单选择
/*
语法格式:条件表达式为true,则执行语句体
if (条件表达式) {
语句体;
}
*/
// 双选择
/*
语法格式:条件表达式为true,则执行语句体1,否则执行语句体2
if (条件表达式) {
语句体1;
} else {
语句体2;
}
*/
// 多选择
/*
语法格式:条件表达式为true,则执行语句体1,否则继续判断,条件为false继续下一个
if (条件表达式) {
语句体1;
} else if {
语句体2;
} else if {
语句体3;
} ... {
语句体n;
} else {
}
*/
// 成年18岁,才可以谈对象,否则好好学习
// 使用键盘录入配合练习
Scanner scan = new Scanner(System.in);
System.out.print("是否成年,请输入您的年龄:");
int age = scan.nextInt();
if (age >= 18) {
System.out.println("继续浏览");
} else if (age < 0) {
System.out.println("还未出生");
} else {
System.out.println("好好学习");
}
}
}
十、switch选择
import java.util.Scanner;
public class SwitchDemo {
public static void main(String[] args) {
/*
语法格式:
switch (1) {
case 1:
语句体;
[break;]
case 2:
语句体;
default:
语句体;
}
*/
// 输入一个整数,打印对应的星期
Scanner scan = new Scanner(System.in);
// 如果输入的是 1 ~ 5,打印工作日,6 和 7 打印休息日,否则输入错误
System.out.print("请输入星期对应的数字:");
int week = scan.nextInt();
/*
if (week == 1) {
System.out.println("工作日");
} else if (week == 2) {
System.out.println("工作日");
} else if (week == 3) {
System.out.println("工作日");
} else if (week == 4) {
System.out.println("工作日");
} else if (week == 5) {
System.out.println("工作日");
} else if (week == 6) {
System.out.println("休息日");
} else if (week == 7) {
System.out.println("休息日");
} else {
System.out.println("输入错误");
}
*/
switch (week) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("工作日");
break;
case 6:
case 7:
System.out.println("休息日");
break;
default:
System.out.println("输入错误");
}
}
}
十一、while循环
public class WhileDemo {
public static void main(String[] args) {
/*
语法格式:如果条件表达式为true,则执行语句体,语句体执行结束继续判断条件表达式如此反复
while (条件表达式) { // 注意:条件表达式如果一直为true,则死循环
语句体;
}
先判断条件是否满足,满足则执行。
*/
int i = 5;
while (i < 5) {
System.out.println("i = " + i);
i++; // 自增处理
}
/*
do {
语句体;
} while (条件表达式);
至少执行一次,再判断条件是否满足。
*/
do {
System.out.println("i = " + i);
i++;
} while (i < 5);
}
}
十二、for循环
public class ForDemo {
public static void main(String[] args) {
/*
语法格式:
for (①定义初始变量; ②条件表达式; ④变量操作) {
③语句体;
}
执行步骤:
先执行第一步,初始化变量
再执行第二步判断条件表达式
如果条件表达式为true,则执行第三步语句体
如果条件表达式为false,则结束循环
第三步执行结束,执行第四步
第四步执行结束以后继续判断条件表达式
如果条件表达式为true,则执行第三步语句体
如果条件表达式为false,则结束循环
...
*/
for (int i = 0; i <= 5; i++) {
// System.out.println("i = " + i);
}
// 九九乘法表
// 外层循环控制行数,一共 9 行
for (int x = 1; x <= 9; x++) {
// 内层循环控制列数,每一列打印的个数和行数相等,所以条件表达式 <= 行
for (int y = 1; y <= x; y++) {
System.out.print(y + " * " + x + " = " + y * x + "\t");
}
System.out.println();
}
// 死循环
for (; ; ) {
System.out.println("死循环");
}
}
}
十三、break和continue
continue 语句用在循环语句体中,用于终止 某次循环过程 ,即跳过循环体中尚未执行的语句,接着进 行下一次是否执行循环的判定。
在任何循环语句的主体部分,均可用 break 控制循环的流程。break用于 强行终止整个循环 ,不执行循环中剩余的语句。(break语句还可用于多支语句switch中)
例:随机生成若干[0,10]之间的整数,当生成到5的时候停止
注意:break用于终止整个循环,只是本层的循环
十四、String比较和Random对象
import java.util.Random;
public class StringDemo {
public static void main(String[] args) {
// 字符串比较是否相等
String str1 = "我是一个字符串";
String str2 = "我是一个字符串";
// equals 如果字符串相等返回true,否则false
if (str1.equals(str2)) {
System.out.println("字符串相等!!!");
}
}
}
public class RandomDemo {
public static void main(String[] args) {
// 第二步创建 random 对象
Random rand = new Random();
// 第三步调用方法,左闭右开整数(int范围),可以入参指定范围
int result = rand.nextInt(100);
System.out.println("result = " + result);
// 左闭右开双精度小数,[0,1)
double d = rand.nextDouble(); // 等同于 Math.random();
System.out.println("d = " + d);
}
}