JVM、JRE、JDK的关系
-
JDK(Java Development Kit)是整个 Java 的核心,是 java 开发工具包,包括 了 Java 运行环境 JRE、Java 工具和Java 基础类库。
-
JRE(Java Runtime Environment)是运行 JAVA 程序所必须的环境的集合,包 含 java 虚拟机和 java 程序的一些核心类库。
-
JVM 是 Java Virtual Machine(Java 虚拟机)的缩写,是整个 java 实现跨平台 的最核心的部分,能够运行以 Java 语言写作的软件程序。
数据类型
基本数据类型: 整型:byte-字节型 short-短整型 int-整型 long-长整型
浮点型 float-单精度浮点型 double-双精度浮点型
字符型 char
布尔型 boolean
引用数据类型 类class 接口interface 枚举 enum 注解@interface 数组[]
基本数据类型转换
隐式类型转换(小转大)
将取值范围小的类型
自动提升为取值范围大的类型
byte-> short(char)-> int-> long -> float-> double
public static void main(String[] args) {
// 隐式类型转换 : 直接转换
short n1 = 125;
int n2 = n1;
System.out.println(n2);
byte n3 = 34;
long n4 = n3;
System.out.println(n4);
byte n5 = 12;
double n6 = n5;
System.out.println(n6);
char n7 = 'A';
System.out.println(n7 + 2);
// 最终决定的还是数据类型
char n8 = 97;
System.out.println(n8);
int n9 = 'A'; // int
System.out.println(n9);
}
强制类型转换
将取值范围大的类型
强制转换成取值范围小的类型
数据类型 变量名 = (数据类型)被强转数据值;
public static void main(String[] args) {
// 强制类型转换:有可能数据丢失
int n1 = 45;
byte n2 = (byte) n1;
System.out.println(n2);
double n3 = 45.6;
int n4 = (int) n3;
System.out.println(n4);
// char比较特殊
char c1 = 'A';
c1 = (char) (c1 + 3);
System.out.println(c1);
System.out.println("-----基本数据类型 ---> String---------------");
// 基本数据类型 -> String
// 1. 拼接
int n5 = 45;
String s1 = "" + n5;
System.out.println(s1);
// 2. 调用String的静态方法valueOf
String s2 = String.valueOf(n5);
System.out.println(s2);
byte n6 = 45;
String s3 = String.valueOf(n6);
System.out.println(s3);
System.out.println("-----String ---> 基本数据类型---------------");
/*
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
*/
String s4 = "65";
System.out.println(s4 + 10);
int n7 = Integer.valueOf(s4);
System.out.println(n7 + 10);
byte n8 = Byte.valueOf(s4);
System.out.println(n8);
double n9 = Double.valueOf(s4);
System.out.println(n9);
// 值转换
char c2 =(char) (int) Integer.valueOf(s4);
System.out.println(c2);
// 通过索引,字符串获取字符
char c3 = s4.charAt(0); // 索引从0开始
System.out.println(c3);
}
基本数据类型与字符串类型的转换
value of
parse xxx
public static void main(String[] args) {
String str = "125";
Integer number1 = Integer.valueOf(str);
Integer number2 = Integer.parseInt(str);
Double number3=Double.parseDouble(str);
Byte number4=Byte.parseByte(str);
Short number5= Short.parseShort(str);
Long number6=Long.valueOf(str);
System.out.println(number1);
System.out.println(number2);
System.out.println(number3);
System.out.println(number4);
System.out.println(number5);
System.out.println(number6);
}
包装类
装箱:把基本数据类型转为包装类对象。
拆箱:把包装类对象拆为基本数据类型。
/*
数值型
Byte、Short、Integer、Long
Float,Double
Character 不是数值型
*/
public static void main(String[] args) {
// 自动装箱:基本类型转换为”对应“的包装类
int number1 = 100;
Integer number2 = number1; // 自动装箱
Long number3 = 1L * number1;
System.out.println(number2);
System.out.println(number3);
System.out.println("---------------------");
// 自动拆箱:包装类转换为”对应“的基本类型
Integer number4 = 100;
int number5 = number4;
Long number6 = number4 * 1L;
System.out.println(number5);
System.out.println(number6);
}
自加自减运算
变量前++
:变量先自身加1,然后再取值。
变量后++
:变量先取值,然后再自身加1。
public static void main(String[] args) {
int number1 = 10;
number1 ++;
System.out.println(number1);
int number2 = 10;
++ number2;
System.out.println(number2);
System.out.println("-----------------------");
int number3 = 10;
int number4 = number3 ++ ;
System.out.println(number3 + ":" +number4);
System.out.println("---------------------------");
int number5 = 10;
int number6 = ++ number5 + ++ number5;
/*
++ number5 + ++ number5 : 10
11 + ++ number5 : 11
11 + 12 : 12
23 : 12
*/
System.out.println(number5 + ":" + number6);
}
输出语句(Java基础)
System.out.println(输出内容);//输出内容之后,紧接着换行
System.out.print(输出内容);输出内容之后不换行
分支语句
if..elseif...else
public static void main(String[] args) {
String sex = "男";
int age = 12;
if(age < 0 ){
System.out.println("年龄异常");
} else if (age < 18) {
if(sex == "男"){
System.out.println("男孩");
}else{
System.out.println("女孩");
}
}else {
if(sex == "男"){
System.out.println("先生");
}else{
System.out.println("女士");
}
}
}
public static void main(String[] args) {
// 剪刀、石头、布
Scanner scanner = new Scanner(System.in);
System.out.println("请输入剪刀、石头、布");
String player = scanner.next();
System.out.println("玩家出:" + player);
int computer = (int)(Math.random() * 3);
if(computer == 0){
System.out.println("电脑出剪刀");
}else if(computer == 1){
System.out.println("电脑出石头");
}else{
System.out.println("电脑出布");
}
if(player.equals("剪刀")){
if(computer == 0){
System.out.println("平局");
}else if(computer == 1){
System.out.println("电脑赢了");
}else{
System.out.println("玩家赢了");
}
}
}
switch ...cace
public static void main(String[] args) {
System.out.println("请输入年:");
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
System.out.println("请输入月:");
int month = scanner.nextInt();
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31天");
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
System.out.println("29天");
}else {
System.out.println("28天");
}
break;
default:
if(month < 1 || month > 12){
System.out.println("月份异常");
}else{
System.out.println("30天");
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年:");
int year = scanner.nextInt();
System.out.println("请输入月:");
int month = scanner.nextInt();
System.out.println("请输入日:");
int day = scanner.nextInt();
// 12: 1 月 + 2 月 + 3 月 + 4 月 + 5 月 + 6 月 + 7 月 + 。。。 + 11月 + day
// 11: 1 月 + 2 月 + 3 月 + 4 月 + 5 月 + 。。。 + 10月 + day
// 7 : 1 月 + 2 月 + 3 月 + 4 月 + 5 月 + 6 月 + day
int total = 0;
switch (month - 1){
case 11:
total += 30;
case 10:
total += 31;
case 9:
total += 30;
case 8:
total += 31;
case 7:
total += 31;
case 6:
total += 30;
case 5:
total += 31;
case 4:
total += 30;
case 3:
total += 31;
case 2:
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
total += 29;
}else{
total += 28;
}
case 1:
total += 31;
default:
total += day;
}
System.out.println("总天数:" + total);
}
循环语句
for
public static void main(String[] args) {
String code = "";
for(int i = 0; i < 6; i++){
int random = (int)(Math.random() * 3);
switch (random){
case 0:
code += (char)((Math.random() * 26) + 'A');
break;
case 1:
code += (char)((Math.random() * 26) + 'a');
break;
default:
// code += (int)(Math.random() * 10);
code += (char)((Math.random() * 10) + '0');
}
}
System.out.println("当前验证码为:"+code +" 请输入验证码:");
Scanner scanner = new Scanner(System.in);
String code2 = scanner.next();
// equalsIgnoreCase比较时区分大小写
if(code.equalsIgnoreCase(code2)){
System.out.println("验证码正确");
}else{
System.out.println("验证码错误");
}
}
while
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int week=0;
while (true){
System.out.println("请输入星期数:");
week = scanner.nextInt();
if(week >= 1 && week <=7){
break;
// break后面不能写代码
}
System.out.println("输入错误,请重新输入");
}
System.out.println("--------------循环结束 " +"星期"+ week);
}
do...whlie
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// for 指定次数
// while / do~ while 不确定次数
/*
for / while : 有可能一次都不执行,需要考虑死循环
do~while 至少执行一次
*/
int player = 0;
int computer = 0;
do{
System.out.println("请输入结果:"); // 胜利,失败,平局
String result = scanner.next();
if(result.equals("胜利")){
player ++;
}else if(result.equals("失败")){
computer ++;
}
}while (player < 2 && computer < 2);
if(player > computer){
System.out.println("玩家胜利");
}else {
System.out.println("电脑胜利");
}
}
public static void main(String[] args) {
// 随机生成一个100以内的数,猜这个随机数是多少?
Scanner scanner = new Scanner(System.in);
int random = (int) (Math.random() * 100); // 0 ~ 99
int num = 0;
int count = 0;
System.out.println(random); // 开挂了!!!
do{
System.out.println("请" + (count + 1) + "次输入数字:");
num = scanner.nextInt();
if(num > random){
System.out.println("猜大了");
}else if(num < random){
System.out.println("猜小了");
}
count ++;
}while (num != random && count < 3);
if(num == random){
System.out.println("猜对了");
}else{
System.out.println("猜错了,答案是:" + random);
}
}
break continue return
public static void main(String[] args) {
// break 语句 : 破坏当前的结构
// continue 语句 : 跳过当前的循环,执行下次循环
// return 语句 : 退出当前方法
System.out.println("---------------循环开始");
for(int i = 0 ; i < 10 ; i++){
if (i == 5){
// break;
// continue;
return;
}
System.out.println("i:" + i);
}
System.out.println("------------------循环结束");
}
数组
数据类型[] 数组名 = {元素1,元素2,元素3...};
数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3...};
数组存储的元素的数据类型[] 数组名字 = new 数组存储的元素的数据类型[长度];
public static void main(String[] args) {
int [] sum1={1,2,3};
int [] num2=new int[]{1,2,3,4};
int [] sum=new int[6];
}
public static void main(String[] args) {
// 输入一个数组,装了5个学生的成绩
Scanner scanner = new Scanner(System.in);
int [] scores = new int[5];
for(int i = 1 ; i <= scores.length ; i++){
System.out.println("请输入第" + i + "个学生的成绩:");
int score = scanner.nextInt();
scores[i-1] = score;
}
System.out.println(Arrays.toString(scores));
}
顺序查找
public static void main(String[] args) {
// 顺序查询
int [] arrs = {2,76,45,23,17,11,34};
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要查询的值:");
int number = scanner.nextInt();
int index = -1;
for(int i = 0 ; i < arrs.length ; i++){
if( number == arrs[i]){
index = i;
break;
}
}
if(index == -1){
System.out.println("没有找到");
}else{
System.out.println("找到了,下标为:"+index);
}
}
二分查找
public static void main(String[] args) {
// 二分查询 排好序的
int [] arr = {2,4,7,11,15,19,23,28,32,36,45,56,72,98,100,101,104,107};
System.out.println("请输入查询的数字:");
int number = new Scanner(System.in).nextInt();
int leftIndex = 0;
int rightIndex = arr.length - 1;
int index = -1;
do{
int mid = (leftIndex + rightIndex) / 2;
System.out.println("当前找:(" + arr[mid] +"," + number + ")");
if(number == arr[mid]){
index = mid;
}else if(number > arr[mid]){
leftIndex = mid + 1;
}else{
rightIndex = mid - 1;
}
}while (index == -1 && leftIndex <= rightIndex );
if(index == -1){
System.out.println("没有找到");
}else{
System.out.println("找到了,下标为:" + index);
}
}
冒泡排序
public static void main(String[] args) {
// flag 记录是否有交换
boolean flag = true;
int [] arrs = {10,1,2,3,4,5,6,7,8,9};
// 循环的时候如果没有交换就停止
for(int i = 0; i < arrs.length-1 && flag ; i++){
// 刚开始的时候设置为false
flag = false;
for(int j = 0 ; j < arrs.length - i-1 ; j++){
int n1 = arrs[j];
int n2 = arrs[j + 1];
System.out.println("(" + n1 + "," + n2 + ")");
if(n1 > n2){
arrs[j] = n2;
arrs[j + 1] = n1;
flag = true; // 有交换
}
}
System.out.println("------------------------" + Arrays.toString(arrs));
}
System.out.println(Arrays.toString(arrs));
}