web123456

Java from Beginner to Master 1st Edition (Java Fundamentals)

Java from Beginner to MasterFirst Edition (Java Fundamentals)

  • I. Installation of the software
    • 1. JDK installation
    • 2. IDEA installation
  • 2. HelloWorld case
  • 3. Java comments
  • 4. Java constants
  • 5. Java data types
  • 6. Java variables
  • 7. Type conversion
    • 7.1 Automatic type conversion
    • 7.2 Forced Type Conversion
  • 8. Operators
      • 8.1 Arithmetic operators
      • 8.2 String + operations
      • 8.3 Assignment Operators
      • 8.4 Self-Increment and Self-Decrement Operators
      • 8.5 Relational Operators
      • 8.6 Logical Operators
      • 8.7 Short-Circuit Logic Operators
      • 8.8 Ternary Operators
  • 9. Data entry
  • 10. Process control
      • 10.1 Sequential structure
      • 10.2 Branch structure
          • 10.2.1.1 If Statements
          • 10.2.1.2 if... . else statements
          • 10.2.1.3 if.... .else if statement
          • 10.2.2 Switch Statements
      • 10.3 Circular structure
          • 10.3.1 For loops

I. Installation of the software

1. JDK installation

First we open the official JDK website:portal,https://www.oracle.com/java/technologies/downloads/
Once inside, we'll scroll down and find this.
在这里插入图片描述
Installation according to your own computer, installation I will not demonstrate, direct brainless click on the next step, and then calculate the installation path on the line!
Once installed, open your installation directory, click on the bin directory and copy the path above, then go and add the environment variables.
Then open cmd again and type

javac -version
  • 1

在这里插入图片描述
If you get a screen like this, you've done it.

2. IDEA installation

First open the official IDEA website:portal /idea/download/#section=windows
Then click Download, and download whatever version you want. Download it and run it.
在这里插入图片描述
在这里插入图片描述
And then there's a point right here
在这里插入图片描述
Choose all of these.I don't think there's a 32-bit 64-bit one for that one (I'm too lazy to reinstall it, here's the netmap I found)
For the rest, just select next and wait for it to install.
After the press, you may or may not want to reboot, we directly double-click, open, create a new project
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
Then he will automatically generate some files:
在这里插入图片描述
All of our Java files are going to be in this src folder
If you want your IDEA to look as good as mine, check out this post:portal

Let's get down to business.

2. HelloWorld case

We right-click on the src folder and select
在这里插入图片描述
Then he'll generate it automatically:

public class hello{
}
  • 1
  • 2

Let's change it to:

public class hello{
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5

Run results:
在这里插入图片描述
Remember, Java is just like Python, all symbols have to be in English!!!!

3. Java comments

There are two kinds of annotations in Java are://

public class hello{
    public static void main(String[] args) {
        System.out.println("Hello World!"); // I'm a note
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5

Also: /**/

public class hello{
    public static void main(String[] args) {
    /*
I'm also annotating
*/
        System.out.println("Hello World!"); 
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

// is a single-line comment, /**/ is a multi-line comment.

4. Java constants

  • Constant Type ---------------- Description ----------------------- Example
  • String constants ------- enclosed in double quotes --------- "Hello World"
  • Integer constants ---------- Numbers without decimals -------------666, -88
  • Decimal constants ---------- Numbers with decimals ----------------3.14, -5.21
  • Character constants --------- enclosed in single quotes ----------- 'A', '0', 'I'
  • The Boolean constant --------- Boolean value indicates true or false -------------- has only two values: true and false
  • The null constant ----------- a special value, the null constant ------ value: null

5. Java data types

Data type Keyword Memory footprint Range of values
byte		1				 -128 ~ 127
integer (math.)short		2				-32768 ~ 32767
					int			4		    -2(used form a nominal expression)31(raised the) nth power~ 2(used form a nominal expression)31(raised the) nth power-1
					long		8			-2(used form a nominal expression)63(raised the) nth power~ 2(used form a nominal expression)63(raised the) nth power-1
====================================================================================
floating pointfloat		4Negatives:-3.402823E+38 ~ -1.401298E-45
Positive numbers:1.401298E-45 ~ 3.402823E+38
					double		8Negatives:-1.797693E+308 ~ -4.9000000E-324
Positive numbers:4.9000000E-324 ~ 1.797693E+308
====================================================================================
characterchar		2				   0 ~ 65535
====================================================================================
booleanboolean		1				 true, false

Description:E+38means that it is multiplied by10(used form a nominal expression)38(raised the) nth power, equal, E-45multiply (math.)10negative (math.)45(raised the) nth power
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

6. Java variables

A variable is a quantity whose value can be changed during the running of a program
Essentially, a variable is a small area of memory
Syntax for creating variables:

	type name = value
  • 1

Examples:

int My = 2;
  • 1

We can just output it:

public class test_1 {
    public static void main(String[] args) {
        int My = 2;
        System.out.println(My);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述
Note: Variables in Java can only begin with a letter, an underscore, not a number, and cannot be the same as a Java keyword.
For example, class can't be used as a variable, but Class can be

7. Type conversion

7.1 Automatic type conversion

Assigning a value or variable that represents a small range of values to another variable that represents a large range of data
Paradigm: double d = 10;

Indicates data range from small to large graph
byte ---> short------|
						int --- long --- float --- double
			   char------|
  • 1
  • 2
  • 3
  • 4

7.2 Forced Type Conversion

Format:Target data type Variable name = (target data type) followed by this variable
Paradigm:

int k = (int)88.88
  • 1

8. Operators

8.1 Arithmetic operators

Symbol Role
+plus
-diminish
*                multiply (mathematics)
/                Divide The result is taken as the quotient
%               Taking a remainder The result is taken as a remainder
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

8.2 String + operations

A string is a bunch of characters but in Java we usually use char and in Python we use string.
So what is the + operation for strings, which is putting two characters together, for example:

char a = 'A'
char b = 'B'
System.out.println(a + b);
  • 1
  • 2
  • 3

(I won't write the full code here)
and then output
在这里插入图片描述
We see that the output is 131, this is why, we would have wanted to output AB, right?
But when the computer is running, he is treating him as a numerical value to be calculated
If you have learned C/C++, you should know that this is the ascall code, in the ascall code, the capital A stands for 65, B is 66, and the two add up to 131.
However, characters cannot be added to other types of data, or an error will be reported:
在这里插入图片描述
But the output is still fine:
在这里插入图片描述

8.3 Assignment Operators

Symbol Function Description
=Assignment a=10will10Assign to variable a
+=Post-add assignment a+=b, replace a+The value of b is given to a
-=Subtract and assign a-=b, replace a-The value of b is given to a
/=Divide and assign a/=b, dividing a by the value of b gives a
%=Assign the value a after taking the balance%=b, divide a by b and give ah the remainder.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

8.4 Self-Increment and Self-Decrement Operators

Symbol Function Description
++Self-incrementing The value of the variable is added to the1
		--Self-decreasing The value of the variable minus1
  • 1
  • 2
  • 3
  • Caveats:
  • ++ and - - can be placed either after or before a variable
  • When used alone, ++ and - - have the same result whether they are placed before or after the variable
  • When participating in an operation, if placed in front of a variable, take the variable first to participate in the operation, and then take the variable to do ++ or - -
    When participating in an operation, if placed after a variable, take the variable and do ++ or - - first, then take the variable and participate in the operation.

8.5 Relational Operators

Symbol Description
==					a==b Determine whether the values of a and b are equal and hold astrue, which does not hold forfalse
		 !=					a!=b Determine whether the values of a and b are not equal and hold astrue, which does not hold forfalse
		  >					a>b Determine whether a is greater than b, which holds fortrue, which does not hold forfalse
		 >=					a>=b Determine whether a is greater than or equal to b, which holds fortrue, which does not hold forfalse
		  <					a<b Determine whether a is less than b, holds fortrue, which does not hold forfalse
		 <=					a<=b Determine whether a is less than or equal to b, which holds fortrue, which does not hold forfalse
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • Caveats:
  • The relational operators of thein the endallbooleantype, either true or false.
  • Never write "==" for "=".

8.6 Logical Operators

Symbol Function Description
&Logic and a&b, both a and b aretrueThe results aretrueotherwisefalse
		 |Logical or a|b, both a and b arefalseThe results arefalseotherwisetrue
		 ^Logical differentiation a^b, a and b results differ astrue, the same asfalse
		 !logical nonce!a, the result is the opposite of that of a
  • 1
  • 2
  • 3
  • 4
  • 5

I'm not going to go into much detail here, I'll give you a string of code, run it yourself to experience it (I can't explain it clearly if I have to 2333)

public class test_02 {
    public static void main(String[] args) {
        int i = 10;
        int j = 20;
        int k = 30;

        // & false if there is false
        System.out.println((i > j) & (i > k)); // false & false
        System.out.println((i < j) & (i > k)); // true & false
        System.out.println((i > j) & (i < k)); // false & true
        System.out.println((i < j) & (i < k)); // true & true
        System.out.println("--------");
        // | true if there is true
        System.out.println((i > j) | (i > k)); // false | false
        System.out.println((i < j) | (i > k)); // true | false
        System.out.println((i > j) | (i < k)); // false | true
        System.out.println((i < j) | (i < k)); // true | true
        System.out.println("--------");
        // ^ false if same, false if different
        System.out.println((i > j) ^ (i > k)); // false | false
        System.out.println((i < j) ^ (i > k)); // true | false
        System.out.println((i > j) ^ (i < k)); // false | true
        System.out.println((i < j) ^ (i < k)); // true | true
        System.out.println("--------");
        // !
        System.out.println((i > j)); // false
        System.out.println(!(i > j)); // !false
        System.out.println(!!(i > j)); // !!false
        System.out.println(!!!(i > j)); // !!!false
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

8.7 Short-Circuit Logic Operators

Symbol Function Description
&&Short Circuit and Function and&Same, but with short-circuiting
||Shorted or Acting with|Same, but with short-circuiting
  • 1
  • 2
  • 3

Let's look at the difference between & and & & first:
在这里插入图片描述
Here I first give i=10, j=20, and then do ++ operation to compare it with 100 respectively. Then there is no doubt that it must be smaller than 100 so it is false
Then we're outputting i and j, and they both perform the ++ operation, and then we look at &&
在这里插入图片描述
Here we find that j does not perform the ++ operation, so why is that?
Obviously as long as (j++ > 100) is running then it can't be 20, so it's clear that this sentence isn't even running
This is what I mean by "short-circuiting". If the first one is false, then the second one is not executed, and the same goes for ||.

  • Caveats:
  • Logic with &, the right side is executed regardless of the left side true or false
    Short-circuit with &&, if the left side is true, the right side is executed; if the left side is false, the right side is not executed
  • Logical or |, the right side is executed regardless of whether the left side is true or false
    short or ||, if left is false, right is executed; if left is true, right is not executed

8.8 Ternary Operators

  • Format: relational expression? expression1:expression2
  • Example: a > b ? a : b
  • Calculation rules:
First calculate the value of the relational expression
If the value istrueexpression (math.)1is the result of the operation
If the value isfalseexpression (math.)2is the result of the operation
  • 1
  • 2
  • 3

Here's a snippet of code that you can try yourselves against this format:

public class test_04 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int max = a > b ? a : b;
        System.out.println(max);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

9. Data entry

Let's think about this in terms of Python for a moment:

a = input("")
  • 1

It's over, right?
So how do we do it in Java?
In Java he gave us the Scanner class for data entry, that this class should be used how?
There are three sections:

  • 1. Guide package
import java.util.Scanner;
The code to guide the package must appear above the class definition:
  • 1
  • 2

在这里插入图片描述

  • 2. Creating Objects
Scanner sc = new Scanner(System.in);
In the above format, only sc is the variable name, which can be changed, but nothing else can be changed.
  • 1
  • 2
  • 3. Receive data
int i = sc.nextInt();
This is the format, only i is a variable that can be changed, we'll talk about the rest later.
  • 1
  • 2

10. Process control

Let's start with the classification of process control statements:

  • sequential structure
  • substructure(if, switch)
  • circular structure(for, while, do...while)
    So we'll go through them in order.

10.1 Sequential structure

Sequential structure is the simplest and most basic process control in the program, there is no specific syntax, in accordance with the sequence of the code, the sequential execution of the program, the program is a large number of code is executed in this way. Simply put, it means that my code is executed in a sequential order, line by line.

10.2 Branch structure

10.2.1.1 If Statements

Format:

if (relational expression (math.)) {
body of a statement;
}
  • 1
  • 2
  • 3

Execute the process:
First calculate the value of the relational expression
in case oftrueappeals from
in case offalseJust don't do it.
Continue with the following statements
This is in fact similar to the Python's

if xxxxxx:
	xxxxxxx
  • 1
  • 2

One meaning, that is.
在这里插入图片描述
You need to put a parenthesis here, and then the entire body of the if statement should be enclosed in curly braces like a : colon in Python.
在这里插入图片描述

10.2.1.2 if...else statements

Format:

if (relational expression (math.)) {
The body of the statement;
} else {
body of a statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5

You can write that way too:

if (relational expression (math.)) {
The body of the statement;
} 
else {
body of a statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

identical
It's just like this one for Python:

if xxxxx:
	xxxx
else:
	xxxx
  • 1
  • 2
  • 3
  • 4

To show you a rough flowchart:
在这里插入图片描述

10.2.1.3 if...else if statements

There's really no need to say anything more about this
Format:

if (relational expression (math.)) {
body of a statement;
} else if {
body of a statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5

Id:

if (relational expression (math.)) {
body of a statement;
} 
else if (relational expression (math.)) {
body of a statement;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

The else if in Java is actually the same as the elif in Python, so I won't go into that.
Remember that else doesn't need a condition, but else if does.

10.2.2 Switch Statements

The switch statement should be familiar to those who have studied C/C++, and the syntax is similar.
Without further ado, straight to the format:

switch (displayed formula) {
	case (be) worth1:
body of a statement;
		break;
	case (be) worth2:
body of a statement;
		break;
	.....
	default:
body of a statement;
		break; (That's optional.)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Formatting Instructions:

  • Expression: takes the value ofbyte, short, int, charJDK5 onwards can be enums, JDK7 onwards can beString
  • case: followed by the value to be compared with the expression
  • break: means terminal, end, and is used to end theswitchstatement
  • default: Indicates that the content of the place is executed when all the cases do not match, andifstatementelsehomologous
    在这里插入图片描述

10.3 Circular structure

10.3.1 For loops

(To be continued. Update as soon as I have time)