Classes and objects (medium)

Posted by Headwaters on Wed, 24 Nov 2021 01:14:21 +0100

Classes and objects (medium)

Classes and objects (I)
an introduction to
Composition of classes
Three characteristics of class
How to make a standard class

preface

In the previous chapter, we explained the basic composition and several characteristics of classes and objects
Let's first recall the composition of classes: properties and behavior
In this chapter, we mainly talk about one type of attribute: String

Tip: the following is the main content of this article. The following cases can be used for reference

1, What is String?

All double quoted strings in Java programs are objects of the String class
First, let's look at the characteristics of String type
1. Strings are immutable, and their values cannot be changed after creation
2. Although String values are immutable, they can be shared
3. String effect is equivalent to character array (char []), but the underlying principle is byte array (byte [])

2, Use steps

Since the String type is under the java.lang package, we can use it directly without importing the package

Common construction methods of String
public String()Create a blank string object that contains nothing
public String(char[] chs)Create a string object based on the contents of the character array
public String(byte[] bys)Create a string object based on the contents of the byte array
String s = "abc";Create a string object by direct assignment. The content is abc

Sample code

public class StringDemo01 {
    public static void main(String[] args) {
        //public String(): create a blank string object without any content
        String s1 = new String();
        System.out.println("s1:" + s1);

        //public String(char[] chs): creates a string object according to the contents of the character array
        char[] chs = {'a', 'b', 'c'};
        String s2 = new String(chs);
        System.out.println("s2:" + s2);

        //Public string (byte [] bytes): creates a string object according to the contents of the byte array
        byte[] bys = {97, 98, 99};
        String s3 = new String(bys);
        System.out.println("s3:" + s3);

        //String s = “abc”; 	 Create a string object by direct assignment. The content is ABC
        String s4 = "abc";
        System.out.println("s4:" + s4);
    }
}


The most convenient way to define string format
String variable name = "string content";

The difference between the two methods of creating string objects

  • Create by construction method

    For string objects created through new, each new will apply for a memory space. Although the contents are the same, the address values are different

  • Create by direct assignment

    As long as the character sequence is the same (order and case), no matter how many times it appears in the program code, the JVM will only create a String object and maintain it in the String pool

3, String comparison

==Function of No

-Compare basic data types: specific values are compared
-Compare reference data types: object address values are compared

Function of equals method

Method introduction

public boolean equals(String s)     Compare whether the contents of two strings are the same and case sensitive

Sample code

public class StringDemo02 {
    public static void main(String[] args) {
        //Get the object by constructing the method
        char[] chs = {'a', 'b', 'c'};
        String s1 = new String(chs);
        String s2 = new String(chs);

        //Get the object by direct assignment
        String s3 = "abc";
        String s4 = "abc";

        //Compare whether the address of the string object is the same
        System.out.println(s1 == s2);
        System.out.println(s1 == s3);
        System.out.println(s3 == s4);
        System.out.println("--------");

        //Compare whether the string contents are the same
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s3.equals(s4));
    }
}

4, User login case

Case requirements

Known user name and password, please use the program to simulate user login. Give a total of three opportunities. After logging in, give corresponding prompts

code implementation

/*
    Idea:
        1:If the user name and password are known, define two string representations
        2:Enter the user name and password to log in with the keyboard, which is implemented with Scanner
        3:Compare the user name and password entered on the keyboard with the known user name and password, and give the corresponding prompt. The content comparison of the string is implemented with the equals() method
        4:Use the loop to realize multiple opportunities. The number of times here is clear. Use the for loop to realize it. When the login is successful, use break to end the loop
 */
public class StringTest01 {
    public static void main(String[] args) {
        //If the user name and password are known, define two string representations
        String username = "itheima";
        String password = "czbk";

        //Use the loop to realize multiple opportunities. The number of times here is clear. Use the for loop to realize it. When the login is successful, use break to end the loop
        for(int i=0; i<3; i++) {

            //Enter the user name and password to log in with the keyboard, which is implemented with Scanner
            Scanner sc = new Scanner(System.in);

            System.out.println("Please enter user name:");
            String name = sc.nextLine();

            System.out.println("Please input a password:");
            String pwd = sc.nextLine();

            //Compare the user name and password entered on the keyboard with the known user name and password, and give the corresponding prompt. The content comparison of the string is implemented with the equals() method
            if (name.equals(username) && pwd.equals(password)) {
                System.out.println("Login succeeded");
                break;
            } else {
                if(2-i == 0) {
                    System.out.println("Your account is locked. Please contact the administrator");
                } else {
                    //2,1,0
                    //i,0,1,2
                    System.out.println("Login failed, you still have" + (2 - i) + "Second chance");
                }
            }
        }
    }
}

summary

In this chapter, we explain the characteristics and common construction methods of String
In the next chapter, we will continue to explore the content of classes and objects.

Topics: Java