软件园学生在线

  • {{ item.name }}
  • 2023试用期

登录与注册

# 后端零 者亚杰

  • 者亚杰
  • 2023-10-15
  • 1

一、基础知识

1.JDK
开发者工具包。包括开发工具和JRE
2.JRE
运行环境。包括JVM和核心类库
3.JVM
虚拟机。可以实现跨平台。识别.class文件
4. .java和 .class
写的java程序是.java文件。通过javac编译成.class文件

二、基本语法

(1)注释

注释不会被编译

单行注释

// 此处为注释内容
// 一直单行注释也可以达到多行注释的效果

多行注释

/ *
注释
注释
可以一直换行
*/

可以生成到帮助文档的注释

/**
*注释
*注释
*注释
*/

2.标识符
(1)命名规则
数字、美元符、下划线、字母
不能有空格
第一个不能为数字
不能为关键字和保留字
严格区分大小写

(2)命名规范
驼峰式
类名、接口名:首字母大写,后面每个单词首字母大写
变量名、方法名:首字母小写,后面每个单词首字母大写
常量名:全部大写,单词间用下划线连接

(3)变量
在内存中开辟空间,用来存放数据
必须先声明再使用

3.数据类型
整型(默认类型为int)
用来声明整数变量
byte:默认值 0,包装类 Byte
short:默认值 0,包装类 Short
int:默认值 0,包装类 Integet
long:默认值 0,包装类 Long。用的时候加L或l
例:

byte b = 1;
short s = 1;
int i = 1;
long l = 1L;

浮点型(默认为double)
用来声明小数变量
float:默认值 0.0,包装类 Float。用的时候加F或f
double:默认值 0.0,包装类 Double
例:

float f = 0.9F;
double d = 0.9;

布尔型
boolean:默认值 false,包装类 Boolean。true或false

char型
用来声明字符变量
char:默认值\u0000,包装类 Character。

char c= 'a';
char c2 = 'A';
char c3 = ' ';//此处为空格

4.运算符
(1)算术运算符
加(+)、减(-)、乘(*)、除(/)、取余(%)、自增(++)、自减(--)

int a = 5;
int b = 3;
System.out.println(a + b);// 8
System.out.println(a - b);// 2
System.out.println(a * b);// 15
System.out.println(a % b);// 2
//自增、自减有优先级
System.out.println(a++);// 5。先打印在加1
System.out.println(a);// 6
System.out.println(++b);// 4。先加1再打印

(2)关系运算符
相等(==)、不等(!=)、大于(>)、小于(=)、大于或等于(>=)、小于或等于(<=)
输出值为布尔类型

System.out.println(1 == 2);// false
System.out.println(1 == 1);// true
System.out.println(1 != 2);// true
System.out.println(1 != 1);// false
System.out.println(1 > 2);// false
System.out.println(1 = 2);// false
System.out.println(1 <= 2);// true

(3)逻辑运算符

// & 与 有假则假-----&& 短路与 若左边为false,右边则不执行
// | 或 有真则真-----|| 短路或 若左边为true,右边则不执行
// ! 非 取反,!true == false, !false == true
// ^ 异或 前后条件的结果相同为false,前后结果不同为true
System.out.println(true & true);// true
System.out.println(true & false);// false
System.out.println(true | true);// true
System.out.println(true | false );// true
System.out.println(false | false );// false
System.out.println(!true);// false
System.out.println(!false);// true
System.out.println(true ^ true);// false
System.out.println(false ^ false);// false
System.out.println(true ^ false);// true
System.out.println(false ^ true);// true

(4)赋值运算符

// = 将右边的东西赋值给左边
// += a += b ===> a = a + b
// -=
// *=
// /=
// %=  同理

5.控制语句
选择
1.if

if(condition){
    //代码。condition为true时运行
}

2.if-else

if(condition){
    //代码。condition为true时运行
}else{
    //代码。condition为false时运行
}

3.if-else if

if(condition){
    //代码。condition为true时运行
}else if(condition2){
    //代码。condition2为true时运行
}else if(condition3){
    //代码。condition3为true时运行
}else{
    //代码。以上都为false时运行
}

4.switch
缺少break会有穿透效果,可以利用

//可以直接定位到表达式等于的那个case,减少运算
switch(表达式){
    case 值1:
    // 代码。表达式等于值1时进入
    break;
    case 值2:
    // 代码。表达式等于值2时进入
    break;
    case 值3:
    // 代码。表达式等于值3时进入
    break;
    default:
    // 代码
    break;
}

区间用if,值用switch

循环
1.for

// 打印五行666
for (int i = 0; i < 5; i ++){
    System.out.println(666);
}
// 1-100的和
int sum = 0;
for (int i = 1; i <= 100; i++) {
    sum += i;
}
System.out.println(sum);

2.while

while(condition){
    //代码。condition为true进入循环
}

3.do-while

do{
    //代码
}while(condition);
// 无论condition是否为true,都会运行一次

6.方法

public class MethodDemo1 {
    public static void main(String[] args) {
    int rs = sum(12,33);
        System.out.println(rs);
    }
        // 这是一个求和方法
    public static int sum(int a, int b){
        int c = a + b;
        return c;
    }
    // 无返回值用void,以下是一个打印方法
    public static void printHelloWorld(int n){
        for (int i = 1; i <= n; i++) {
            System.out.println("HelloWorld");
        }
}

7.数组
静态数组

public class ArrayDemo1 {
    public static void main(String[] args) {
        int[] ages = new int[]{12,24,36};
        double[] scores = new double[]{89.9,99.5,59.5,88};
        // 简化写法
        int[] ages2 = {12,24,36};
        double[] scores2 = {89.9,99.5,59.5,88};
        // 另一种写法,一般不建议用
        int ages3[] = {12,24,36};
        double scores3[] = {89.9,99.5,59.5,88};
        System.out.println(ages);
        System.out.println(scores);
    }
}
public class ArrayDemo2 {
    public static void main(String[] args) {
        int[] arr = new int[]{12,24,36};
                            //0   1  2
        // 数组的访问
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);

        // 修改数组中的数据
        arr[0] = 66;
        arr[2] = 100;
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);

        //遍历数组中的数据
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        // 访问数组的元素个数,数组名.length
        System.out.println(arr.length);
    }
}

动态数组(案例)

public class  ArrayDemo5 {
    public static void main(String[] args) {
        // 评委打分案例
        // 定义一个动态初始化数组,负责后期存储6个评委的的打分
        double[] scores = new double[6];

        Scanner sc = new Scanner(System.in);

        // 遍历数组中的每个位置,录入评委的分数,存入到数组中去
        for (int i = 0; i < scores.length; i++) {
            System.out.println("请您输入当前第" + (i + 1) + "个评委的分数");
            double score = sc.nextDouble();
            scores[i] = score;
        }
        // 遍历数组中的每个元素进行求和
        double sum = 0;
        for (int i = 0; i < scores.length; i++) {
            sum += scores[i];
        }
        System.out.println("选手最终得分是:" + sum / scores.length);
    }
}

剩下的不懂

者亚杰
者亚杰
© 2025 软件园学生在线
Theme by Wing