Recursive process parsing of Java string
This article introduces the relevant knowledge of "recursive process parsing of Java strings". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Basically every class in Java inherits from Object, and standard container classes are no exception. Therefore, all container classes have a toString () method, and the method is overridden so that the String results it generates can express the container itself and the objects contained in the container.
For example, ArrayList.toString (), which iterates through all the objects contained in the ArrayList, calls the toString () method on each element:
Public class Person {private int age; private String name; public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getName () {return name;} public void setName (String name) {this.name = name;} public Person (int age, String name) {super (); this.age = age; this.name = name;} @ Override public String toString () {return "Person [age=" + age + ", name=" + name + "]" } public static void main (String [] args) {ArrayList persons = new ArrayList (); Person p1 = new Person (10, "Zhang San"); Person p2 = new Person (12, "Li Si"); persons.add (p1); persons.add (p2); / / (1) and (2) output the same System.out.println (persons); / / (1) System.out.println (persons.toString ()); / / (2)}}
The output is as follows:
[Person [age=10, name= Zhang San], Person [age=12, name= Li Si]] [Person [age=10, name= Zhang San], Person [age=12, name= Li Si]]
If you want the toString () method to print out the memory address of the object, you might consider using the this keyword:
Public class Person {private int age; private String name; public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getName () {return name;} public void setName (String name) {this.name = name;} public Person (int age, String name) {super (); this.age = age; this.name = name;} @ Override public String toString () {return "Person address:" + this } public static void main (String [] args) {ArrayList persons = new ArrayList (); Person p1 = new Person (10, "Zhang San"); Person p2 = new Person (12, "Li Si"); persons.add (p1); persons.add (p2); System.out.println (persons); / / (1) System.out.println (persons.toString ()); / / (2)}}
When you create a Person object and print it out, you get a very long list of exceptions. If you save the Person object into an ArrayList and then print the ArrayList, you will get the same exception. In fact, when the following code runs:
"Person address:" + this
Automatic type conversions occur here, with Person types converted to String types. Because the compiler sees a String object followed by a "+", and the next object is not String, the compiler tries to convert this into a String. How does it convert? It is by calling the toString () method on this that a recursive call occurs.
If you really want to print out the memory address of the object, you should call the Object.toString () method, which is the method responsible for this task. Therefore, instead of using this, you should call the super.toString () method.
That is, change the above toString () method to:
@ Override public String toString () {return "Person address:" + super.toString ();}
This is the end of the content of "Recursive process parsing of Java string". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!