Java self-study detailed study notes (1) ----- Basic concepts of Java

Table of contents

Java basic concepts

1. Notes

2. Keywords

1. What are keywords:

2. Characteristics of keywords:

3. What does the class keyword mean:

3. Literal value

1. What is a literal?

2. Common literals

3. Some special literals

4. Variables

1. What are variables and usage scenarios?

2. Definition format of variables

3. Some notes on variables:

4. Variable code exercises:

5. Data type

1. Things to note:

2. The relationship between the ranges of integers and decimals:

6. Identifier

1. What is an identifier?

2. Hard requirements for identifiers (very important)

7. Keyboard entry

1. What is keyboard entry?

2. Steps for keyboard entry

3. Use the student management system to demonstrate:


Java basic concepts

1. Notes

Comments are divided into multi-line comments, single-line comments and text comments

2. Keywords

1. What are keywords:

It is an English word that has been given a specific meaning by Java.

2. Characteristics of keywords:

●Keywords should be all lowercase

●Keywords will be marked with special colors

3. What does the class keyword mean:

Class: used to (create/define) a class. Followed by the class name. Classes are the most basic unit of Java, equivalent to cells in the human body. A complete Java project is composed of different classes, and all code must be written within the scope of the class.

3. Literal value

1. What is a literal?

Tell the programmer how the data is written in the program

2. Common literals

Note: Null values ​​cannot be printed directly. If you want to print null values, you can only use string form.

3. Some special literals

1), \t (tab character):

Function: When printing table data, align the table.

Usage: You can use double quotes or single quotes. When printing, pad the length of the previous string to 8, or an integer multiple of 8. Fill in at least one space and fill in at most 8 spaces.

Comparative Results:

Without tabs:

With tab characters:

2), \n: Line break, change to the next line at the current position, and will not return to the beginning of the line.

3), \r: Enter, return to the beginning of the current line, and will not change to the next line. If the output is continued, the previous contents of this line will be overwritten one by one.

4. Variables

1. What are variables and usage scenarios?

Variable: A quantity whose value may change during the execution of a program.

Usage scenario: When a certain data changes frequently, use variable storage and just modify the value of the record in the variable. For example, the username and password when logging in.

2. Definition format of variables

Data type variable name = data value;

Data type: Add type restrictions for the data stored in the variable (integer? decimal?...). There is a detailed introduction later.

Variable name: The name of the storage space. The name is for convenience of future use and must not be repeated.

Data value: The data that actually exists in the variable.

Equal sign: assignment. Assign the data on the right to the variable on the left.

3. Some notes on variables:

1), only one value can be stored;

2) Duplicate definitions of variable names are not allowed;

3) There can be multiple variables in one statement;

4) Variables must be assigned values ​​before use, otherwise they cannot be used.

4. Variable code exercises:

There are no passengers on a bus at the beginning. One passenger gets on at the first stop, two passengers get on at the second stop, one passenger gets off, and one passenger gets on at the third stop. How many passengers are there in total on the bus? (The answer is 3)

package repeat;

public class testcar {
    public static void main(String[] args) {
        //一开始车上没有乘客
        int count = 0;
        /*
        第一站:上去一位乘客
        就是在原有的基础上+1
         */
        count = count + 1;//或者可以写成count+=1
        //第二站:上去两位乘客,下来一位乘客
        count = count + 2 - 1;
        //第三站:上去一位乘客
        count = count +1;
        //请问到了终点站一共有多少个乘客
        System.out.println(count);
    }
}

count is the variable name of the number of passengers

testcar is the class name

5. Data type

There are two types of data types, one is the basic data type and the other is the reference data type. Reference data types are introduced later.

Basic data types:

1. Things to note:

When the value exceeds the value range, the code will report an error.

When taking a long type value, add l at the end, for example, long n = 1111111111111111118L (this L can be uppercase or lowercase).

When taking a float type value, f must be added at the end. This f can be uppercase or lowercase.

Integer types generally use int, and decimal types generally use double.

2. The relationship between the ranges of integers and decimals:

double>float>long>int>short>byte

6. Identifier

1. What is an identifier?

Identifier: It is the name given to classes, methods, variables, etc.

2. Hard requirements for identifiers (very important)

If the hard requirements are not met, the code will report an error.

1), consisting of numbers, letters, underscores and dollar signs;

2), cannot start with a number;

3), cannot be a keyword;

4), case sensitive;

7. Keyboard entry

1. What is keyboard entry?

Introduction to keyboard input: Java helps us write a class called Scanner, which can receive numbers entered by the keyboard.

2. Steps for keyboard entry

1) Step 1: Guide the package (find where the Scanner class is)

It should be noted that it must be written in a fixed format, and the action of importing the package must appear above the class definition.

2) Step 2: Create an object (indicating that the Scanner class is to be used)

3) Step 3: Receive data (meaning this class is about to start working)

In the above format, it is: data type variable name =scanner.nextInt() Only the variable name and data type can be changed, next is followed by the data type. Others are in fixed format.

3. Use the student management system to demonstrate:

package jdbc1;

//导包

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

public class zsgc {
    public static void main(String[] args) {
        //为了让我们的系统一直跑着所以我们要写一个死循环
        while(true){
            //创建一个Scanner的类
            Scanner scanner = new Scanner(System.in);
            System.out.println("学生管理系统\n"+
                    "功能:增删改查\n"+
                    "1、新增\n"+
                    "2、根据id修改\n"+
                    "3、根据id删除\n"+
                    "4、根据id查询数据\n"+
                    "5、查询所有用户\n"+
                    "其他退出");
            String string = scanner.next();
            switch (string){
                case "1":add();break;
                case "2":update();break;
                case "3":delete();break;
                case "4":selectid();break;
                case "5":selectall();break;
                default:
                    System.out.println("输入错误,系统终止");
                    System.exit(0);
            }
        }
    }


    /*
    全部查询
     */
    public static void selectall(){
        //直接调用查询方法
        ArrayList<Student> list = jdbcStudent.selectall();
        for (Student student:
             list) {
            System.out.println(student);
        }
    }



    /*
    用id查询
     */
    public static void selectid(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入查询的id");
        //接收数据,使用Scanner
        int id = scanner.nextInt();
        Student student = jdbcStudent.selectid(id);
        System.out.println("查询结果"+student);
    }

    /*
    根据id删除
     */
    public static void delete(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入删除的id");
        int id = scanner.nextInt();
        jdbcStudent.delete(id);
    }

    /*
    根据id修改
     */


    public static void update(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入修改的id");
        int id = scanner.nextInt();
        System.out.println("请输入修改的账号");
        String account = scanner.next();
        System.out.println("请输入修改的密码");
        String password = scanner.next();
        Student student = new Student(id,account,password);
        //修改
        jdbcStudent.update(student);//调用修改的语句
    }


    public static void add(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入账号");
        String account = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        //为了少写参数可以用实体类进行数据传递
        Student student = new Student (0,account,password);
        jdbcStudent.add(student);
    }
}

The code results show:

Guess you like

Origin blog.csdn.net/yh1009/article/details/132079289