Typical java problems !!!, Typical java Problems
Question 1: = what is the difference with equal?
= And equals are both compared, while the former is an operator, while the latter is a method. The basic data type and reference data type can both use the operator =, equals can be used only for reference data. The following describes the usage and differences of the two.
= The operator is used to compare whether the values of two variables are equal, that is, to compare whether the values stored in the memory corresponding to the variables are the same, to compare two basic types of data or whether two referenced variables are equal, you can only use the = Operator.
If the data that a variable points to is of the object type, two memory blocks are involved. The object occupies one memory (heap memory) and the variable also occupies one memory, for example, Objet obj = new Object (); variable obj is a memory, and new Object () is another memory, the value stored in the memory corresponding to the variable obj is the first address of the memory occupied by the object. For variables pointing to the object type, if you want to compare whether two variables point to the same object, it depends on whether the values in the memory corresponding to the two variables are equal, in this case, we need to use the = Operator for comparison. The equals method is used to compare whether the content of two independent objects is the same, just like to compare whether two objects have the same looks. The two objects compared by equals are independent. 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 the variables a and B to point to one of them. These two variables are two different objects whose first addresses are different, that is, the values stored in expression a and expression B are different. Therefore, expression a = B returns false, and the content of the two objects is the same. Therefore, expression. equals (B) returns true.
Equals itself is a method. It is a method in the root class Object. All classes and interfaces are directly or indirectly inherited from the Object. Therefore, equals () is used in all classes, all are inherited,
In actual development, we often need to compare whether the passed String content is equal, for example, String input = ...; Input. equals ("quit"), many people do not pay attention to it and use = for comparison. This is wrong. You can find some practical teaching videos from the Internet, there are a lot of such errors. Remember, strings are basically compared using the equals method.
If a class does not define its own equals method, It inherits the equals method of the Object class. The implementation code of the equals method of the Object class is as follows:
boolean equals(Object o){return this==o;}
This indicates that if a class does not define its own equals method, its default equals method (inherited from the Object class) uses the = Operator, it is also to compare whether the objects pointed to by two variables are the same object. In this case, using equals and using = will get the same result. If two independent objects are compared, false is always returned. If you want to write a class to compare whether the content of the two instance objects created by the class is the same, you must overwrite the equals method, it is up to you to write your own code to determine when the content of the two objects is 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 "); string s2 = new String ("heima"); System. out. println (s. equals (s2); // outputs true, because the String class has already overwritten ssystem. out. println (s = s2); // outputs 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 use = to compare various objects of the chain System. out. println (a1 = a2); // outputs false. The two objects share the same content, but their reference addresses are different. // comment out the self-written equals method first, what is the Output boolean B = a1.equals (a2); System. out. println (B); // The result is false, which indicates that the inherited equals method of the call is used. // then, we call the rewritten equals, let's look at the result B = a1.equals (a2); // The method System that has been rewritten is called now. 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 age) {this. age = age ;}public int getAge () {return age ;}// override the equals method public boolean equals (Object o) {// checks whether two objects are the same Object, if yes, return true if (this = o) {return true;} // then judge whether another object o is null. if it is null, there is no need to compare it, if (o = null) {return false ;}}
Question 2: Prime Number
Package com. oumyye. prime Number; import java. util. random; public class Snippet {public static void main (String [] args) {Random rd = new Random (); // defines a Random variable int I = rd. nextInt (1000); // obtain a random number System. out. println (isNum (I); // output result} public static String isNum (int I) {String ret = I + "is a prime number "; // The default value is the prime number if (I = 2) return ret; // if it is 2, the default result is returned for (int j = 2; j <I/2; j ++) {// set the number of cycles if (I % j = 0) {ret = I + "not a prime number"; // return ret if the integer is not a prime number break; // return result }}
Question 3: solving the fifth-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 to int ction = 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 ("%-3 s", array [I * n + j]);} System. out. println ();}}}