On an article I’ll give you a simple Shared Java some basic grammar and noun explanation, many students direct messages to me say not enough comprehensive and detailed summary, hope to sum up, I’m such a request is not too much, do not deceive you, I was in the interest of time, do not you want to effect, heart feel sorry and ashamed, the specific circumstances no longer say more, I hope you can understand.

After the painful experience, he immediately set about and books on the Internet, to collect data, then wrote the article, I want to give beginners Java classmate share is dry, absolutely without any useless content, purpose is to look after, there will be a harvest, if you need to also share with friends see, so don’t waste of everyone’s time, Thank you again for your long-term support, I will continue to work hard, to give you more good products.

The original article is attached here. If you need it, you can see it: a compilation of basic grammar and noun explanations

For beginners, you can take a look: if you need a systematic Java learning schedule, don’t waste time!

Add: the latest Java learning materials in 2021:For beginners, it has an excellent reputation.

Of course, there are several other articles you can browse through when you have time, you will definitely gain something. If you don’t understand or need any help, please leave a comment and let me know. I will contact you as soon as I see it. All right, let’s get to the point.

First of all, the first thing to note when writing a program:

1. Naming conventions

1. All project names are lowercase

2. All package names are in lower case

Capitalize the first letter of the class name. If the class name consists of more than one word, capitalize the first letter of each word.

Public class MyFirstClass{}Copy the code

4. Variable names and method names start with a lowercase letter. If the name consists of more than one word, capitalize the first letter of each word.

Such as: int index = 0; public void toString(){}Copy the code

5. All uppercase names of constants

For example: public static final String GAME_COLOR= "RED";Copy the code

6. All naming rules must follow the following rules:

1) The name can contain only letters, digits, underscores (_), and $signs ($)

2) Cannot start with a number

3) The name cannot be a keyword in JAVA.

4), firmly do not allow the emergence of Chinese and pinyin naming.

2. Annotation specification

1. Class annotations

Each class must be preceded by a class annotation. The annotation template looks like this:

/** * Copyright (C), 2006-2010, ChengDu Lovo info. Co., Ltd. * FileName: * * @Author Name of class creator * @date Date of creation * @version 1.00 */Copy the code

2. Attribute comments

Each attribute must be preceded by an attribute comment. The annotation template is as follows:

/ / Private String strMsg = null;Copy the code

3. Method comments

Each method must be preceded by a method comment. The comment template looks like this:

/** * Detailed usage of class methods ** @param Parameter 1 Usage of parameter 1 * @return Description of the returned result * @throws Exception type. The error code indicates that an exception is thrown from such a methodCopy the code

4. Constructor annotation

Each constructor must be preceded by a comment. The comment template looks like this:

/** * Detailed constructor instructions ** @param Parameter 1 Instructions for parameter 1 * @throws Exception type. The error code indicates that an exception is thrown from such a methodCopy the code

5. Internal method annotations

Use single – or multi-line comments within methods, which are added as the case may be.

For example: // Background Color Color bgColor = color. RED Java is case sensitive, which means that the Hello identifier is different from Hello. Class name For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, capitalize the first letter of each word, such as MyFirstJavaClass. Method names All method names should start with a lowercase letter. If the method name contains several words, capitalize the first letter of each following word. Source File The source file name must be the same as the class name. When saving the file, you should use the class name as the file name (remember Java is case sensitive), with the suffix.java for the file name. (A compilation error will result if the file name and class name are not the same). Public static void main(String []args) ¶Copy the code

1. Identifier

  • In the Java language, a valid sequence of characters that identifies class names, object names, variable names, method names, type names, array names, and package names is called an identifier.
  • The identifier consists of letters, digits, underscores (_), and dollar signs, and cannot start with a number.
  • The Java language is case sensitive;
  • Identifier naming rules: The first letter of class names is uppercase, variable names and method names are humped, constants are all uppercase, multiple words are separated by “_”, and package names are all lowercase.

2. Keywords

  • In the Java language, there are specialized terms that have been assigned special meanings and can no longer be used to name identifiers. These special terms are called keywords.
  • Java has 50 keywords and three reserved words, none of which can be used to name identifiers.
  • True, false, and NULL are reserved keywords but cannot be used to name identifiers. Reserved keywords are reserved by Java and may be used as keywords in later upgrade versions.

Here’s a general overview of keywords

Assert is used for program debugging. Boolean is one of the basic data types. Byte is one of the basic data types. Char Char char char char char char char char char char char char char char char char char char char char char char char char char Do is one of the basic data types of double in the do-while loop. Else is used in conditional statements. Extends indicates that a type is a subtype of another type. Common types such as class and interface final are used to describe final properties, indicating that a class cannot be subclassed, or that member methods cannot be overridden. Finally is used to handle exception cases and to declare a block that is almost certain to be executed. Float is one of the basic data types, the single-precision floating-point type. Import indicates access to the specified class or package. Instanceof tests whether an object is an instance object of the specified type. Int is one of the basic data types. Integer type Interface Long One of the basic data types, Native is used to declare a method that is implemented in a computer-related language such as C/C++/FORTRAN. New is used to create a new instance object. Protected Mode Public An access control mode: Common mode return returns data from member methods. One of the basic data types is short. The short integer type static indicates that strictFP is used to declare FP_strict (single or double precision floating point numbers) expressions that comply with the IEEE 754 arithmetic specification super “Synchronized” indicates that a section of code is required to execute synchronously. This refers to a reference to the current instance object and throws an exception Transient specifies all exceptions that need to be thrown in the currently defined member method. Transient specifies a member field that does not need to be serialized. Try a block that might throw an exception. Void Specifies that the current member method does not return a value Used in cyclic structures

3. Basic data types

1. Integer type (int is the default)

2. Floating-point (double is the default)

When assigning to a variable of type float, always add “F” or “F” to the end if the value has a fractional part;

3. Character types (2 bytes)

    • Char ch = ‘a’;
    • Some characters cannot be entered on the keyboard, so escape characters are used.

4. Boolean type (1 byte)

    • Boolean flag = true;

The default value

    • Numerical variable: 0;
    • Character variable: ‘\0’;
    • Boolean variables: false;
    • Reference data type: NULL;

Conversion between different data types

    • Automatic type conversion (low to high)

    • Cast (high to low)
public class Test003 { public static void main(String[] args) { byte b = 100; int i = 22; Float f = 78.98 f; int res = b + i + (int)f; System.out.println("res: "+res); // Int f = 78 system.out.println ("res: "+res); //res: 200 } }Copy the code

Operators and expressions

  1. Arithmetic operator

public class Test003 { public static void main(String[] args) { int i = 5; System.out.println(0/i); //0 System.out.println(0%i); //0 System.out.println(i/0); / / divisor cannot be zero, the abnormal Java. Lang. ArithmeticException System. Out. The println (I % 0); / / divisor cannot be zero, the abnormal Java. Lang. ArithmeticException}}Copy the code
  1. Assignment operator

  1. Increment and decrement operators (++, –)
public class Test003 {
    public static void main(String[] args) {
        int i = 5;
        System.out.println(i++);    //5
        System.out.println(++i);    //7
        System.out.println(i);    //7
        System.out.println(--i);    //6
        System.out.println(i--);    //6
        System.out.println(i);    //5     }
}
Copy the code
  1. Relational operator

  1. Logical operator
public class Test003 { public static void main(String[] args) { boolean t = true; boolean f = false; System.out.println(t && f); / / false, short circuit and operator, if the operator on the left is false is not calculated on the right side of the expression System. Out. The println (t | | f); //true, short circuit or operator, if the left side of the operator is true, the expression on the right side is not evaluated system.out.println (t & f); / / false, and operator, regardless of whether the left to false expression to compute the right of the System. The out the println | f (t); System.out.println(t ^ f); // the expression system.out.println (t ^ f) is evaluated whether the left side is true or not. System.out.println(!); // The xor operator is true as long as the left and right sides are different. f); //true, take the inverse operator}}Copy the code
  1. An operator
Public class Test003 {public static void main(String[] args) {// int b1 = 6; // Binary = 00000000 00000000 00000110 int b2 = 11; // Binary is 00000000 00000000 00000000 00001011 system.out.println (b1 & b2); / / the bitwise and operator, binary for 00000000, 00000000, 00000000, 00000010, the results of 2 System. Out. The println (b1 | b2); // The binary is 00000000 00000000 00000000 00001111. The result is 15 system.out.println (b1 ^ b2); // The binary is 00000000 00000000 00000000 00001111. // The bit-specific XOR operator. The binary is 00000000 00000000 00000000 00001101. The result is 13 system.out.println (~b1); // The binary is 1111111111 11111111 11111111 11111111 11111001, the result is -7 system.out.println (b1 << 2); // Left shift operator, binary 00000000 00000000 00000000 00011000, result 24 int b3 = -14; // left shift operator, binary 00000000 00000000 00000000 00011000, result 24 int b3 = -14; Println (b3 >> 2); Println (b3 >>> 2); // Unsigned right shift operator, binary 001111111111 11111111 11111111 11111100, result is 1073741820}}Copy the code
  1. Ternary operator
public class Test003 { public static void main(String[] args) { int a = 1; int b = 2; int c = 4; int res = c==a+b? ++a:c>a+b? ++b:++c; // The ternary operator (expression)? (value 1):(value 2), if the expression is true, value 1; otherwise, value 2 system.out.println (res); //++b = 3}}Copy the code
  1. Operator precedence

5, arrays,

1. One-dimensional arrays

public class Test003 { public static void main(String[] args) { int[] i; Int ii[]; // Declare an integer array variable int ii[]; I = new int[5]; Float [] f = new float[5]; // Create a one-dimensional array object of length 5 and point variable I to it. Float [] f = new float[5]; Double [] d = {1, 2, 3.4, 4.5}; double[] d = {1, 2, 3.4, 4.5}; System.out.println(d[3]); Println (f[0]); // Getelement (); // getelement (); // When an array object is created, the array elements in the object are the default values for the data type, so the result here is 0.0 //System.out.println(I [5]); / / when get elements in an array by the array subscript, value > = [] array length is submitted to the abnormal Java lang. ArrayIndexOutOfBoundsException (array subscript bounds) / / System. Out. The println (ii [0]); System.out.println(d.length); // If an array variable is declared but does not point to a specific array object, compile error system.out.println (d.length); // Get the length of the array, which is 4}}Copy the code

2. Two-dimensional arrays

public class Test003 { public static void main(String[] args) { int[][] i; // Declare an integer array variable int ii[][]; // Declare an integer array variable int[] iii[]; I = new int[5][2]; Float [][] f = new float[5][2]; float[][] f = new float[5][2]; / / directly to create a length of 5 single-precision floating-point type 2 d array object, and point to the object variable f double [] [] d = {{1}, {2, 3}, {4 and 6}, {7,8,9,10}}; System.out.println(d[3][1]); System.out.println(f[0][0]); //System.out.println(I [5][0]); //System.out.println(I [5][0]); / / when get elements in an array by the array subscript, value > = [] array length is submitted to the abnormal Java lang. ArrayIndexOutOfBoundsException (array subscript bounds) / / System. Out. The println (ii [0] [0]). System.out.println(d.length); // If an array variable is declared but does not point to a specific array object, compile error system.out.println (d.length); Println (d[2].length); // Return the length of the array with subscript 2}}Copy the code

6, process control statements (if the switch, for, while, do… While)

1. Conditional branch statements

public class Test003 { public static void main(String[] args) { int[] score = new int[5]; score[0] = -7; score[1] = 65; score[2] = 80; score[3] = 90; score[4] = 59; for(int i=0; i<score.length; I++) {if (score [I] > = 0 && score < 60) [I] {System. Out. Println (" fail "); } else if (score [I] > = 60 && score < 80) [I] {System. Out. Println (" pass "); } else if (score [I] > = 80 && score < 90) [I] {System. Out. Println (" good "); } else if (score [I] > = 90 && score < 100) [I] {System. Out. Println (" optimal "); }else {system.out.println (" abnormal score "); } } char ch = 'a'; Switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) {//switch (ch) { // Case 'a': system.out.println (" "); // Case 'a': system.out.println ("); break; Case 'B': case 'B': system.out.println (" good "); break; Case 'C': case 'C': system.out.println (" pass "); break; Default: // If the above conditions do not match, then enter default to start the execution statement system.out.println (" failed "); }}}Copy the code

2. Loop statements

public class Test003 { public static void main(String[] args) { int res = 0; For (int I =1; int I =1; int I =1; i<=10; i++) { if(i==3) continue out; If (I ==5) break out; if(I ==5) break out; //break out of loop res = res + I; } System.out.println(res); Int res2 = 0; int res2 = 0; int i = 1; in: do{ if(i==3) continue in; If (I ==5) break in; if(I ==5) break in; Res2 = res2 + I; i++; }while(i<=10); System.out.println(res2); }}Copy the code

\

Two, the noun explanation

1. Origin of Java

Java was owned by Sun and later acquired by Oracle.

James gosling, the father of Java. The language was originally called Oak, after C++;

Java version: JDk6/7 is the most commonly used version. Java 8 introduces important features such as streams and lambda expressions. The latest version of Java is now 11;

2, JDK, JRE, cross-platform, Java platform

  • JDK: The Java Development Runtime Environment (JDK) is installed on the programmer’s computer.

JDK = JRE + development tool set (e.g. Javac compile tool, etc.)

  • Java Runtime Environment (JRE) is a Java Runtime Environment that you can install if you don’t need to develop and only need to run Java programs

JRE = JVM + JavaSE standard class library

  • JDK contains JRE contains JVM
  • Cross-platform features
    • Platform refers to the operating system (Windows, Linux, Mac).
    • Java programs can run on any operating system, written once and run everywhere
    • Why can the JVM (Java Virtual Machine), which relies on Java, be cross-platform? JAVA programs run on virtual machines, and there are different versions of virtual machines for different operating systems
    • The Java language is cross-platform; the Java Virtual Machine is not
  • The Java platform
    • Java SE (Desktop application) Standard Edition
    • Java EE (Web Applications) Enterprise Edition
    • Java ME(Mobile) Mini – Android, less used since the rise of IOS

3, JavaAPI

Java Application Programming Interface

Commonly used API

Use of StringBuffer and StringBuilder

StringBuilder(thread unsafe)/StringBuffer(thread safe) are variable length character objects

Using append method to concatenate variables is efficient

Both have 16-bit buffers by default

The capacity method is used to obtain the occupied space, and the length method is used to obtain the real number of characters. The append method concatenates variables;

The main performance differences between String and StringBuffer are:

A String is immutable, so every time you change a String variable, it generates a new String and points to the new String, so it’s best not to use a String that changes its contents frequently. This is because every time an object is generated, it affects the performance of the system. In particular, when there are more unreferenced objects in memory, the JVM GC frequently, which reduces the performance of the JVM.

When using StringBuffer, the StringBuffer object itself is operated on each time, rather than generating new objects and changing object references. So StringBuffer is recommended for most cases, especially if the string object changes frequently.

In real modular programming, the programmer responsible for a module may not be able to clearly determine whether the module will run in a multi-threaded environment. Therefore, you can only use StringBuilder unless you are sure that the bottleneck is the StringBuffer and that your module will not run in multi-threaded mode. Otherwise, StringBuffer.

Performance: StringBuilder>StringBuffer>String.

5. Math related

Round (double/float) Round returns long/int

Floor (double d) round down returns double

Ceil (double d) rounded up returns double

Abs (double/long/float/int

Random () gets a random decimal (double) between 0 and 1, greater than or equal to 0.0 and less than 1.0

6. Random Random number

NextInt (int I) Gets a random integer between 0 and I, including 0 but excluding I

NextDouble () gets random decimals between 0 and 1, including 0.0 but not 1.0

7, Java. Math. BigDecimal

A more precise type than double; Often used for operating amount, exchange rate, etc.;

Common methods:

add

subtract

multiply

divide

The setScale method is used to format the decimal point

SetScale (1) indicates that one decimal place is reserved. By default, rounding is used

8, SimpleDateFormat usage

Format (date object) formats a date to a specified format string

Parse (a Date string in the specified format) converts the string to Date. Note: The format must be the same as that specified by SimpleDateFormat or ParseException is reported

9, [Java. Util. Date]

Convert the 1999-10-10 format string to a date object

New Date() gets the current system time

GetTime () gets the number of milliseconds

10, [Java. SQL. Date]

Date is its parent class, so sqL.date is more powerful than util.date.

11, the Calendar

In the Calendar class, the value of the month is the actual month value minus 1

The value of week is different from that of the Date class. In the Calendar class, Sunday is 1, Monday is 2, Tuesday is 3, and so on

12, Java code writing and implementation process

  • Source files: Write Java source files (also called source files) with the.java extension;
  • Compile: The source file is then compiled by the compiler into a bytecode file with the.class extension; Why compile? JAVA programs are run by the virtual machine, we write code that the virtual machine does not know, we have to write their own code to translate the language that the virtual machine knows
  • Run: Finally use the interpreter to run the bytecode file.

13, comments,

  • Definition: Text used to explain and describe a program. Comments are not executed
  • Classification:
    • Single-line comments: // Comments the content
    • Multiline comment: /The comment/
    • Documentation comments: / *The comment/
  • Note:
    • For single-line and multi-line comments, the annotated text is not interpreted by the JVM
    • The documentation comments can be parsed by javadoc, a tool provided by the JDK, to produce a set of documentation for the program in the form of a Web page file
    • Single-line comments can be nested; multi-line comments cannot be nested

14. Keyword and identifier

  • The keyword
    • Definition: a word that is given a special meaning by the Java language and has a special purpose. For example, class, int, and double are predefined by Java
    • Features: All lowercase letters. Note that String is not a keyword
    • Note: Goto and const are reserved words in Java, words that are not given a special meaning but are still used by Java
  • identifier
    • Definition: the sequence of characters used to name classes, interfaces, methods, variables, etc
    • Composition rules (can contain only the following contents and no other contents):
      • English upper and lower case
      • Numeric characters
      • And $_
    • Notes:
      • Numbers can’t start
      • Do not use keywords
      • Strictly case-sensitive, with no limit on length
      • When naming, try to know the meaning by name
    • Common naming conventions for identifiers (which are not syntactically bound):
      • Package name: Use lowercase when multiple words are used. Connect, and reverse the domain name to aaA.bbb.ccc
      • Class name & interface name: big hump Aaa AaaBbbCcc
      • Variable name & method name: small hump aaa aaaBbbCcc
      • Constant name: Multiple words are capitalized. Use _ to connect AAA_BBB_CCC

Data types in Java

Why are there data types? The Java language is strongly typed, with specific data types defined for each type of data

  • The classification of data types in Java
    • Basic data types: The basic data types are built-in in the Java language. They are integer, decimal, character, and Boolean. These four basic types are the simplest and most basic types.
    • Reference datatypes: are powerful datatypes that are created based on basic datatypes.
      • JavaSE provides a super class library containing nearly 10,000 reference data types.
      • Basic type: Class interface array enumeration

Constants in Java

  • Definition: A constant is a constant amount of data that cannot be changed during the execution of a program
  • Integer constants are int by default

In Java, it is an error to define long integer data to +L if the value exceeds the value range of int

  • Decimal constants are of type double by default

D is double, F is float defines data of type float to be followed by + F, otherwise it defaults to double

Classification of constants:

    • Integer types
      • Decimal notation: Normal digits. Such as 13, 25, etc
      • Binary representation: to0b(0B)At the beginning. Such as b1011 0, 0 b1001
      • Hexadecimal notation: to0x(0X)At the beginning. The number consists of 0-9 and a-F, such as 0x23A2, 0xa, and 0x10
      • Octal representation: to0At the beginning. Such as 01, 07, and 0721
    • Decimal type: 1.0, -3.15, and 3.168
    • The Boolean type can be true or false. Note that the value is case-sensitive
    • Character types:
      • Like ‘a’, ‘a’, ‘0’, ‘home’
      • Characters must be used' 'Package, and it can contain only and only one character
    • String type:
      • The String type is a reference type, so let’s learn how to use it as a constant type
      • Such as “I love Java”, “0123”, “”,” NULL”
      • String must be used""Parcels of any length
  • In a computer, there are three representations of signed numbers: source code, inverse code, and complement. All data operations are carried out using complement code
    • +8: 0 000 1000 -8: 1000 1000

This is the binary fixed-point notation, where the highest bit is the sign bit, 0 is positive, 1 is negative, and the remaining bits are the magnitude of the value.

    • + 8:0 000 1000-8:1111 0111

The inverse code of the positive number is the same as its original code; The inverse of a negative number is the bitwise inversion of the original code, except for the sign bit.

    • Complement: + 8:0 000 1000 -8:1111 1000

The complement of positive numbers is the same as the original code; The complement of a negative number is the addition of 1 at the end of its inverse

17. Variables and operators

1. Variables and computer storage units

  • Variables are small boxes of data in memory that you can only use to store and fetch data
  • The variable name is an identifier, which means that any valid identifier can be used as a variable name.
  • Considerations for using variables
    • A variable may not be assigned a value after it is defined, and then assigned a value when it is used. Cannot be used without assignment
    • Variables are scoped when used. (Local and global variables)
    • Variables may not be defined repeatedly
  • Computer memory unit
    • The smallest unit of storage and operation in a computer: a byte.
    • Common storage unit 1B (bytes) = 8 bits

1KB = 1024B

1MB = 1024KB

1GB = 1024MB

1TB = 1024GB

1PB = 1024TB

18. Data type conversion

  • Automatic type conversion: represents a conversion from a small range data type to a large range data type. This is called automatic type conversion

    Automatic type conversion format:Large range datatype variables = small range datatype values;

Byte, short, char — > int — > long — > float — > double Byte, short, and char do not convert to each other. They are first converted to int in the operation

  • Cast: Represents a conversion from a large range of data types to a small range of data types. This is called cast

Cast format: small range datatype variable = (small range datatype) large range datatype value;

19. Operators and priorities

Arithmetic operator

+ – * / % ++ —

+ : plus sign, plus, concatenation string **++, –** after the operator, first use the original value of variable a to participate in the operation, after the operation, the value of variable A increment or decrement by 1; **++, –** prefixes the variable a by incrementing or subtracting it by 1, and then uses the updated value to perform the operation.

  • Notes:
    • When concatenating strings, the addition operator should note that it will only be converted to a string if it is added directly to the string.
    • Division When both sides are integers, take the integer part and round the remainder. When one side is floating point, the normal division is performed.
    • % is mod of divisor, mod of decimal is meaningless. The resulting sign is the same as the mod sign.
    • Integer does the dividend, 0 cannot do the divisor, otherwise an error is reported.
    • If you divide a decimal by 0, you get Infinity. If you modulo 0, you get NaN

Assignment operator

*+= -= = /= %=

  • The **+=, -=, *=, /=** assignment operators include a cast operation that casts the left and right results to the left
  • Note: The left-hand side of the assignment operator must be a variable
int n = 10; byte by = 20; by += n; By = (byte)(by + n);Copy the code

Comparison operator

= =! = < > <= >=

  • The result can only be true and false
  • A comparison between characters, in terms of their ASCII values
  • A float is compared to an integer and returns true as long as the values are equal

Logical operator

& with — — — — — false&true – False | | or — — — — — False true — – true ^ xor — — — — — true ^ flase — – true! The — — — — –! True — — — — — Flase && short-circuit and — — — — — false && true — — — — — false | | short circuit or — — — — — false | | true — – true

  • The ampersand: short circuit operator, A &&b, if A is false, does not calculate the value of B. The result is false & : A &b, even if A is false, it evaluates the value of B.
  • | | : short-circuit operator, is also A | | B, if A is true, do not count the value of B, directly get the results to true | : A | B, even if A is true, also calculate the value of B.
  • Xor ^ : false if the left and right are the same, true if the left and right are different;

Ternary operator

  • Grammar:Boolean expression? Expression 1: Expression 2
  • If the Boolean expression is true, return the value of expression 1, otherwise return the value of expression 2

Operator precedence

Priority description operators 1 parenthesis (), [] 2 +, -3 +, +, -,! More than 4 battles, *, /, % 5 add and subtract 6 shift +, – < <, > >, > > > > 7 size relations, > =, <, < = 8 equal relationship = =,! = 9 bitwise and & 10 bitwise exclusive or bitwise or | ^ 11 12 13 logic or logic and && | | 14 condition operation? Operation: 15 assignment =, + =, = =, *, /, % = = 16 assignment operation & =, | =, <, < =, > > =, > > > =

An operator

  • A bit operation is a direct operation on a binary
  • In bit operations, the operands must be integers
  • Features of bitwise Xor operators:
    • One data bit is different or different from another data bit twice, the number itself is unchanged.
    • Any number and itself xor, the result is 0
    • Any number and 0 xor, the result is itself
    • So if I move one to the left, that’s the same thing as multiplying by 2: 3 << 2 = 12 –> 322 = 12
    • 3 >> 1 =1 –> 3/2=1
    • 3 >>> 1 =1 –> 3/2=1
  • For the same operation, bit operation is more efficient than arithmetic operation

Reference data types, process control statements, and arrays

1. Reference the data type

  • Variable definitions and assignment formats that refer to data types:Data type variable name = new data type ();
  • Invoke the functions of an instance of this type:Variable name. Method name ();
  • Scanner class:
    • Guide package:import java.util.Scanner;
    • Creating an Object Instance:Scanner sc = new Scanner(System.in);
    • Call method:
int i = sc.nextInt(); String s = sc.next(); // To receive the string input from the consoleCopy the code
  • Random number class Random
  • Methods introduction
Public int nextInt(maxValue) public int nextInt(maxValue) Public double nextDouble(); public double nextDouble(); public double nextDouble();Copy the code

Use mode of Random:

  • The import guide package:import java.util.Random
  • Create instance format:Random variable name = new Random();
  • The assignment:A = variable name. nextInt(maxValue);

2. Process control statements

1. if 2. if... else... 3. if... else if... else... 4. while 5. for 6. do... while... 7. Switch case default break // Case penetrability: If multiple case conditions are followed by the same execution statement, the execution statement needs to be written only once. This is a way of shorthandCopy the code

Break statement

  • Function: jump out of the circulatory body
  • use
    • Cannot be used alone. You must place the break keyword in a switch or loop statement
  • works
    • There is no need to determine any conditions, as long as the break change directly jump out of the execution of the subsequent code. It completely breaks out of the selection or loop structure
    • You can only jump out of the nearest block, not across multiple levels of code
  • tag
    • When a break statement appears in the inner loop of a nested loop, it can only break out of the inner loop. If you want to use a break statement to break out of the outer loop, you need to tag the outer loop
    • Usage: In the outer loop outside the front of a line, followed by a colon: identifier, that is, the definition is finished.
    • When using break or continue in the inner loop, the label is followed by the previously defined label
itcast: for (i = 1; i <= 9; I++) {// for (j = 1; j <= i; J++) {// inner loop if (I > 4) {// determine if the value of I is greater than 4 break itcast; // Escape from the outer loop}}Copy the code

The continue statement

  • Function: End this cycle early and continue the next cycle
  • Usage: Cannot be used alone. The continue keyword must be included in the loop statement
  • Rule of operation: no need to judge any conditions, as long as the continue change directly out of the round of the next cycle

Return, break, and continue break out of control statements

  • Break: Ends the current loop and executes the statements below the current loop.
  • Continue: Ends this loop and continues to the next one
  • Return: Terminates a method, which is returned to the upper-level caller, or, if inside the main method, terminates the program.

A return can also be used to end a loop, because a return is used to end a method. If there is a loop in the method, the loop will end no matter how many levels of loop the return is nested in. The statements following the loop are no longer executed

That’s all for the time being, due to the limited ability, if there is any problem, please correct me, I will be very grateful. The whole Java knowledge system is like a building, and the basic knowledge is like the foundation of the building. Only when the foundation is firmly laid can the building stand upright. Come on, students, let knowledge change your destiny, and let science and technology change our world!