Java from entry to abandon series

I have learned Java for nearly four months. I have to keep something, otherwise I feel like I have never learned it. Therefore, I specially write this article to summarize the gains in the learning process and share it with friends who are learning Java. I hope it can be a little help to you.

1. JDK installation and Java environment variable configuration

Two, tool introduction

1. JCreator
has been a development tool for a long time, and it is still available now. For newbies who are just getting started, this is not bad. Simple and easy to use, but not powerful enough.
For details on the installation process, please see here: https://blog.csdn.net/Bertil/article/details/107385842

2.
The eclipse development tool is used by most people, after all, the function is also very powerful and you can install the genuine version for free.
For details on the installation process, please see here: https://blog.csdn.net/qq_38566465/article/details/79484286

3. IDEA is
very nb. In addition to the basic functions of the first two, it can also make front-end web design and so on. The function is very powerful and is loved by the majority of back-end developers. Of course, the more powerful the function, the more memory will be occupied, so the premise of using this is to ensure that the CPU of your computer can withstand it. In addition, the activation of IDEA will also be restricted. Students with campus corporate mailboxes can directly activate the original version. I am not very clear about other situations. I search the Internet for details.
For details on the installation process, please see here: https://blog.csdn.net/weixin_43184774/article/details/100578786

The tool is just an aid, the ability is the key. My Java teacher once encountered a student who could directly type a long code with a notebook. This is the realm of the strong. But the prerequisite is to install the JDK and configure the environment variables before you can compile and run the program in the character terminal; secondly, the foundation must be very solid, after all, there is no code assist function.

3. About API document download and usage instructions

1. Download
For details on the download process, please see here: https://blog.csdn.net/Bertil/article/details/107335554

2. How to use
it is difficult to explain this time, directly above the picture (Source: Java Programming Basics 6th Edition Experimental Guide)
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Four, Java basic knowledge

(1) Data types
Data types are divided into two types: basic data types and reference data types (also called composite data types)
1. Basic data types
Integer type: byte short int long
floating point type: float double
logical type: boolean
character type : Char
2. Reference data types,
including classes, arrays, interfaces, etc.

For the introduction of data types, please see here: https://blog.csdn.net/Bertil/article/details/107386628

(2) Keywords and identifiers
1. Keywords
Insert picture description here
Insert picture description here
2. Identifiers and naming rules
Identifiers: the valid character sequence used to represent variable names, class names, method names, array names, and file names.

Naming rules:
(1) It can be composed of letters, numbers, underscores (-), and dollar signs ($).
(2) Must start with a letter, underscore or dollar sign, not with a number.
(3) Keywords cannot be used as identifiers.
(4) It is case sensitive.

Coding habits: the first letter of the class name is capitalized, and the first letter of variables, methods and objects is lowercase.

(3) Data type conversion is
generally divided into automatic conversion and forced conversion.

1. Automatic type conversion
① The data type before conversion is compatible with the type after conversion.
②The display range of the converted data type is larger than that before the conversion.
Condition ② indicates that when performing operations on different types of data, they must be converted to the same type first, and then the operations must be performed. The priority relation of conversion from "short" to "long" is:
byte→short→char→int→long→float→double
2. Coercive type conversion
If you want to convert longer data into shorter data (unsafe) , It is necessary to force type conversion. The format of forced type conversion is as follows:
Insert picture description here
For details on data type conversion, please see here: https://blog.csdn.net/Bertil/article/details/108176556

(4) Keyboard input operation
Scanner is a class dedicated to input operations, and you can use this class to input an object.

例如:
Scanner reader = new Scanner(System.in);
int a = reader.nextInt();

The created reader object can call the nextInt() method to read the int type data entered by the user from the keyboard, or use the reader object to call the following methods to read the corresponding type of data entered by the user on the keyboard:
nextByte()
nextDouble()
nextFloat()
nextInt()
nextLong()
nextShort()
next()
nextLine()
In addition, the reader object can also call nextByte(), nextFloat(), nextDouble(), nextInt() and so on.

After next×××() is called, it waits for the user to input data on the command line and press Enter (or space bar, Tab key) to confirm.
The next() and nextLine() methods wait for the user to input a line of text on the keyboard, and then return a String type of data.

The difference between next() and nextLine():
next(): You must read the valid characters before you can end the input. For the end characters such as the space bar, Tab key or Enter key before entering the valid character, it will automatically If it is removed, only after valid characters are input, the method will treat these symbols input later as separators.
nextLine(): The ending character is the Enter key, that is, all characters before Enter are returned.

(5) Operators and expressions
Insert picture description here
For details on operators and expressions, please see here: https://blog.csdn.net/Bertil/article/details/108297472

(6) Three annotation methods
Insert picture description here

(VII) Process control
1. Three structures of sequence, branch, and loop
For details of the three structures, please see here: https://blog.csdn.net/Bertil/article/details/108308128

2. Jump statement in the loop
break statement: make the flow of the program jump out of a statement block (switch or loop structure).
continue statement: terminate the current round (time) of the loop, and enter the next round (time) of the loop.
return statement: used to make the program return from the method (function) and return a value.

Five, Java classes and objects

(1) Definition of object-oriented and class
1. Class and object are the core concepts in object-oriented programming methods.

Class is the description (commonality) of a certain type of things, which is an abstract and conceptual definition; while the object is a concrete individual (personality) that actually exists in that type of thing, so it is also called an instance. The class describes the properties of the object (static characteristics) and the behavior of the object (dynamic characteristics). The Object is an instance of the Class, a real individual.

When a novice is just learning object-oriented, it may be a little abstract to understand, but you can do some basic class design questions to promote understanding. The focus of object-oriented programming thinking is the design of classes, not the design of objects.

(2) Basic characteristics of object-oriented

1. Encapsulation : Use abstract data types to encapsulate data and data-based operations, protect data and conceal specific details, and only retain limited interfaces to connect with the outside world.
2. Inheritance : make the object of the special class (subclass) have all the attributes and methods of the general class (parent class), and at the same time can add its own attributes and methods.
3. Polymorphism : the coexistence of multiple different methods with the same name in a program. Commonly used overloading and coverage.

(3) The grammar
Insert picture description here
of the definition of the class The access control symbol of the class is only public; abstract and final are mutually opposed and cannot be used in the definition of a class at the same time.

Let's look at an example- the definition of the cylinder class.

class Cylinder   //定义圆柱体类Cylinder
{
    
    
    double radius;     //声明成员变量radius
    int height;        //声明成员变量height
    double pi=3.14;   //声明数据成员pi并赋初值
    void area( )   //定义成员方法area(),用来计算底面积
      {
    
    
          System.out.println("圆柱底面积="+ pi*radius* radius);
      }
     void volume( )   //定义成员方法volume (),用来计算体积
      {
    
    
         System.out.println("圆柱体体积="+((pi*radius* radius)*height);
      }
}

(4) The difference between local variables and member variables

1. From the grammatical form of view, belongs to the class member variables, and local variables are the variables or parameters of the method defined in the method; variable member can be public, private, and other static modification, and local variables can not, can be both final modification.
2. From the perspective of how variables are stored in memory , member variables are part of the object, the object is stored in the heap memory, and the local variable is stored in the stack.
3. On the survival time variables in memory look, member variables are part of the object, creating an object as it exists; local variables with the method call is generated, with the end of the method call disappear.
4. If a member variable is not assigned an initial value, it will be automatically initialized to 0 by default (member variables modified with final but not modified by static must be explicitly assigned); local variables will not be assigned automatically, and must be explicitly assigned.

(5) Object creation and use
1. Steps
to create an object
Declare the variable pointing to the "object created by the class"; use new to create a new object and assign it to the previously declared variable.

2. The use of
objects The format of referencing object members through objects is as follows:
object name. object member For
example, volu.radius = 2.8; volu.height = 5;
if the reference is a member method, as long as the member method name is in parentheses Simply provide the required parameters, if the method does not require parameters, use empty parentheses. Such as: vlu.area()

For multiple objects of a class, their member variables are allocated in different memory, so when the member variables of a certain object are modified, the others are not affected.

(6) Calling methods in class definitions In the definition of
the same class, a method can directly call other methods of this class without adding object names.
To emphasize that it is a "member of the object itself", you can add this keyword before the member name, that is, "this. member name". At this time this represents the object that calls this member. For example:
Insert picture description here
(7) Parameter transfer method
1. Call method with variable as parameter
2. Method call with array as parameter or return value

For details on parameter passing, please see here: https://blog.csdn.net/Bertil/article/details/108308489

(8) Anonymous object
When an object is created, when the method of the object is called, it is not necessary to define the reference variable of the object, but directly call the method of the object . Such an object is called an anonymous object .

例如,若将
      Cylinder volu=new Cylinder();
      volu.SetCylinder(2.5, 5,3.14);
     改写为:
      new Cylinder().SetCylinder(2.5, 5,3.14);Cylinder()就是匿名对象。

To use anonymous objects, there are usually two situations as follows:
1. If only one method call is required for an object.
2. Pass the anonymous object as an argument to a method call.

:一个程序中有一个getSomeOne()方法要接收一个MyClass类对象作为参数,方法的定义如下:
    public static void getSomeOne (MyClass c)
    {
    
    
        ……
     }
     可以用下面的语句调用这个方法。
                   getSomeOne(new MyClass( ) );

(9) Java exception handling mechanism
1. Basic concepts of exception handling
Insert picture description here

2.Java exception handling mechanism
Insert picture description here

For details on Java exception handling, please see here: https://blog.csdn.net/Bertil/article/details/106402047

Six, OJ classic examples in practice

(1) Object sorting
Insert picture description here
Insert picture description here

import java.util.*;
public class Main {
    
    
    public static void main(String[] args) {
    
    
    	Scanner sin = new Scanner(System.in);
		int count,weight;
		String name;
		if(sin.hasNextInt())
			count = sin.nextInt();
		else 
		{
    
    
			System.out.println ("error input");
			return;
		}
		if(count <= 0)
		{
    
    
			System.out.println ("error input");
			return;
		}
		Cat[] catArray = new Cat[count];
		for (int i = 0; i<count; i++)
		{
    
    
			name = sin.next();
			if(sin.hasNextInt())
				weight = sin.nextInt();
			else 
			{
    
    
				System.out.println ("error input");
				return;
			}
			catArray[i] = new Cat(name,weight);
		}	
        Cat.printCats(catArray); 
        Arrays.sort(catArray, new CatNameComparator()); 
        Cat.printCats(catArray);
        Arrays.sort(catArray, new CatWeightComparator());   
        Cat.printCats(catArray);
    }
}
class Cat{
    
    
	String name;
	int Weight;
	public Cat(String name,int Weight){
    
    
		this.name = name;
		this.Weight = Weight;
	}
	public static void printCats(Cat[] catArray){
    
    
		for(int i = 0;i < catArray.length;i++){
    
    
			System.out.println ("name="+catArray[i].name+" "+"weight="+catArray[i].Weight);
			
		}
		System.out.println ();
			
	}
}
class CatNameComparator implements Comparator<Cat>{
    
    
	public int compare(Cat c1,Cat c2){
    
    
		return c2.name.compareTo(c1.name);
	}
}
class CatWeightComparator implements Comparator<Cat>{
    
    
	public int compare(Cat c1,Cat c2){
    
    
		return c2.Weight - c1.Weight;
	}	
}

(2) Common methods in the String class
Insert picture description here

import java.util.*;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner reader = new Scanner(System.in);
        String str1 = reader.next();
        String str2 = reader.next();
        if(str1.equals(str2))
        	System.out.println ("true");
        else
        	System.out.println ("false");
        int idx,count=0;
        idx=str1.indexOf(str2);
        while(idx>=0)
        {
    
    
        	idx=str1.indexOf(str2,idx+str2.length());
        	count++;
        }
        System.out.println (count);
        int lastIndex = str1.lastIndexOf(str2);
        System.out.println (lastIndex);
        String newstr1 = str1.toUpperCase();
        System.out.println (newstr1);
        String newstr2 = str2.toLowerCase();
        System.out.println (newstr2);
        int num1=0;
        for(int i=0;i<str1.length();i++)
        {
    
    
        	if(Character.isDigit(str1.charAt(i)))
        	num1++;
        }
        System.out.println (num1);
        int num2=0;
        for(int i=0;i<str2.length();i++)
        {
    
    
        	if(Character.isLetter(str2.charAt(i)))
        	num2++;
        }
        System.out.println (num2);
        System.out.println (str1.replace("abc","cde"));
    }
}

Guess you like

Origin blog.csdn.net/Bertil/article/details/107334869