Road of big data day04_2-- classic bug (equals and == to compare different, break out of the different)

A, equals and == to compare different

When a person to achieve five malls to go shopping, whether shopping console input (Y / N), the comparison is a problem, no matter what is found false input, access to information was later discovered, comparing string, == and equals not the same.

Look at the following example:

. 1 String S1, S2, S3 = "ABC", S4 = "ABC" ;
 2 S1 = new new String ( "ABC" );
 . 3 S2 = new new String ( "ABC" );
 . 4  
. 5 S1 == S2 is to false       // memory address is not the same two variables, that is, they are not the same object point, 
. 6  
. 7 s1.equals (S2) is to true     // content contained two variables are ABC, so equal.

we discover,

A, for string variables, the use of "==" and "the equals ()" method when comparing strings, the comparison of different methods.

 1 "==" compares two variables value itself, that two objects first address in memory.

(Java, the first address is the start address of the object it is stored in memory, which is used to store the address following the address various attributes it contains, it will use the memory to store a plurality of memory blocks each object parameters, and by the first address to find the object, and then you can find the various attributes of the object)

 2, "equals ()" included in the contents of the comparison string are the same.

Examples shopping: fortunate friends can look at the code I knocked welcome supplement.

. 1  Package day04;
 2  
. 3  Import java.util.Scanner;
 . 4  
. 5  public  class Shop_bug {
 . 6      public  static  void main (String [] args) {
 . 7          // . 5 stores, each store for later three restriction clothes 
. 8          int [] = Shops new new  int [. 5 ];
 . 9          int COUNT = 0 ;
 10          Scanner SC = new new Scanner (the System.in);
 . 11          
12 is          for ( int I = 0; I <shops.length; I ++ ) {
 13 is             System.out.println ( "Welcome" + (i + 1) + " shop for shopping!" );
 14              for ( int J = 0; J <. 3; J ++ ) {
 15                  System.out.println ( "whether to select purchase of goods (the Y / N)? " );
 16                  String A = sc.next ();
 . 17                  IF (a.equals (" the Y ") || a.equals (" N " )) {
 18 is                      IF (a.equals ( "the Y" )) {
 . 19                          COUNT ++ ;
 20 is                      } the else {
 21 is                          BREAK ;
22                     }     
23                 }else{
 24                      System.err.println ( "a problem with the way the input and later failed!" );
 25                      J-=. 1 ;
 26 is                  }
 27                   
28              }
 29              System.out.println ( "always welcome section" + ( i + 1) + "store" );
 30              System.out.println ();
 31 is              
32          }
 33 is          System.err.println ( "You purchased a total of" + count + "clothes!" );
 34 is          
35          
36      }
 37  
38 }

Two, break out of the different

In the process of knocking code, when we met for two cycles, internal to judge when sometimes jump out when the judge does not comply with the loop, the code outside of the for loop, because they do not know the scope break, leading the meaning of the questions is better to achieve the effect.

example:

 

 Here I would specially if () is written inside to execute a true break, in the first for loop, the outer loop to add a second for output statement, the results we found that, when executing a break statement in embedded sets for loop, a for loop out is present.

 

 

 

Guess you like

Origin www.cnblogs.com/wyh-study/p/11815293.html