Notes - javase common classes

Keywords: Java

1.String class
                 The String class is immutable and cannot be modified after the String object is declared.
                String s1  = "a";
                String s2  ="b";
                s1= s1+s2;
                 "A" and "B" are all string constant areas. Adding and subtracting strings at run time will be placed in the heap. Let's see if there is a modified string in the string constant pool,
                 If it exists, the memory address of the string is returned. If it does not exist, put the string in the string constant pool first, and then return the memory address.

                 String s1= new String("abc"); And String s2 ="abc";
                                s1 creates an object in the heap, which stores the memory address of "abc" in the string constant area. There are two new objects
                 s2 directly points to the memory address of the string "abc" in the constant area. new an object.

                 String common methods:
                     endsWith:   Determines whether the string ends with the specified suffix.
                     startsWith: determines whether the string starts with the specified prefix.
                     equals: String equality comparison.
                     equalsIgnoreCase, string equality comparison, ignoring case.
                     indexOf to get the first occurrence position of the specified string in the string.
                     lastIndexOf: gets the position of the last occurrence of the specified string in the string.
                     Length: gets the length of the string.
                     replaceAll: replaces the content specified in the string.
                     split: splits the string according to the specified expression.
                     subString: intercepts the string.
                     trim: remove front and back spaces.
                     valueOf: converts other types to strings.

public class StringTest {
    public static void main(String[] args) {
        String s1 ="abcdb";

        //Determines whether the string ends with the specified string.
        System.out.println(s1.endsWith("b"));//true

        //Determines whether the string starts with the specified string.
        System.out.println(s1.startsWith("a"));//true

        //Compare strings for equality
        System.out.println(s1.equals("av"));//false

        //Compare strings for equality, ignoring case
        System.out.println(s1.equalsIgnoreCase("abcdB"));//true

        //Gets the first occurrence of the specified string in the string.
        System.out.println(s1.indexOf("a"));//0

        //Gets the position of the last occurrence of the specified string in the string.
        System.out.println(s1.lastIndexOf("b"));//4

        //Gets the length of the string
        System.out.println(s1.length());//5

        //replaceAll: replaces the content specified in the string.
        System.out.println(s1.replaceAll("b","z"));//azcdz

        //split: splits the string according to the specified expression.
        String s2 ="my name is zhangsan";
        String[] s = s2.split(" ");//Split the string into words according to the spaces and store them in a string array

        //subString: intercepts the string.
        System.out.println(s1.substring(1,2));//b.  Intercept the section before closing and then opening

        //trim: remove front and back spaces
        String s3="   zhangsan    " ;
        System.out.println(s3.trim());//zhangsan

        //valueOf to convert other types to strings
        String s4 =String.valueOf(123465);//Convert int type 123465 to string "123456".
    }
}

2. StringBuffer and StringBuffer
                     StringBuffer is called string buffer. Its working principle is to open up a memory space to store character sequences. If the character sequences are full
                    , The size of the String buffer is changed again to accommodate longer strings. The biggest difference between it and String is that it is a variable object.
                     append() string append method.
                     The usage of StringBuffer and StringBuffer is the same. The methods in StringBuffer are synchronous methods. Is thread safe. Low efficiency.

public class StringBufferTest {
    public static void main(String[] args) {

        //Create a string buffer
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i <100;i++){

            //Append string
            sb.append(i);
            sb.append(",");
        }
        //Remove the last,.
        System.out.println(sb.substring(0,sb.length()-1));
    }
}


                 3. Package class corresponding to basic data type
                    
                     Wrapper classes of basic types mainly provide more practical operations, which makes it easier to deal with basic types. All packaging classes are
                                         final, so its subclasses cannot be created. Wrapper classes are immutable objects.

                 Packaging types corresponding to 8 basic data types

Basic data typePackaging
byteByte
short        Short
charCharacter
intInteger
long      Long
floatFloat
doubleDouble
booleanBoolean

4. Date category
                     Common date class
                        java.util.Date,
                        java.util.SimpleDateFromat

public class DateTest {
    public static void main(String[] args) {

        //Get current system date
        Date now = new Date();

        //Format date format
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss SSS");
        String nowStr = sdf1.format(now);
        System.out.println(nowStr);

        //Convert string to date format
        String date ="1937-10-1  08:00:00 000";
        try {
            Date date1 = sdf1.parse(date);
            System.out.println(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }


    }
}


                  
                 5. Digital category:
                    java.text.DecimalFormat
                    java.math.BigDecimal

public class DecimalFormatTest {
    public static void main(String[] args) {
        //Format the number format, add the thousandth, and keep 4 decimal places
        DecimalFormat df1 = new DecimalFormat("###,###.####");
        String numStr = df1.format(155646561654.156);
        System.out.println(numStr);

        //Format the number format, add the thousandth, keep 4 decimal places, and fill in zero if it is not enough.
        System.out.println(new DecimalFormat("###,###.0000").format(152.16));
    }
}
public class BigDecimalTest {
    public static void main(String[] args) {

        BigDecimal v1 =new BigDecimal(123);
        BigDecimal v2 =new BigDecimal(1225);
        BigDecimal add = v1.add(v2);

        //Additive operation
        System.out.println(add);

    }
}

6.Random
                     java.util.Rondom can generate random numbers.

public class RandomTest {
    public static void main(String[] args) {
        //Create a new random number generator.
        Random random = new Random();
        //Returns the next pseudo-random number, evenly distributing int values from the sequence of this random number generator.
        System.out.println(random.nextInt(100));

    }
}

7. Enumeration
                     Using enum, you can circle a range
                     If there are only several possible values, they can be defined as enumeration because they can be enumerated one by one.

public class EnumTest {
    public static void main(String[] args) throws Exception{
        Result r = method1(10, 2);
        if (r == Result.SUCCESS) {
            System.out.println("success!");
        }
        if (r == Result.FAILURE) {
            System.out.println("Failed!");
        }
    }
    //SUCCESS is returned correctly and FAILURE is returned
    private static Result method1(int value1, int value2) {
        try {
            int v = value1/value2;
            return Result.SUCCESS;
        }catch(Exception e) {

            return Result.FAILURE;
        }
    }
}
enum Result {
    SUCCESS,
    FAILURE
}

Posted by sublevel4 on Mon, 25 Oct 2021 04:33:08 -0700