Java基础语法
1. 类型转换
1.1 自动类型转换
1.2 表达式的自动类型转换
public class ClassDemo {
public static void main(String[] args) {
//表达式的自动类型转换
byte a = 10;
int b = 20;
double c = 1.0;
int rs1 = a + b;
double rs2 = a + b + c;
System.out.println(rs1); //30
System.out.println(rs2); //31.0
//面试题
byte i = 10;
byte j = 20;
//byte k = i + j; //报错,因为i和j默认是int来运算
int k = i + j;
System.out.println(k); //30
}
}
1.3 强制类型转换
2. 运算符
2.1 基本算数运算符
2.2 +符号做连接符
2.3 自增自减运算符
//面试题
public class Operator {
public static void main(String[] args) {
int k = 3;
int p = 5;
//k 3 4 5 4
//p 5 4 3 4
//rs 3 + 5 - 4 + 4 - 5 + 4 + 2
int rs = k++ + ++k - --p + p-- - k-- + ++p + 2;
System.out.println(k); //4
System.out.println(p); //4
System.out.println(rs); //9
}
}
2.4 赋值运算符
2.5 关系运算符
2.6 逻辑运算符
2.7 三元运算符
//面试题
public class Operator {
public static void main(String[] args) {
int i = 10;
int j = 30;
int k = 50;
int temp = i > j ? i : j;
int rsMax = temp > k ? temp : k;
System.out.println(rsMax); //50
}
}
2.8 运算符优先级
3. 键盘录入技术及API
4. 程序流程控制
4.1 顺序结构
4.2 分支结构
4.2.1 if (适合区间匹配)
4.2.2 switch (适合值匹配)
4.3 循环结构
4.3.1 for循环
public class ForLoop {
public static void main(String[] args) {
for (int i = 100; i <= 999 ; i++) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if((ge*ge*ge + shi*shi*shi + bai*bai*bai) == i){
System.out.print(i+"\t"); //153 370 371 407
}
}
}
}
4.3.2 while循环
4.3.3 do-while循环
4.3.4 死循环
import java.util.Scanner;
public class DeadLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int okPassword = 520;
while(true){
System.out.println( "请输入正确密码:");
int password = sc.nextInt();
if(password == okPassword){
System.out.println("成功了");
break;
}else{
System.out.println("密码错误");
}
}
}
}
4.3.5 循环嵌套
4.3.6 跳转关键字:break、continue
5. 随机数Random类
import java.util.Random;
import java.util.Scanner;
public class RandomTest {
public static void main(String[] args) {
Random r = new Random();
int luckNumber = r.nextInt(100) + 1;
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请您输入数据(1-100):");
int guessNumber = sc.nextInt();
if(guessNumber > luckNumber){
System.out.println("过大");
}else if(guessNumber < luckNumber){
System.out.println("过小");
}else{
System.out.println("答对了");
break;
}
}
}
}
6. 数组
6.1 数组的定义
6.1.1 静态初始化数组
6.1.2 数组的访问
注意事项:
6.1.2 动态初始化数组
6.2 数组的遍历
6.3 数组的案例
6.3.1 数组元素求和
6.3.2 数组求最值
6.3.3 猜数字游戏
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] data = new int[5];
Random r = new Random();
for (int i = 0; i < data.length; i++) {
data[i] = r.nextInt(20) + 1;
}
Scanner sc = new Scanner(System.in);
OUT:
while(true){
System.out.println("请您输入一个1-20之间的整数:");
int guessData = sc.nextInt();
for (int i = 0; i < data.length; i++) {
if(data[i] == guessData){
System.out.println("您已经猜中了该数据,其索引值是:" + i);
break OUT; //结束整个死循环
}
}
System.out.println("猜的数据在数组中不存在,请重新猜测");
}
for (int i = 0; i < data.length; i++) {
System.out.println(data[i] + "\t");
}
}
}
6.3.4 随机排名
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] codes = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < codes.length; i++) {
System.out.println("请您输入" + (i+1) + "个员工的工号:");
int code = sc.nextInt();
codes[i] = code;
}
Random r = new Random();
for (int i = 0; i < codes.length; i++) {
int index = r.nextInt(codes.length);
int temp = codes[index];
codes[index] = codes[i];
codes[i] = temp;
}
for (int i = 0; i < codes.length; i++) {
System.out.println(codes[i] + "\t");
}
}
}
6.3.5 数组排序
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] arr = {5, 2, 3, 1};
for (int i = 0; i < arr.length - 1; i++) {
//i=0 比较次数 3 j = 0 1 2
//i=1 比较次数 2 j = 0 1
//i=2 比较次数 1 j = 0
for (int j = 0; j < arr.length - i - 1; j++) {
if(arr[j] > arr[j+1]){
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + "\t");
}
}
}
6.4 数组的内存图
示例:
两个数组变量指向同一个数组对象:
6.5 数组使用常见问题
7. Debug
8. 方法
8.1 方法定义、调用
8.1.1 完整版
8.1.2 其他方式
8.2 方法使用常见问题
8.3 方法案例
8.3.1 判断整数是奇数还是偶数
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
check(11);
check(100);
}
public static void check(int num){
if(num % 2 == 0){
System.out.println(num + "是偶数");
}else{
System.out.println(num + "是奇数");
}
}
}
8.3.2 数组求最值
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] ages = {23, 19, 25, 78, 34};
int max = getMax(ages);
System.out.println("最大值是:" + max);
}
public static int getMax(int[] arr){
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
}
8.4 方法调用的内存图
另一个例子:
8.5 方法的参数传递机制
8.5.1 基本类型的参数传递
8.5.2 引用类型的参数传递
8.6 方法的参数传递案例
8.6.1 打印数组内容
public class Test {
public static void main(String[] args) {
int[] ages = {10, 20, 30, 40};
printArray(ages);
int[] numbers = {1, 2, 3, 4, 5};
printArray(numbers);
}
public static void printArray(int[] arr){
System.out.println("[");
if(arr != null && arr.length > 0) {
for (int i = 0; i < arr.length; i++) {
/*
if(i == arr.length - 1){
System.out.println(arr[i]);
}else{
System.out.println(arr[i] + ",");
}
*/
System.out.println(i == arr.length - 1 ? arr[i] : arr[i] + ",");
}
}
System.out.println("]");
}
8.6.2 从数组中查询元素的索引返回
public class Test {
public static void main(String[] args) {
int[] ages = {10, 20, 30, 40};
int index = searchIndex(ages, 20);
System.out.println(index);
}
public static int searchIndex(int[] arr, int data){
for (int i = 0; i < arr.length; i++) {
if(arr[i] == data){
return i;
}
}
return -1;
}
}
8.6.3 比较两个数组内容是否一样
public class Test {
public static void main(String[] args) {
int[] arr1 = {10, 20, 30, 40};
int[] arr2 = {10, 20, 30, 40};
System.out.println(compare(arr1, arr2));
}
public static boolean compare(int[] arr1, int[] arr2){
if(arr1.length == arr2.length){
for (int i = 0; i < arr1.length; i++) {
if(arr1[i] != arr2[i]){
return false;
}
}
return true;
}else{
return false;
}
}
}
8.7 方法重载
8.8 return关键字单独使用
9. 包
10. 权限修饰符