Java learning record-fast learning based on C++

HB small salted fish learning record

  • When I was in junior high school, I learned a little bit of Java, which was a stepping stone to programs, but I didn't expect that the first in-depth study of C language and C++ when I went to college. At the beginning of this winter vacation in early 21st, I decided to learn Java and network programming at home, and post a post to record the process for future review.

[Preliminary preparation]

  • The Java JDK version selected is: "1.8.0_181"
  • The compiler I chose is: Eclipse
  • The specific environment construction method is known from the query on the Internet, so I won't say more.
  • Learning address: (Station B will always drop God)

    Dark horse programmer Java language entry to proficiency-[Basic + Advanced]-with a full set of materials (IDEA version)

[Note 1] 1- Basic grammar

  1. The include header file in C++ is similar to the import package mechanism in Java.
  2. The starting point of C++ and Java code execution is the main function.
  3. The variable keywords are basically the same as C++. It is worth mentioning that since there are no pointers in Java, strings in Java are stored by the string variable String. The usage method is similar to the string in C++/STL. Remember to capitalize the first letter of String in Java.
  4. When applying for an array in Java, it is divided into dynamic allocation and static allocation. After the dynamic allocation is new, the initial values ​​are not set, and the initial values ​​are directly set after the static allocation is new.
    int[] array1 = new int[3];//动态分配 int[] array2 = new int[]{1,2,3};//静态分配
  5. Arrays and strings in Java have their own methods similar to those in C++/STL. For example, length() is to get the length.
  6. Variable type coercion is consistent with C++ and can be converted using (int)double format.
  7. The character type consistent with C++ is also the ASCII code used.
  8. The if/else conditional judgment statement is consistent with C++.
  9. The use of the switch keyword is consistent with C++.
  10. The usage method of for loop, while loop, do...while loop is consistent with C++.
  11. The usage of break and continue is consistent with C++.
  12. The idea of ​​class in Java is basically the same as in C++. Note that public/private identifiers in Java classes do not need to add':'.
  13. The declaration/use method of classes and methods in Java is basically the same as C++.
  14. For objects that are only used once, anonymous objects can be used, just new.
  15. The final keyword stands for final and unchangeable.
  16. External class name. Internal class name Object name = new external class name (). new internal class name (); directly declare the internal class.
  17. The current compiler supports automatic boxing and automatic unboxing.
  18. Except for int–Integer and char–Character , the other types of packaging are capitalized.
  19. Generics save the trouble of type conversion and are more secure, but they can only store one type of data.
  20. The native keyword means that the method calls the method of the local operating system.
  21. When the parameter type of the method has been determined, but the number of parameters is uncertain, variable parameters can be used.
    Modifier return value type method name (data type...variable name){}The bottom layer is an array.

[Note 2] 115-118P Scanner receiving class

  1. The way that Java receives input is to call the Scanner class. Need to guide the package: import java.util.Scanner;
  2. The method of receiving data as:
    string - Scanner.next () / Scanner.nextLine ();
    other types - Scanner.next type (); eg: int-Scanner.nextInt (); double -Scanner.nextDouble ();

[Note three] 121-124P Random random number class

  1. Java generates random numbers by calling the Random class. Need to guide the package: import java.util.Random;
  2. The method of Random class to generate random numbers is similar to that of Scanner, such as Random.nextInt(100); the range is left closed and right opened, ie [0,100), 0~99.

[Note 4] 133-142P String string class

  1. No need to guide the package.
  2. String comparison method: str1.equals(str2); The same returns true. .equalsIgnoreCase() ignores case.
  3. String length: str1.length(); returns the string length.
  4. Concatenating strings: str3 = str1.concat(str2); The return value is the new string address.
  5. Get the character at the specified position of the string: char ch = str1.charAt(1); The return value is a char.
  6. Find the position of the first occurrence of the substring in the main string: int a = str1.indexOf("abc"); The return value is the index position of the first occurrence. If not, return -1.
  7. String interception method:
    str2 = str1.substring(5); //Intercept from No. 5 to the end
    str2 = str1.substring(4,7); //Intercept from No. 4 to No. 6, left closed and right open [4 ,7)
  8. Convert a string to a char array: char[] chars = "hello".toCharArray();
  9. Convert a string to a byte array: byte[] bytes = "hello".getBytes();
  10. Replace the specified content in the string: String str1 = "how do you do?".replace("o","*"); //h*wd* y*ud*?
  11. Split string: String[] array1 = "aaa.bbb.ccc".split("\\."); //Cut into three strings aaa, bbb, ccc

[Note 5] 143-147P static static keywords

  1. When modified with static, the modified method/variable no longer belongs to the object, but belongs to the class.
  2. Since it no longer belongs to the object, the method/variable with static can be called directly .
    Static method: class name. Static method ();
    static variable: class name. Static variable;
  3. Static cannot directly access non-static, for example, static methods access non-static variables.
  4. You cannot use this in static methods.
  5. For static variable initialization, we can use static code blocks. Static code blocks are executed only once when the class is first declared.
  6. Static content always takes precedence over non-static content.

[Note 6] 148-149P Arrays array tools

  1. Need to guide the package: import java.util.Arrays;
  2. Turn the array into a string: Arrays.toString();
    int[] a1 = {10,20,30};
    String s1 = Arrays.toString(a1);//[10, 20, 30]
  3. Array sorting: Arrays.sort();
    int[] a1 = {20,10,30};
    Arrays.sort(a1);//10,20,30 The default is ascending

[Note 7] 150-151P Math mathematical tools

  1. No need to guide the package.
  2. Take the absolute value: Math.abs(); consistent with C++, but the floating-point number type is also abs(), not fabs().
  3. Take the square root: Math.sqrt(); consistent with C++.
  4. Find the power of n: Math.pow(a,b); Find the power of a, which is consistent with C++.
  5. Ratio: Math.max(a,b) / Math.min(a,b); The ratio of a and b is consistent with C++.
  6. Round up: Math.ceil();
  7. Round down: Math.floor();
  8. Rounding: Math.round();
  9. There are also common mathematical values ​​in the Math package. For example, Math.PI is the pi and Math.E is the e value.

[Note 8] 171-184P interface related

  1. The interface has no static code blocks and construction methods.
  2. There is only one parent class of a class, but a class can implement multiple interfaces at the same time .
    When the method of the parent class conflicts with the default method of the interface, the parent class takes precedence .
    When implementing multiple interfaces, there is a default method conflict , and the default method needs to be rewritten.
    When multiple interfaces are implemented, there is an abstract method conflict , and it only needs to be overridden once.
  3. To use an interface, you must first define the interface class. The format for
    defining interface classes is as follows: public interface interface name {interface content}
  4. To use an interface, you need to create an interface implementation class to implement the interface. The format of the
    interface implementation class is as follows: public class implementation class name implements interface name {…}
  5. The variables defined in the interface are all constants.
    The format of the constant is as follows: public static final data type constant name = numeric value; the three keywords in front can be omitted.
  6. The interfaces are all abstract methods, and the implementation class must override all abstract methods of the interface, unless the implementation class is also an abstract class. (Must cover)
    Abstract method format such as: public abstract return value type method name (parameter list);
  7. After Java 8, default methods can be defined in interfaces. (Can be overwritten or not) The
    default method format is as follows: public default Return value type Method name (parameter list) {Method body}
  8. After Java 8, static methods can be defined in interfaces.
    The format of a static method is as follows: public static return value type method name (parameter list) {method body}
  9. After Java9, private methods can be defined in interfaces. (My Java version is 1.8.0, which is Java8, so it is not supported.) The format of
    ordinary private methods is as follows: private return value type method name (parameter list) {method body} The
    static private method format is as follows: private static return value type method name (Parameter list) {method body}

[Note 9] 185-194P Inheritance is related to polymorphism

  1. C++ inherits with a colon, and Java inherits with the keyword extends.
  2. @Override detects whether to override. Generally speaking, Java's class inheritance is very similar to C++'s class inheritance.
  3. Java does not support multiple inheritance, so there is the super keyword to find the parent class.
  4. abstract is an abstract keyword. Abstract class/abstract method.
  5. Rules for accessing member variables: access
    by object name : look at the type of object when declaring the object (on the left side of the equal sign), if not, look up. Access
    through member method : See to whom the method belongs, give priority to whose member variable is used, and look up if there is none.
  6. Realize polymorphism: The parent class references point to the child class objects.
    Polymorphic format:
    parent class name object name = new subclass name ();
    interface name object name = new implementation class name ();
  7. According to Article 6 and Article 7, in polymorphism, the member method of the subclass is preferred; the member variable of the object name is accessed, and the member variable of the parent class is preferred.
  8. "Look at the left when compiling, and on the right when running", that is, before compiling, the object is regarded as the parent type (left), the member variables at runtime are on the left, and the member methods are on the right.
  9. Personally, the existence of polymorphism makes the code more logical.
  10. Upward transformation : A new subclass object on the right, treat it as the parent class. (Cannot call subclass specific methods)
  11. Downcast : restore the parent class object to the original subclass object.
    Format: subclass name object name = (subclass name) parent class object; (a bit like a forced type conversion)
  12. Object instanceof class name : If the object belongs to this class, it returns true. Use this to perform down-casting according to the object type, and use subclass-specific methods.

[Note 10] 215-218P Object/Objects root class

  1. No need to guide the package.
  2. Object.toString() : Converted to a string, the address value of the converted object is converted by default and needs to be rewritten.
  3. Object.equals (object) : compare whether the object is equal, the address value of the compare object by default, needs to be rewritten.
  4. Objects.equals (object 1, object 2) : The equals method of the second article may have a null pointer exception, and the equals method of Objects is more complete.

【Note 11】 219-225P Date and Time

  • Date class
  1. Need to guide the package: import java.util.Date;
  2. The null parameter constructor of the Date class is to get the current date.
  3. The parameterized constructor of the Date class is to pass in a long millisecond value, and calculate the date based on the millisecond value. 0L is 0:00 on January 1, 1970. (China is East Eight District plus 8 hours)
  4. Date.getTime() : Convert the date to a millisecond value, and the return value is of type long.
  • DateFormat class
  1. Need to guide the package: import java.text.DateFormat;
  2. The DateFomat class is an abstract class, you cannot create objects directly, you can use its subclass SimpleDateFormat(String pattern)
  3. The SimpleDateFormat class needs a guide package: import java.text.SimpleDateFormat;
  4. SimpleDateFormat s1 = new SimpleDateFormat("yyyy year MM month dd day HH hour mm minute ss second"); The constructor can pass parameters according to this format, the letters cannot be changed, and the rest can be changed.
  5. String str1 = s1.format(Date object); Convert a Date object into a string in the specified format in the SimpleDateFormat object.
  6. Date date = s1.parse("20:00:00:00 on January 04, 2020"); According to the format of the SimpleDateFormat object, it is converted to a Date format object.
  • Calendar class
  1. Need to guide the package: import java.util.Calendar;
  2. The Calendar class is an abstract class, and objects cannot be created directly.
  3. Calendar c1 = Calendar.getInstance(); Use polymorphic methods to get a subclass of Calendar. The method on the right side of the equal sign is to obtain a subclass of Calendar.
  4. Calendar.get(field) : Returns the numeric value of the specified field of the calendar. Example: c1.get(Calendar.YEAR);
  5. Calendar.set(field, value) : Set the numeric value of the specified field of the calendar. Example: c1.set(Calendar.YEAR, 2020);
  6. Calendar.add(field, value) : Add a given value to the specified field, which can be a negative number. Example: c1.add(Calendar.YEAR, -2);
  7. Calendar.getTime() : Convert Calendar class to Date class. Example: Date date = c1.getTime();

[Note 12] 226-226P System system class

  1. No need to guide the package.
  2. System.currentTimeMillis(): Returns the current time in milliseconds, which can be used to calculate the running time of the program.
  3. System.arraycopy (parameter 1, parameter 2, parameter 3, parameter 4, parameter 5): Copy the data specified in the array to another array.
    Parameter 1 / Parameter 3 : Source array / target array.
    Parameter 2 / Parameter 4 : The starting position in the source array / the starting position in the target array.
    Parameter 5 : The number of array elements to be copied.

[Note 13] 227-229P StringBuilder string buffer class

  1. No need to guide the package.
  2. String buffer can improve the efficiency of string operation.
  3. The bottom layer of String is a final-modified array, while the bottom layer of StringBuilder is an array that is not final-modified and can change the length.
  4. StringBuilder(): No parameter constructor, an empty string is suggested by default.
  5. StringBuilder(String str): Constructor with parameters to create a string with str.
  6. append (parameter): add the specified parameter content to the object. The return value is "this", so there is no need to receive the return value.
  7. toString(): Convert a StringBuilder object into a String object, and return a String object, which needs to be received.

[Note 14] 234-281P container and related*

For details, please see another article of mine : link point I
introduced the Java container and its related content.

Guess you like

Origin blog.csdn.net/qq_45698148/article/details/112385217