Basic knowledge of Java (partial, casual notes)

Table of contents

Basic knowledge:

Basic framework:

Note:

Keywords:

Literal: (data type)

variable:

Precautions:

Computer storage rules:

Basic syntax:

Identifier:

Keyboard entry:

Introduction to IDEA project structure

Arithmetic operators:

Judgment and looping

sequential structure

branch structure

if statement

switch statement format

while loop

Loop structure

array

Static initialization of arrays

Array address value and element access

Array element access

Dynamic initialization of arrays

Two-dimensional array

Dynamic initialization of two-dimensional array

Arrays class

method

method definition

Calling a method with a return value

Precautions:

Method overloading:

object-oriented

classes and objects

How to define a class

How to get the class

How to use objects

Object-oriented advancement

encapsulation

Introduction to Java memory allocation

Explain from the perspective of memory

The memory principle of this

The difference between member variables and local variables

function call;

random

string

Two ways to create String objects

String comparison

fun

Format dates using SimpleDateFormat


Basic knowledge:

Basic framework:

 public class ywj1 {
     public static void main(String[] args){  //快捷 psvm 回车
         System.out.println("Hello world");   //快捷键 sout 打印 换行
         System.out.print("Hello world");   //打印 不换行
     }
 }
 //格式化代码 CTRL+Alt+l
 // arr.fori  循环数组快捷键
 //CTRL + Alt + M 自动抽取方法
 //shift + F6  变量的批量修改

Note:

 
// 单行注释
 /*  多行注释 */
 /** 注释信息  **/

Keywords:

  • English words given specific meanings by Java

  • Keyword letters must be all lowercase

  • Commonly used code editors have special color markings for keywords, which is very intuitive.

 class //Used to (create/define) a class, which is the most basic unit of Java
       //class + class name

Literal: (data type)

  • Tell the programmer: the format in which data is written in the program

    System.out.println(123)     //输出整数  
     System.out.println('a')     //输出字符
     System.out.println("abc")   //输出字符串
     /*拓展:
       \t 制表符 在打印的时候,把前面的字符串的长度补齐到8,或者8的倍数。最少补1个空格,最多补8个空格
       布尔 boolean
       */

    variable:

     // 数据类型 变量名 = 数据值;
     int a = 10;
     double b = 10.00;
     String name  //字符串
     Boolean 

Precautions:

  • Only one value can be stored

  • Variable names are not allowed to be defined repeatedly

  • One statement can define multiple variables

  • Variables must be assigned a value before use

  • variable scope

Computer storage rules:

In computers, any data is stored in binary form

 /* 
 二进制 0b 开头
 十进制  不用加任何前缀
 八进制 以 0 开头
 十六进制 以 0x 开头 由0-9 a-f组成
 */

Basic syntax:

Identifier:

  • Composed of numbers, letters, underscores, and the dollar sign ($)

  • cannot start with a number

  • Cannot be a keyword

  • case sensitive

  •  /*- CamelCase nomenclature: methods, variables For example: name firstName
     - Big CamelCase nomenclature: class name Student GoodStudent */

Keyboard entry:

 //导包 导包的动作必须出现在类定义的上边
 import java.util.Scanner; 
 //创建对象 表示我要开始用Scanner这个类了
 Scanner sc = new Scanner(System.in);  //sc是变量名 可以变 其他都不允许改变
 //接收数据 真正开始干活了
 int i = sc.nextInt();   //这个格式里,只有i是变量名,可以变,其他都不允许改变

Introduction to IDEA project structure

project

module

package

class

Arithmetic operators:

  • When performing operations on numbers, operations cannot be performed on data of different types. They need to be converted into the same type before operations can be performed.

  • implicit conversion

    • One thing to note is: if you assign a value with a value range to a variable with a small value range. Assignment is not allowed. If you must do this, you need to add forced conversion.

      (Format: target data type variable name = (target data type) data to be forced;)

 "+" //The string appears as a connector and is executed one by one from left to right.
 ++ //Function plus; add 1 to the value of the variable
 -- //Decrease the value of the variable by 1
    

Judgment and looping

sequential structure

branch structure

if statement

 /*
 if(关系表达式){
     语句体;
 } else if(关系表达式){
 }
   ...
 else{
     语句体;
 }
 */

switch statement format

 
switch(表达式){
     case 值1:
           语句体1;
            break;
     case 值1:
           语句体1;
            break;
      ....
      
     default:
         语句体N+1;
         break;
         
 }
 /*
 1. Default does not have to be written at the bottom, but it is customary to write it at the bottom.
 2. Case penetrates, so remember to write break

while loop

 
/*
 初始化语句;
 while(条件判断语句){
     循环体语句;
     条件控制语句;
 }
 */

Loop structure

 for( ){
     
 } 
 while(){
     
 }
 do{
     
 }while(true)

array

 //格式一:数据类型 [] 数组名
 int [] array
     
 //格式二:数据类型 数组名 [] 
    int array []
  
     
 char [] arr = new char[100];  //定义一个数组长度为100的数组  

Static initialization of arrays

Initialization: It is the process of opening up space for an array container in memory and storing data in the container. Full format: data type [ ] array name = new data type [ ] { element 1, element 2, element 3...}

Simplified format: data type [ ] array name = { element 1, element 2, element 3...};

 
int[] array = new int[]{1,2,3};
 //简化格式
 int[] array ={1,2,3};
 doble[] array1 = new double[]{1,2,3};

Array address value and element access

 int [] arr = {1,2,3,4,5};
 System.out.println(arr);    //此时输出的是数组的地址值

Array element access

 
int [] arr = {1,2,3,4,5};
 int n = arr[0];  //获取数组中的数据
 System.out.println(n);
 arr[1] = 100;   //把数据存到数组当中
 //遍历打印
 for(int i=0;i<arr.length;i++){
     System.out.println(arr[i]);
 }
 /*
 在Java中,关于数组长度的一个属性,length
 调用方式:数组名.length
 System.out.println(arr.length);
 拓展:
 自动的快速生成数组的遍历方式:
 idea提供的
 数组名.fori
 */

Dynamic initialization of arrays

  • Dynamic initialization: Initialization only specifies the length of the array, and the initial value is assigned by the system array

  • Format: data type [ ] array name = new data type [array length]

Two-dimensional array

 /*
 格式;数据类型[][] 数组名 = {
   
   {元素1,元素2},{元素1,元素2}};
 eg: int[][] arr = {
   
   {11,22},{33,44}};
 int arr[][] = {
   
   {11,22},{33,44}};

Dynamic initialization of two-dimensional array

 
type[][] typeName = new type[typeLength1][typeLength2];
 int[][] a = new int[2][3];

Arrays class

  • Assign a value to an array: via the fill method.

  • Sort the array: use the sort method, in ascending order.

  • Compare arrays: Use the == equals == method to compare whether the element values ​​in the array are equal.

  • Searching for array elements: The binarySearch method can perform a binary search operation on the sorted array.

method

  • Method is the smallest execution unit in the program (actually it is a function written by yourself)

  • When is the method used?

    • That is, repeated code and code with independent functions can be extracted into methods.

  • What are the benefits of this method in actual development?

    • Improve code reusability

    • Improve code maintainability

method definition

 /*
 格式
 public static void 方法名(){  //void视情况而定
     方法体(就是打包起来的代码);
     //return 返回值;
 }
 调用:(必须先定义后调用,否则程序会报错)
 方法名();
 */

Calling a method with a return value

 /*
 直接调用:
 方法名(实参);
 赋值调用:
 整数类型 变量名 = 方法名(实参);
 输出调用:
 System.out.println(方法名(实参));
 */

Precautions:

  • If the method is not called, it will not be executed.

  • There is a horizontal relationship between methods and they cannot be nested within each other.

  • The order in which methods are written has nothing to do with the order in which they are executed.

  • The return value type of the method is void, which means the method has no return value. If there is no return value, the return statement can be omitted. If you want to write return, it cannot be followed by specific data.

  • Under the return statement, you cannot write code because it will never be executed and is invalid code.

return keyword

  • The method has no return value: it can be omitted. If written, it indicates the end of the method

  • The method has a return value: it must be written. Represents the end method and return result.

Method overloading:

  • Methods with different method names and different parameters in the same class. It has nothing to do with the return value.

    • The parameters are different: different numbers, different types, and different orders.

  • benefit;

    • You don’t need so many words when defining methods

    • You don’t need to go through so much trouble when calling methods.

object-oriented

  • Finding things to solve problems is called object-oriented

classes and objects

  • Class (design diagram): It is a description of the common characteristics of objects

  • Object: is a real concrete instance

How to define a class

 public class class name {
     1. Member variables (representing attributes, generally nouns)
     2. Member method (represents behavior, usually a verb)
     3. Constructor
     4. Code block
     5. Internal class
 }

How to get the class

  • Class name object name = new class name();

How to use objects

  • Access properties: object name. member variable name

  • Access behavior: object name. method name (...)

Replenish:

  • A class used to describe a type of thing, professionally called: Javabean class

    In the Javabean class, the main method is not written.

  • In the past, the class in which the main method was written was called the test class.

    We can create objects of the Javabean class in the test class and make assignment calls.

  • It is recommended to capitalize the first letter of the class name, and you need to know the meaning of the name, using camel case mode;

  • Multiple classes can be defined in a Java file, and only one class can be public-modified, and the public-modified class name must become the code file name.

  • In actual development, it is recommended to define a class in one file.

  • The complete definition format of member variables is: modifier data type variable name = initialization value; generally there is no need to specify an initialization value, there is a default value.

Object-oriented advancement

encapsulation

  • Tell us how to correctly design the properties and methods of objects

  • Whatever the object represents, the corresponding data must be encapsulated and the behavior corresponding to the data must be provided.

  • benefit?

    • Make programming simple. If something happens, just find the object and adjust the method.

    • To reduce our learning costs, we can learn less and remember less, or we don’t need to learn at all, and we don’t need to remember the methods of objects. We can just find them when needed.

private keyword

  • It is a permission modifier, in order to ensure the security of data

  • Members can be modified (member variables and member methods)

  • Members modified by private can only be accessed within this class

  • For each privatized member variable, the get and set methods must be mentioned

    • set method: assign values ​​to member variables

    • get method: provides the value of member variable to the outside world

 
//作用:给成员变量name进行赋值
 public void setName(String n){
     return name;
 }
 //作用;对外界提供name属性的
 public String getName(){
     return name;
 }

this keyword

  • Can distinguish between member variables and local variables

The principle of proximity

 
System.out.println(age)  //就近原则
 System.out.println(this.age)  //不会到局部位置去找 只会调用成员位置的 

Construction method

  • Constructor method is also called constructor and constructor

  • Function: When creating an object, it is automatically called by the virtual machine to assign (initialize) values ​​to member variables.

Format:

 
/*
 piblic class Student{
     修饰名 类名(参数){
         方法体;
     }
 }
 */
  • Features:

    • The method name is the same as the class name, and the case must be the same.

    • There is no return value type, not even void

    • There is no specific return value (result data cannot be brought back by return)

    Precautions

  • Definition of constructor method

    • If no constructor is defined, the system will give a default and parameterless constructor

    • If a constructor is defined, the system will no longer provide a default constructor

  • Constructor overloading

    • The parameterized constructor and the parameterless constructor have the same method name but different parameters. This is called overloading of the constructor.

  • Recommended usage

    • Regardless of whether it is used or not, manually write the parameterless constructor and the constructor with all parameters.

Standard Javabeans

  • The class name should know its meaning

  • Member variables are modified with private

  • Provide at least two constructors

    • No-argument constructor

    • Constructor with all parameters

  • member method

    • Provide setXxx()/getXxx() corresponding to each member variable

    • If there are other behaviors, they also need to be negotiated

shortcut key

  • Quickly insert privatized full-name variables

 // all +insert  
 //or alt + Fn + insert
  • Plug-in: Generate standard Javabeans in 1 second

  •  File—>Settings—>Plugins—>Search ptg in Maketplace—>Install

Introduction to Java memory allocation

  • stack memory

    • The memory entered when the method is run, and the variables are also here

  • Heap memory

    • What comes out of new opens up space in this memory and generates an address.

  • method area

    • The memory entered when the bytecode file is loaded

  • native method stack

  • register

Explain from the perspective of memory

  • Basic data types

    • Data values ​​are stored in their own space

    • Features: Assigning values ​​to other variables also assigns real values.

  • Reference data type

    • Data values ​​are stored in other spaces, and address values ​​are stored in your own space.

    • Features: Assign to other variables, assigned address value

The memory principle of this

  • The role of this: distinguish local variables and member variables

  • The essence of this: the address value of the method caller

The difference between member variables and local variables

  • Member variables: variables outside methods in a class

  • Local variables: variables in methods

function call;

  • Mainly what I have encountered and used

random

  • Random r = new Random(); //Randomly generate an integer number and assign it to r

 import java.util.Random;
 public class dd {
     public static void main(String[] args) {
         int n = 10;
         while(n!=0){
             //随机生成一个整数
             Random r = new Random();
             //定义生成的整数的范围是1~20;
             int rr = r.nextInt(20) + 1;
             System.out.println(rr);
             n = n -1;
         }
     }
 }
 /*
 random.nextInt(int n)
 随机生成一个int值,该值介于[0,n)之间,也就是0-n之间的随机数,包括0不包括n
 随机生成0-9的数:random.nextInt(10)
 随机生成1-10的数:random.nextInt(10)+1
 */

random roll call

 
import java.util.Random;
 public class ee {
     public static void main(String[] args) {
         Random random = new Random();
         String[] names = {"刘备","曹操","关羽","张飞","赵云","孙权"};
         //通过随机生成的数来定义下标,从而为随机选取
         int index = random.nextInt(names.length);
         System.out.println("随机抽取到的名字为:" + names[index]);
     }
 }

随机输出10个1~100之间的数

     random.ints(0,100).limit(10).sorted().forEach(System.out::println);

string

  • API: Application Programming Interface

  • Notes on String:

    • The content of the string will not change, and its object cannot be changed after it is created.

Two ways to create String objects

 /*
 直接赋值:
 String name = "张三";
 new:
 //空参构造
 String s = new String();

String comparison

  • Basic data types: book data values

  • Reference data type: compared to the address value

 
boolean res = s1.equals(s2); //判断s1和s2是否相等
 boolean res = s1.equalsIgnoreCase(s2);//比较字符串对象中的内容是否相等 忽略大小写

fun

  • Format dates using SimpleDateFormat

 
package test;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 public class Test {
     public static void main(String[] args) {
         Date Now = new Date();
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM- hh:mm:ss");
         System.out.println("当前时间为:" + simpleDateFormat.format(Now));
     }
 }

Guess you like

Origin blog.csdn.net/weixin_63292027/article/details/127894336