Dark Horse programmer _ Summary of common java Problems

Source: Internet
Author: User

1. minor issues with the equals method [java] public class Student {public static void main (String [] args) {synchronized SC = new synchronized (System. in); System. out. println ("Enter the User name:"); String username = SC. nextLine (); System. out. println ("enter the password:"); String password = SC. nextLine (); if (username. equals ("admin") & password. equals ("admin") {System. out. println ("true");} else {System. out. println ("false") ;}} public class Student {Public static void main (String [] args) {synchronized SC = new synchronized (System. in); System. out. println ("Enter the User name:"); String username = SC. nextLine (); System. out. println ("enter the password:"); String password = SC. nextLine (); if (username. equals ("admin") & password. equals ("admin") {System. out. println ("true");} else {System. out. println ("false") ;}} why is the above equals method not rewritten but correct, and the following code must be rewritten to the equals method? When will this equals method be rewritten?? [Java] public class Student {private String name; private int age; public Student (String name, int age) {this. name = name; this. age = age;} public static void main (String [] args) {Student st1 = new Student ("zhang", 20); Student st2 = new Student ("zhang ", 20); if (st1.equals (st2) System. out. print ("Equal"); else System. out. print ("not equal");} public boolean equals (Object obj) {if (obj = null) return false; else {If (obj instanceof Student) {Student c = (Student) obj; // forcibly convert if (c. name = this. name & c. age = this. age) {return true ;}}return false ;}} public class Student {private String name; private int age; public Student (String name, int age) {this. name = name; this. age = age;} public static void main (String [] args) {Student st1 = new Student ("zhang", 20); Student st2 = new Student ("zhang ", 20); if (st1.eq Uals (st2) System. out. print ("Equal"); else System. out. print ("not equal");} public boolean equals (Object obj) {if (obj = null) return false; else {if (obj instanceof Student) {Student c = (Student) obj; // forcibly convert if (c. name = this. name & c. age = this. age) {return true ;}}return false ;}for your questions, you need to start with the Object class. The Obeject class is the parent class that does not explicitly inherit other classes. That is to say, the class Object is the root class of the class hierarchy. Each class uses an Object as a superclass. The root class contains the equals method. 1. Any subclass that inherits the Obeject class or directly uses the methods in the Object class, that is, directly uses the equas function in the Object class, this function is used to indicate whether another object is "equal" to this object. It compares the address value. You can either override the method, override the equals method, and define your own comparison method. Www.2cto.com 2. The String class represents a String. Strings are constants, and their values cannot be changed after creation. The string buffer supports variable strings. Because the String object is unchangeable, it can be shared. (This sentence comes from the API documentation of the String class ). For the equals method in the String class, you can understand that she compares the String content and returns true if the String content is the same; for example, String s1 = "abc "; string s2 = "abc" String s3 = "abcd", s1.equals (s2) returns true, and s1.equals (s3) returns false; 3, you can use the second one to explain the result obtained by not rewriting the equals method in the first example. In the following example, you overwrite the equals method in the Object class, and create your own comparison method for the Student class by comparing whether the names and ages are the same. At the underlying layer, the String class has already rewritten the equals method of the Object class. The equals method of the String class determines whether the two String constant character sequences are the same. If so, true is returned; while most other classes or programmer-defined classes directly call the equals method of the Object class, in this case, the equals method only determines whether the references of two objects point to the same object. If it is a class provided by jdk, check whether it has overwritten equals. If it is not overwritten and you want to use it again, you need to overwrite it by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.