JAVA basic knowledge notes 1

JAVA basic knowledge notes 1

Guide package

  1. The contents under the java.lang package do not need to be imported, and all other packages need to be imported.

Object

  1. Anonymous object: only the object on the right, without the name and assignment operator on the left.
  2. Random object: nextInt(bound:10) left closed and right open, which is [0,10). In fact, the int integer can only be taken from 0-9.

ArrayList collection

  1. For the ArrayList collection, what you get by printing directly is not the address value, but the content. If the content is empty, what you get is the empty square brackets; the object array directly printing is the address.
  2. ArrayList will be added successfully, and the return value of boolean type must be true; but other collections you will learn in the future may not be successful.
  3. Generics can only be reference types, not basic types. The packaging types are all located under the java.lang package.
    figure 1
    figure 2
    Starting from JDK1.5+, automatic boxing and automatic unboxing are supported. Automatic
    boxing: basic type -> packaging type
    automatic unboxing: packaging type -> basic type

String

  • Common 3+1 ways to create characters.
    Three construction methods:
    public String(): Create a blank string without any content.
    public String(char[] array): Create a corresponding string based on the contents of the character array.
    Public String(char[] array): Create a corresponding string based on the contents of the byte array.
    A direct creation:
    String str = "Hello";//Direct double quotes and content on the right side, and double quotes and content directly on the right are also string objects. If you don’t have new, jvm will automatically new, which can be regarded as direct assignment to the string class. Become an object.

  • String constant pool: The double-quoted string written directly in the program is in the string constant pool.
    For reference types, == compares the address value.
    For basic types, == is to compare values.
    The string written directly in double quotes is in the constant pool, and new is not in the pool.
    image 3
    Figure 4

  • It is to compare the address value of the object. If you really need to compare the content of the string, you can use two methods:

    • public boolean equals (Object obj): The parameter can be any object, only if one parameter is a string and the content is the same, it will give true; otherwise, it will return false. Note:
      Any object can be received by Object.

    The Equals method has symmetry, and a.equals(b) has the same effect as b.equals(a). It is best to ensure that the constant is before the variable, that is, the constant .equals (variable) ("abc".equals(str)); if the variable in the variable .equals (constant) (str.equals("abc")) is null, Then NullPointerException. It doesn’t matter if both are variables, for example:
    String str4 = null;
    System.out.println("abcd".equals(str4));
    System.out.println(str4.equals("abcd"));//NullPointerException

    • public boolean equalsIgnoreCase (String str), ignore case and compare content.
  • The method to obtain the function of string: public int length (): Return the length of this string.

    • public String concat(String str): Connect the specified string to the end of the string. You can also use + directly for string splicing. For example s += "word" + arr[i]+ "#";
    • public char charAt (int index): Returns the char value at the specified index. public int indexOf (String str): Returns the index of the first occurrence of the specified substring in the string.
    • public String substring (int beginIndex): Returns a substring, intercepting the string from beginIndex to the end of the string. public String substring (int
      beginIndex, int
      endIndex): returns a substring, intercepting the string from beginIndex to endIndex. Including beginIndex, excluding endIndex.
  • String conversion function public char[] toCharArray (): Convert this string to a new character array.

    • public byte[] getBytes (): Use the platform's default character set to convert the String encoding into a new byte array.
    • public String replace (CharSequence target, CharSequence replacement): Replace the string matching the target with the replacement string.
  • String split function public String[] split(String regex): Split this string into a string array according to the given regex (rule).

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/108570432