Introduction: This is my process of learning java, summarizing some basic knowledge and simple cases
Java development kit: Install JDK
JDK directory introduction:
1. JDK/bin: stores many commands, such as javac.exe (responsible for compiling) and Java.exe (responsible for running)
2. Develop HelloWord.java source program [copy, don't ask why, pay attention to case]
The relationship among JDK,JRE and JVM, and the main structures contained in JDK and JRE
JDK=JRE+JAVA development tool (javac.exe, java.exe, Javadoc. Exe)
JRE=JVM+JAVA core class library
3. Compile HelloWord.java source program through javac tool:
First, determine whether the javac command is available. In the DOS command window, enter javac and press enter. If 'java' appears, it is not an internal or external command, nor is it running a program or processing File because the windows operation could not find the javac command file
* Handling javac unavailability:
How does the windows operating system search for a command on the hard disk?
1. Search from current directory
2. If it cannot be found in the current directory, search for a command from the path specified by the environment variable path
3. If it cannot be searched again, the above error will be reported
Configure environment variables:
1. After installation, right-click My computer, click properties, and select advanced system settings
2. Select the Advanced tab and click environment variables
3. Create a new Java in the individual user variable_ Home and PATH attributes. If they already exist, click Edit
Variable name: JAVA_HOME
Variable value: F: \ software \ install \ Java_ eleven // Configure according to your actual path
Variable name: PATH
Variable value:% JAVA_HOME%\bin
4. Create a new PATH attribute in the system variable. If it already exists, click Edit
Variable name: PATH
Variable value:% JAVA_HOME%\bin
5. Test whether the JDK installation is successful
1. Press and hold the Win+R key and type "cmd";
2. Type the command: java -version,java,javac Several commands, the following information appears, indicating that the environment variable configuration is successful;
Java consists of three blocks:
1. JavaSE(Java Standard Edition)
2. Java EE (Java Enterprise Edition)
3. JavaME(Java mini)
Java se is the foundation, and the main direction in the future is Java EE
Java language features [open source, free, pure object-oriented, cross platform]:
1. Simplicity: the concept of shielding pointers (C + + implements the bottom layer of Java language), not C language
2. Object oriented: it is more in line with people's thinking mode and easier to understand
Portability: Java programs can be compiled once and run everywhere without any modification windows and Linux operating systems (cross platform)
3. Because the two operating system kernels are different and the execution instructions are different, Java programs cannot deal with the operating system directly
4. In order to realize the interaction between Java programs and the underlying operating system, let Java run a virtual computer [JVM]
5. Multithreading: it is divided into multiple subtasks according to the. Each subtask is called a thread. It completes the running process of the main task together, which can shorten the time and improve the service efficiency
6. Robustness: garbage generated during Java language operation is automatically recycled [GC mechanism for short]
7. Security: the JVM can prevent side effects outside the system, and appropriate restrictions in the Java language enhance security (extension: the output of java compiler is not executable code, but bytecode.)
java API documentation: the class library provided by the language is called api
Start the first Java program
// Create a java source file: HelloWorld.java public class HelloWorld { /*This is the first Java program *It will print Hello World *This is a multiline comment */ public static void main(String []args) { //This is a single line comment and the entry to the program /**This is also a single line comment**/ System.out.println("Hello World");//Print Hello World } }
Naming conventions in Java
* Package name: all in lowercase
* Class name and interface name: the first letter should be capitalized
* Variable name, method name: the first word is lowercase, and the second word is uppercase
* Constant name: all letters are capitalized. If it is multiple words, it is connected with an underscore
Java program operation includes two important stages:
1. Compilation stage: normal byte files (not pure binary) are generated if they conform to Java language
Process: create a new. Java extension file (source file), which is written with java source code / source program [in accordance with Java language, you need to remember]. Use the built-in javac.exe command to compile the Java program. The path of javac java source file is used in the DOS command window. Javac is a java compiler tool / command
* A java source file can be written to generate multiple. class files
* The final file to be executed is the bytecode file / class file (deletion or not does not affect the java program)
Java program execution, but generally java source programs should not be deleted)
* After compilation, copy the class file to other operating systems and run cross platform
2. Run phase: after the JDK is installed (with a javac.exe), there is also a tool / command called java.exe (the command is mainly responsible for the run phase)
* java.exe is used in DOS command window
* Example: if there is a HelloWorld.class on the hard disk, use java HelloWorld
* Note: never write as java HelloWorld.class [wrong]
There is a public class in front of it, and the name after it must be the same as the java file name
There must be no "exception" during compilation and running
Process:
1. Open DOS command window
2. Input: java A
3. The java.exe command starts the JVM, and the JVM starts the classLoader
4. classLoader searches the hard disk for the A.class file, finds the file, and loads the byte file into the JVM
5. The JVM interprets byte file (A.class) data in binary 1010101010
6. Then the operating system performs binary and underlying hardware platform interaction
Data type:
1. Integer: byte int short long
2. Floating point type: float double
3. Character type: char
4. boolean: boolean
Priority:
From low to high: boolean,byte,short,char - > int - > long - > float - > double
Automatic type conversion: the system automatically converts from low level to high level
Cast: when is it used? Assign a high-level number to a low-level variable of the number
Operator:
1. Arithmetic operators:+ - * / "+" (connector) ++ (add 1 before) Add first and then use; otherwise, add 1 later (add after use) -- (minus 1 before) Add first and then use; otherwise, subtract 1 (add after use)
2. Assignment operator: = + = - = * = / =%=
3. Comparison operator: the comparison result is either true or false
4. Logical operators: & ^! & &||
False && False False
False && True False
False || True True
5. Bit operators (operators used to operate on binary bits): & ^ & < > > > > > (unsigned shift right)
keyword:
For special strings (words), all letters should be lowercase
(1) 48 Keywords: abstract, assert, boolean, break, byte, case, catch, char, class, continue, default, do, double, else, enum, extends, final, finally, float, for, if, implements, import, int, interface, instanceof, long, native, new, package, private, protected, public, return, short, static, strictfp super,switch,synchronized,this,throw,throws,transient,try,void,volatile,while.
(2) Two reserved words (not used now, but may be used as keywords in the future): goto and const
(3) Three special direct variables: true, false, null
Identifier:
Where you can name yourself (the character sequence used for naming various variables, methods and classes)
* Note: 1. Identifiers include letters, numbers_ (underscore) and $, which cannot start with a number or use a reserved word (keyword) in Java
2. Identifiers are simply named with meaning
3. "$" should not appear in the code. (because the $symbol will appear after compilation in the following inner classes)
Loop statement:
- for, while and do...while
for(Initial part;Judgment conditions;Iterative part){ // Code statement } while(Judgment part){ // Circulatory body // Iterative part (cannot be omitted), otherwise it is an endless loop } // initialization do { // Iteration part of loop body }while(Judgment part) // Execute before Judge
Branch statement:
if(-else if)-else statement and switch statement
if(Judgment part){ // If the Boolean expression has a value of true }else{ // If the value of the Boolean expression is false } // Multilayer judgment if(Judgment part){ // Judgment 1 }else if(Judgment part){ // Judgment 2 }else if(Judgment part){ // Judgement 3 }else{ //Execute last } // nested // Outside if(Judgment part){ // within if(Judgment part){ // If the Boolean expression has a value of true }else{ // If the value of the Boolean expression is false } }else{ // Execute last } switch(){ // The internal parameters can only be of type int short String case: Constant 1; // Statement 1 case: Constant 2; // Statement 2 case: Constant 3; // Statement 3 ... default: // Other situations // Statement 4 break; // Jump out } // Switch case matches the values in the case in turn according to the values in the switch expression. Once the matching is successful, execute the statements in the corresponding case structure. After calling, continue to execute the statements in the case until a break is encountered or the end of the switch case structure jumps out of the switch case structure
case
Calculate current total days
import java.util.Scanner; public class Araby { public static void main(String[] args) { @SuppressWarnings("resource") Scanner scan=new Scanner(System.in); System.out.print("Please enter the year:"); int year=scan.nextInt(); System.out.print("Please enter the month:"); int month=scan.nextInt(); System.out.print("Please enter the number of days:"); int day=scan.nextInt(); int sunDay=0; switch(month) { case 12: sunDay +=30; case 11: sunDay +=31; case 10: sunDay +=30; case 9: sunDay +=31; case 8: sunDay +=31; case 7: sunDay +=30; case 6: sunDay +=31; case 5: sunDay +=30; case 4: sunDay +=31; case 3: if((year%4==0&&year%100!=0)||year%400==0) { } else { sunDay +=28; } case 2: sunDay +=31; case 1: sunDay +=day; } System.out.println(year+"year"+month+"month"+day+"day\t Always:"+sunDay+"Oh, my God!"); } }
What Zodiac year does a year belong to?
import java.util.Scanner; public class ChiZod { public static void main(String[] args) { @SuppressWarnings("resource") Scanner input = new Scanner(System.in); System.out.print("Please enter the year:"); int year = input.nextInt(); switch(year%12) { case 0: System.out.println("year of the monkey"); break; case 1: System.out.println("year of the rooster"); break; case 2: System.out.println("Year of the dog"); break; case 3: System.out.println("Year of the pig"); break; case 4: System.out.println("year of the rat"); break; case 5: System.out.println("year of the ox"); break; case 6: System.out.println("Year of the tiger"); break; case 7: System.out.println("year of the rabbit"); break; case 8: System.out.println("Year of the Dragon"); break; case 9: System.out.println("year of the snake"); break; case 10: System.out.println("Year of horse"); break; case 11: System.out.println("Year of sheep"); break; } } }
Determine the maximum and minimum values in the array
public class ComSiz { public static void main(String[] args) { int i; int arr[] = new int[]{8,3,88,9,23}; //int arr[] = {,,,} int max = arr[0]; //Assign the first item to max int min = arr[0]; //Assign the first item to min for(i=0;i<arr.length;i++) { //Traverse all elements in the array if(arr[i]>max) { /*arr[0] > arr[0] flase * arr[1] > arr[0] flase * arr[2] > arr[0] true * arr[3] > arr[2] flase * arr[4] > arr[2] flase * max = arr[2]**/ max = arr[i]; } if(arr[i]<min) { /*arr[0] < arr[0] flase * arr[1] < arr[0] true * arr[2] < arr[1] flase * arr[3] < arr[1] flase * arr[4] < arr[1] flase * min = arr[2]**/ min = arr[i]; } } System.out.println("max="+max); System.out.println("min="+min); } }
multiplication table
public class Test1 { public static void main(String[] args) { int i,j;//Define variables for(i=1i<=9;i++) { /** for(initialization; Judgment conditions; Iteration section) **/ //Number of outer loop control lines for(j=1;j<=i;j++) { //The inner loop controls the number of expressions per line System.out.print(i+"*"+j+"="+j*i+"\t");//Print expression } System.out.println();//Print and wrap } } }
Calculator
public class Test3 extends JFrame { public Test3() { Container co2 = this.getContentPane(); co2.setLayout(new GridLayout(4,5)); JLabel jl1 = new JLabel("+"); JLabel jl2 = new JLabel("*"); JLabel jl3 = new JLabel("-"); JLabel jl4 = new JLabel("/"); JTextField jt3 = new JTextField(10); JTextField jt4 = new JTextField(10); JTextField jt5 = new JTextField(10); JTextField jt6 = new JTextField(10); JTextField jt7 = new JTextField(10); JTextField jt8 = new JTextField(10); JTextField jt9 = new JTextField(10); JTextField jt10 = new JTextField(10); JTextField jt11 = new JTextField(10); JTextField jt12 = new JTextField(10); JTextField jt13 = new JTextField(10); JTextField jt14 = new JTextField(10); JButton jb3 = new JButton("="); JButton jb4 = new JButton("="); JButton jb5 = new JButton("="); JButton jb6 = new JButton("="); JPanel jp5 = new JPanel(); JPanel jp6 = new JPanel(); JPanel jp7 = new JPanel(); JPanel jp8 = new JPanel(); co2.add(jp5); jp5.add(jt3); jp5.add(jl1); jp5.add(jt4); jp5.add(jb3); jp5.add(jt5); co2.add(jp6); jp6.add(jt6); jp6.add(jl2); jp6.add(jt7); jp6.add(jb4); jp6.add(jt8); co2.add(jp7); jp7.add(jt9); jp7.add(jl3); jp7.add(jt10); jp7.add(jb5); jp7.add(jt11); co2.add(jp8); jp8.add(jt12); jp8.add(jl4); jp8.add(jt13); jp8.add(jb6); jp8.add(jt14); jb3.addActionListener(e ->{//Register listener if (e.getSource()==jb3) { // e.getSource() can get, e: represents an event int a = Integer.parseInt(jt3.getText());//Defines the value of jt3 received by an integer variable a int b = Integer.parseInt(jt4.getText()); int c = a + b;//Define a variable c to receive the sum of a and B jt5.setText(""+c);//Get the final result } }); jb4.addActionListener(e1 ->{ if (e1.getSource()==jb4) { int d = Integer.parseInt(jt6.getText()); int e = Integer.parseInt(jt7.getText()); int f = d - e; jt8.setText(""+f); } }); jb5.addActionListener(e2 ->{ if (e2.getSource()==jb5) { int g = Integer.parseInt(jt9.getText()); int h = Integer.parseInt(jt10.getText()); int i = g * h; jt11.setText(""+i); } }); jb6.addActionListener(e3 ->{ if (e3.getSource()==jb6) { int j = Integer.parseInt(jt12.getText()); int k = Integer.parseInt(jt13.getText()); int l = j / k; jt14.setText(""+l); } }); this.setTitle("My calculator"); this.setBounds(300,400,600,400); this.setVisible(true); } public static void main(String[] args) { new Test3(); } }