Java core knowledge point learning ---- comparison of equals and =, Singleton Mode

Source: Internet
Author: User

I recently found that my learning ability has slowed down. I want to find out if I have not built a solid Java Foundation. The next series of articles will mainly record my learning of the most basic Java knowledge points. i. comparison of equals and = first look at the example: copy the code package com. amos;/*** @ ClassName: example test * @ Description: Comparison of equal and = in Java * @ author: amosli * @ email: amosli@infomorrow.com * @ date Apr 10,201 4 1:14:10 AM */public class synchronized test {public static void main (String [] args) {int a = 1; float B = 1.0f; System. out. println (a = B); // true String c = new String ("hi_amos"); String d = new String ("hi_amos"); System. out. println (c = d); // false System. out. println (c. equals (d); // true} There are two main methods to judge whether two variables are equal in Java: one is to use the = Operator, the other is to use the equals method to determine whether the two are the same. 1 ). when = is used to determine whether two variables are equal, if both variables are of the basic type and both are numerical type, the data type is not required to be strictly the same, as long as the values of the two variables are equal, returns true. 2 ). if two variables of the reference type point to an object, = returns true. = cannot be used to compare two objects with no parent-child relationship. when the same new String is interpreted as above, = is used to determine whether the two are the same and false is returned, while equals returns true. equa The ls method is applicable to the String class and its source code can be viewed. In fact, equals can only be regarded as a special case of =. as shown in the following source code: copy the public boolean equals (Object anObject) {if (this = anObject) {return true;} if (anObject instanceof String) {String anotherString = (String) anObject; int n = value. length; if (n = anotherString. value. length) {char v1 [] = value; char v2 [] = anotherString. value; int I = 0; while (n --! = 0) {if (v1 [I]! = V2 [I]) return false; I ++;} return true ;}} return false ;}copy the code equals method and call the = discriminant from the very beginning, then, determine whether the value is correct. II. java Singleton mode Singleton Mode features: 1. A singleton class can only have one instance. 2. The Singleton class must create its own unique instance. 3. The Singleton class must provide this instance to all other objects. Copy the code package com. amos;/*** @ ClassName: Singleton * @ Description: The most common Singleton mode in Java * @ author: amosli * @ email: amosli@infomorrow.com * @ date Apr 10,201 4 1:12:00 AM */public class Singleton {public static Singleton instance; public static Singleton getSingleton () {if (instance = null) {instance = new Singleton ();} return instance;} // privatize the constructor private Singleton () {} public static void main (String [] args) {Singleton singleton = Singleton. getSingleton (); Singleton singleton2 = Singleton. getSingleton (); System. out. println (singleton = singleton2); // true String string = new String ("hi_amos"); String string2 = new String ("hi_amos"); System. out. println (string = string2); // false }}

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.