Java_01 基础:标识符,数据类型,运算,if,switch,for,while,数组
Java基础
本章主要介绍Java标识符、数据类型、运算符、判断 if及switch语句、for循环及while循环、数组等。
Java标识符
标识符命名规则:
-
通用规则:标识符可以是字母、数字、下划线_、$符号,但不能以数字开头,不能是Java的关键字以及保留字;
-
包名(package):反域名形式,类似com.alpari.test,字母小写;
-
类名(class):首字母大写,驼峰命名,类似MainClass,类里面一般为属性和方法;
-
方法及变量名:首字母小写,其余每个单词首字母大写,类似addUser(),userName;
关键字保留字 abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return strictfp short static super switch synchronize this throw throws transient try void volatile while
数据类型
Java中数据类型分为基本数据类型(8个)和引用数据类型:
- 基本数据类型(primitive type)
基本数据类型 | 字节数 | 基本数据类型 | 字节数 |
---|---|---|---|
byte(整型) | 1 | short(整型) | 2 |
int(整型) | 4 | long(整型) | 8 |
boolean(布尔) | 1 | char(字符) | 2 |
float(浮点型) | 4 | double(浮点型) | 8 |
-
引用数据类型(reference type)
枚举、类、数组、接口
-
基本数据类型的转换
变量的声明:在Java中,数据是属于强类型定义的,所有数据必须先定义类型才可以使用。
package com.alpari; public class Hello { public static void main(String[] args) { System.out.println("Hello World!");//这是简单的打印输出 // java中注释 这是单行注释 /* 这是多行注释 */ int a = 256; //数据类型 变量名 = 赋值; float c = 2.11F; //float类型后面要加上F或f代表,强烈建议为F long d = 2783787483454L; //同样定义long类型,后面加L或l,强烈建议L double f = c; //类型小的自动转为大的 char e = '瑞'; short g = 32767; byte b = (byte)(g); //强制转换为byte类型,强制转换时会出现内存溢出的问题 System.out.println(g); //32767 System.out.println(b); //-1 } }
基本数据运算
package com.alpari;
public class Hello {
public static void main(String[] args) {
//数值运算:java中运算符一般分为+ - * / %
int a = 10;
int b = 20;
int c = 19;
int d = 10;
int a1 = 10;
int d1 = 10;
System.out.println(a+b); //30
System.out.println(b/c); //1 除法运算如果有一个为浮点型,若int接收,小数后精度全失,只要有一个是小数即可
System.out.println((double)b/c); //1.0526315789473684
System.out.println(b%c); //1 取余数即 20 / 19 = 1...1
System.out.println(d==(++a)); //false
System.out.println(d1==(a1++)); //true
/* 对于++num和num++或者--num和num--:
++num是先对num进行加 1再进行运算,而num++是先运算然后再对num加 1
*/
System.out.println(d);
System.out.println(a);
//逻辑运算:&&与 ||或
if (1==1 && 1>2) {
System.out.println("两边都成立才会执行。");
} else {
System.out.println("有一个不成立则执行。"); //这里执行
}
if (1==1 || 1>2) {
System.out.println("两边有一个成立则执行。");//这里执行
} else {
System.out.println("都不成立则执行。");
}
}
}
逻辑运算符(位运算) |
---|
&&与&:&&如果左边条件不成立则不执行右边代码,而&左边不成立依然执行右边代码 |
||与|:||如果左边条件成立则不执行右边代码,而|左边成立依然执行右边代码 |
三元运算 |
---|
条件?“条件为真执行”:“条件为假执行” |
package com.alpari;
//三元运算 条件?"条件为真执行":"条件为假执行"
public class Hello {
public static void main(String[] args) {
int a = 10;
int b = 10;
String c = a>b?"相等":"不等";
System.out.println(c); //不等
}
}
if及switch判断
- if语法
if 判断语法:if里面依然可以嵌套if,但不建议这样做,可读性不好
if (boolean表达式a) {
boolean表达式a 成立执行的代码
} else if (boolean表达式b) {
boolean表达式b 成立执行的代码
} else {
都不成立执行的代码
}
package com.alpari;
public class Hello {
public static void main(String[] args) {
int a = 80;
if (a >= 80) {
System.out.println("优秀"); //执行 a>=80执行
} else if (a >= 70) {
System.out.println("良好"); //80>a>=70执行
} else {
System.out.println("需要努力"); //a<70执行
}
}
}
- switch语法
switch (值) {
case value:
//当value等于值时执行代码
break; // break打断
default:
//不等时执行
break;
}
package com.alpari;
public class Hello {
public static void main(String[] args) {
// 周一到周五上班,周末不上班,利用switch的穿透性
String a = "周五";
switch (a) {
case "周一":
case "周二":
case "周三":
case "周四":
case "周五":
System.out.println("上班");
break; // 打断,不然穿透性会一直往下执行
case "周六":
case "周日":
System.out.println("休息");
break;
}
}
}
if | switch |
---|---|
范围对比用if | 对比的值时固定的,不可范围对比,支持的数据类型:int、char、byte、short及其包装类,枚举、String(1.7);不支持的数据类型:long、float、double |
for与while循环
-
for语法
for (1.声明变量; 2.循环的条件; 4.改变循环条件 ) { 3.执行方法体 }
package com.alpari; public class Hello { public static void main(String[] args) { // 输出1加到100的和 int sum = 0; for (int i = 1; i <=100 ; i++) { sum += i; } System.out.println(sum); //5050 } }
package com.alpari; public class Hello { public static void main(String[] args) { // 输出1-100被5整除,每行3个 // 第一种:提供一个变量a来控制每行3个 int a = 0; for (int i = 1; i <= 100; i++) { if (i%5 == 0) { System.out.print(i + "\t"); a ++; } if (a==3) { System.out.println(); a = 0; } } // 第二种:每行3个,每个都是5的倍数,只要保证第三个是15的倍数就可以换行 for (int i = 1; i <=100 ; i++) { if (i%5==0) { System.out.print(i+"\t"); } if (i%(5*3)==0) { System.out.println(); } } } }
package com.alpari; public class Hello { public static void main(String[] args) { // 输出乘法口诀表 for (int i = 1; i <=9 ; i++) { for (int j = 1; j <= i; j++) { System.out.print(i +"*" + j +"="+ (i*j) +"\t"); } System.out.println(); // 控制换行 } } } 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
-
while语法
while (boolean表达式) { 表达式成立执行的代码 }
package com.alpari; public class Hello { public static void main(String[] args) { // 输出1-10 的和 int i = 0; int sum = 0; while (i<=10) { sum = sum +i; i++; } System.out.println(sum); // do{代码}while(条件),是至少执行一次的循环 int a = 1; do{ System.out.println("执行一次");//执行 }while (a>1); while (a>1) { System.out.println("可否执行?");//不执行 } } }
-
for与while的区别
对于循环次数确定的用for循环,对于循环次数不确定的用while,do while是先执行代码,再判断循环条件,至少执行一次。
数组
数组我们可以理解为一个大盒子,里面存放一个个元素,数组的创建方式有2种:
-
静态初始化:类型[] 变量名 = {“元素”,“元素"};
-
动态初始化:类型[] 变量名 = new 类型[n]; //n代表里面元素的个数
package com.alpari; import java.util.Arrays; public class Hello { public static void main(String[] args) { int[] arr = {1,2,3}; // 静态初始化 int[] arr2 = new int[3]; // 动态初始化 arr2[0] = 1; //给数组赋值,数组初始值 数字默认为0,字符串默认为null arr2[1] = 2; arr2[2] = 3; System.out.println(Arrays.toString(arr));//Arrays.toString(arr)为工具类方法,打印输出 System.out.println(Arrays.toString(arr2)); } } // 输出: [1, 2, 3] [1, 2, 3]
-
数组的缺点:类型比较单一,且一旦创建无法改变数组的长度
-
数组的遍历
package com.alpari; import java.util.Arrays; public class Hello { public static void main(String[] args) { int[] arr = {1,2,3}; // 第一种遍历方式:数组重要属性.length,获取数组的长度,数组的下标从0开始 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } System.out.println("-----------------"); // 第二种遍历方式:增强for循环 for (int a:arr) { System.out.println(a); } } } // 输出: 1 2 3 ----------------- 1 2 3
-
二维数组(多维数组):在数组里面嵌套数组
package com.alpari; import java.util.Arrays; public class Hello { public static void main(String[] args) { int[][] arr = {{1,2},{3,4}}; // 双增强for循环遍历 for (int[] arr1 : arr) { for (int arr2: arr1) { System.out.println(arr2); } } } } // 输出: 1 2 3 4
排序
常用的冒泡与选择
-
冒泡排序:两个相邻的数进行比较,左边>右边,则换位,依次循环
package com.alpari; import java.util.Arrays; public class Hello { public static void main(String[] args) { int[] arr = {1,4,6,3,5}; for (int i = 0; i < arr.length-1; i++) { for (int j = 0; j < arr.length-1-i; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } System.out.println(Arrays.toString(arr)); } } // 输出: [1, 3, 4, 5, 6]
-
选择排序:// 让下标为0位值与后面所有值进行比较,如果大于其他下标的值,则互换
package com.alpari; import java.util.Arrays; public class Hello { public static void main(String[] args) { int[] arr = {1,4,6,3,5}; for (int i = 0; i < arr.length-1; i++) { int a = i; // 定义一个变量a来存放最大值的下标 for (int j = a+1; j < arr.length; j++) { if (arr[j] > arr[a]) { // 让下标j的值与下标a的值比较,记录下大的值得下标 a = j; // 让值大的下标赋给a } } if (a!=i) { // 来判断最大值的下标是否是i,如果不是,则互换值 int temp = arr[i]; arr[i] = arr[a]; arr[a] = temp; } } System.out.println(Arrays.toString(arr)); } } // 输出: [6, 5, 4, 3, 1]
-
快速排序:利用递归进行快速排序,速度较快,但是容易造成栈内存溢出,最好不用。