Algorithm article---java CLASSIC problem!!!

Source: Internet
Author: User

Blog Address: http://www.cnblogs.com/oumyye/
Question one: What is the difference between = = and equal?

= = and equals are compared, while the former is an operator, the latter is a method, both the base data type and the reference data type can use the operator = =, and only the reference type data can use Equals, the following describes the use of the two and the difference.
The = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator.
If the data that a variable points to is an object type, then this time involves two blocks of memory, the object itself occupies a chunk of memory (heap memory), and the variable occupies a chunk of memory, such as Objet obj = new Object (); the variable obj is a memory, and new object () is another memory, At this point, the value stored in the memory of the variable obj corresponds to the first address of the memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator. The Equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other. For example, for the following code:
String A=new string ("Heima");
String B=new string ("Heima");
Two new statements create two objects, and then use a, b to point to one of the objects, which is two different objects, their first address is different, that is, the values stored in a and B are not the same, so the expression a==b will return false, and the contents of the two objects are the same, the The expression a.equals (b) will return true.

Equals is itself a method, which is the method inside the root class Object, and all classes and interfaces inherit directly or indirectly from Object, so there is a equals () method in all classes, which are inherited.

In real-world development, we often have to compare whether the string content passed in is, for example, string input = ...; Input.equals ("Quit"), many people do not pay attention to use = = to compare, this is wrong, casually from the Internet to find a few project real-life teaching video to see, there are a lot of such errors. Remember that the comparison of strings is basically using the Equals method.

If a class does not define its own Equals method, it inherits the Equals method of the object class, and the implementation code for the Equals method of the object class is as follows:

Boolean equals (Object o) {return this==o;}

This means that if a class does not define its own Equals method, its default Equals method (inherited from the object class) is using the = = operator, and whether the object pointed to by the two variables is the same object, using equals and using = = will get the same result. If the comparison is two independent objects, the total return is false. If you are writing a class that wants to compare the contents of the two instance objects created by the class, then you must override the Equals method, and write your own code to determine at what time that the contents of the two objects are the same. Code, try the difference between equals and = =

/** = = and equals usage and difference * */public class testequlas{public static void Main (string[] args) {String s = new String ("Heima"); s Tring s2 = new String ("Heima"); System.out.println (s.equals (S2));//Output True because the String class has rewritten equalsSystem.out.println (s = = s2);//output False because the addresses of the two are different/ /create three animal objects//a1 and a2name and age are the same Animal a1 = new Animal ("Tom", 5); Animal A2 = new Animal ("Tom", 5);//First try to compare the chain with = = SYSTEM.OUT.PRINTLN (A1 = = a2);//Output False, two object content is the same, but their reference first address is different// First comment out the Equals method you wrote, and see what the output is, Boolean B = a1.equals (A2); System.out.println (b);//The result is false, which proves to be the same equals method of the invocation's inheritance//and then we invoke the equals that we have overridden, and then look at the result B = a1.equals (A2);// Now the call is already rewritten after the method System.out.println (b);//So the print is true}}

public class Animal {private String name;    private int age;        Public Animal () {} public Animal (String name, int age) {this.name = name;    This.age = age;    } public void SetName (String name) {this.name = name;    } public String GetName () {return name;    public void Setage (int.) {this.age = age;    } public int Getage () {return age;            }//Overrides the Equals method public boolean equals (Object o) {//Determines whether two objects are the same object, and if so, returns True if (this = = O) {        return true;        }//To determine whether another object o is null, if it is null there is no need to compare, certainly not equal, directly return if (o = = null) {return false;             if (o instanceof Animal) {///If it is an instance of the current class, cast to an instance of the current class, and then compare the member variables equal Animal s = (Animal) o; Note: A member variable of type string can also be seen as a string object that needs to be compared with equals and cannot be compared with = = to compare if (This.getname (). Equals (S.name) &A            mp;& This.age = = s.age) {return true; }        } return false; }}

Question two: prime number problem
Package Com.oumyye. prime number; Import Java.util.random;public class Snippet {public    static void Main (string[] args) {    Random rd = new random (); Define a random variable    int i = rd.nextint (1000);//Get a random number    System.out.println (Isnum (i));//output result     } public     static  string isnum (int i) {    string ret =i+ "is prime";//default is prime    if (i==2)    return ret;//If it is 2 returns the default result for    (int j = 2; j < I/2; J + +) {//Set number of cycles    if (i%j==0) {    ret=i+ "not prime";//if divisible returns are not prime break    ;    }    }    return ret; return result     }}

Problem three: Solving the 5-order spiral matrix problem

1 2 3) 4 5

16 17 18) 19 6
15 24 25) 20 7
14 23 22) 21 8
13 12 11) 10 9

public class Lxjzqj {public static void main (string[] args) {int n = 5;        0: Right, 1: down, 2: Left, 3: up int direction = 0;        row, column int row = 0, col = 0;        int num = 0;        int[] Array = new INT[N * n];            while (Array[row * n + col] = = 0) {num++;            Array[row * n + col] = num;                switch (direction) {case 0:col++;            Break                Case 1:row++;            Break                Case 2:col--;            Break                Case 3:row--;            Break                if (row = = N | | col = = N | | row = = 1 | | col = =-1 | | array[row * n + col]! = 0) {                direction++;                if (Direction = = 4) Direction = 0;                    switch (direction) {case 0:row++;                    col++;                Break CASE 1:row++;                    col--;                Break                    Case 2:row--;                    col--;                Break                    Case 3:row--;                    col++;                Break                }}} for (int i = 0, i < n; i++) {for (int j = 0; J < N; j + +) {            System.out.printf ("%-3s", Array[i * n + j]);        } System.out.println (); }    }}

Algorithm article---java CLASSIC problem!!!

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.