foreach/equals/wrapper/toString/static




lesson Twelve                        2018-05-02  01:24:19




foreach:

One of the new features of JAVA5, in terms of traversing arrays and collections

Syntax: foreach( part1:part2 ) The syntax of {part3}:

  part1: Define a local variable whose type is the same as the type of the object element in part2. 

  part2: The traversed object obj, an array object, or a collection with generality. 

  part3: loop body

1 String str[] = new String[5];
2 str[0]="String array 1";
3 str[1]="String array 2";
4 str[2]="String array 3";
5 str[3]="String array 4";
6 str[4]="String array 5";
7         for (String string : str) {
8             System.out.println(string);
9         }
foreach

equals:

Located under the java.lang.Object class, the java.lang.Object class is the root parent class of all classes.

  1. Can only handle reference type variables

  2.equals compares whether the address values ​​of two reference type variables are equal

==:

  1. Basic data type, according to the value of the basic data type to judge whether it is equal, equal to return true, otherwise return false, the data types at both ends can be different, in different cases, it can also return true/false

  2. Reference data type, compare the address value of reference type variable for equality

1 public static void main(StringDemo1[] args) {
 2         String str1 = "AA";
 3         String str2 = "AA";
 4         String str3 = new String("AA");
 5         
 6         System.out.println(str1 == str2);//true
 7         System.out.println(str1.equals(str2));//true
 8         
 9 System.out.println(str1==str3);//false Because it is a reference data type, the first address value is compared
10 System.out.println(str1.equals(str3));//true because String has rewritten the equals method, the content is compared
11         
12         
13     }
equals
1 //Override the equals method
 2     public boolean equals(Object obj) {
 3         if (this == obj) {
 4             return true;
 5         } else if (obj instanceof Order) {
 6             Order o1 = (Order) obj;
 7             return this.orderId == o1.orderId&& this.orderName.equals(o1.orderName);
 8         }
 9         return false;
10
11     }
try to override equals method

The memory structure of the String class object


Wrapper class:

Each of the 8 basic data types corresponds to a class, which is a wrapper class.

  1. Boxing: The conversion between basic data types and wrapper classes. Basic data type ---> corresponding wrapper class: call the constructor of the wrapper class.

1 //Packing: basic data type ---> corresponding packaging class: call the constructor of the packaging class.
 2         Integer integer = new Integer(i);
 3         System.out.println(integer.toString());
 4         
 5         integer = new Integer("123abc");
 6 // Exception: java.lang.NunberFormatException
 7         System.out.println(integer.toString());
 8         
 9         Float f = new Float("12.3f");
10         System.out.println(f);
11         
12         boolean b = false;
13         Boolean b1 = new Boolean("false");
14         System.out.println(b1);
Packing: basic data type ---> corresponding packaging class

  2. Folding box: conversion between wrapper classes and basic data types. Wrapper class--->Basic data type: call the XXXValue() method of the wrapper class

1 int i1 = integer.intValue();
2         float f1 = f.floatValue();
3         boolean b2 = b1.booleanValue();
4         System.out.println(i);
5         System.out.println(f1);
6         System.out.println(b2);
Folding box: packaging class ---> basic data type

  3. Conversion between basic data types, wrapper classes, and String:

1 int i1 = 10;
2         
3 String str1 = String.valueOf(i1);//Basic data type, wrapper class--->String class: call the static overloaded valueOf() method of the String class
4         System.out.println(str1);
5         
6 int i2 =Integer.parseInt(str1);//String class--->Basic data type, wrapper class: call the parseXXX() method of the wrapper class
7         System.out.println(i2);
Basic data types, wrapper classes, and conversions between Strings
1 //After JDK5.0, automatic packing and folding
2         int a = 20;
3 Integer a1 = a;//Automatic boxing
4         
5 int a2 = a1;//Automatic folding

toString:

  toString() The toString() method of the java.lang.Object class is defined as follows: 

  public String toString() {
    return getClass().getName() +
    "@" + Integer.toHexString(hashCode());
  }

   1. When printing a reference to an object, the toString() method of the object is actually called by default

   2. When the class where the printed object is located does not override the toString() method in the object, then the toString method defined in the object is called.

     Returns the class where this object is located and the first address value of the corresponding heap space object entity 

   3. When the class where the object we print rewrites the toString() method, the overridden toString method is called

Usually rewritten like this, the property information of the image is returned.

1 public class demo1 {
 2     public static void main(String[] args) {
 3         Person person = new Person();
 4         System.out.println(person);
 5         System.out.println(person.toString());
 6     }
 7 }
 8
 9 class Person {
10     private String name = "XiaoMing";
11     private int age = 22;
12
13     @Override
14 // auto-generated
15     public String toString() {
16         return "Person [name=" + name + ", age=" + age + "]";
17     }
18     
19 //manual implementation
20 //    public String toString() {
21 //        return "name:" + name + " age:" + age;
22 //    }
23 }
Override of toString

static: 

 Static, can modify properties (class variables), methods, code blocks (initialization blocks), inner classes.

 Modified properties:
 1.1. All objects created by the class share this property
 1.2. When one of the objects modifies this property, it will cause other objects to call this property.
 1.3. Class variables, static-modified properties, are loaded with the loading of the class, and only one
 1.4. Instance variables, non-static-modified properties, each object is owned independently, and loaded with the creation of the object
 1.5. Static variables can be called directly through the form of "class.class variables".
 1.6. The loading of class variables is earlier than the object.
 1.7. Class variables exist in the static domain.

 

Modified method (class method):
 2.1. It is loaded with the loading of the class, and it is also unique in memory.
 2.2. It can be called directly through the "class.method" method.
 2.3. Internally, static properties or static methods can be called. Instead of calling non-static properties or methods. On the contrary, non-static methods can call static properties or static methods
 > static methods cannot have this or super keywords!
 Note: The life cycle of static structures (static properties, methods, code blocks, inner classes) is earlier than that of non-static structures, and they are recycled later than non-static structures.

1 public class demo1 {
 2     public static void main(String[] args) {
 3         sportsMan m1 = new sportsMan("aa", 23);
 4         sportsMan m2= new sportsMan("bb", 21);
 5         m1.name= "cc";
 6         m2.age= 22;
 7         m1.nation="China";
 8         
 9         System.out.println(m1);
10         System.out.println(m2);
11         System.out.println(m1.nation);
12         System.out.println(m2.nation);
13         System.out.println(sportsMan.nation);
14     }
15 }
16   
17 class sportsMan{
18 //Instance variable
19     String name;
20     int age;
21 //Class variable
22     static String nation;
23     public sportsMan(String name, int age) {
24         super();
25         this.name = name;
26         this.age = age;
27     }
28     @Override
29     public String toString() {
30         return "sportsMan [name=" + name + ", age=" + age + ", nation="
31                 + nation + "]";
32     }
33 }
static




1 public class TestScore {
 2     
 3     Vector v = new Vector();
 4
 5     @Test
 6     public void Score(){
 7 //Create a Scanner object
 8         Scanner scanner = new Scanner(System.in);
 9 System.err.println("Please enter the grade, and end with a negative number: ");
10 int maxScore = 0; //Store the highest score
11         
12         for(;;){
13 //1. Input the grade from the keyboard (the negative number represents the end of the input)
14             int score = scanner.nextInt();
15             if (score<0) {
16                 break;
17             }
18 //2. Find the highest score among all the positive scores entered
19             if (maxScore<score) {
20                 maxScore = score;
21             }
22 //3. Fill in the input result into the object v created by Vector v = new Vector.
23 Integer score1 = score;//Convert the basic data type to a wrapper class
24 v.addElement(score1);//The parameter of the addElenment method must be an object
25         }
26         
27 //4. Obtain the elements filled in v through v.elementAt(i) in turn and judge the score level and output at the same time.
28         for (int i = 0; i < v.size(); i++) {
29 int score = (int) v.elementAt (i);
30               System.out.println(score);
31 //              System.out.println( v.elementAt(i));
32               char level;
33                 if(maxScore - score <= 10){
34                     level = 'A';
35                 }else if(maxScore - score <= 20){
36                     level = 'B';
37                 }else if(maxScore - score <= 30){
38                     level = 'C';
39                 }else{
40                     level = 'D';
41                 }
42 System.out.println("The student's grade is: " + score + ", the grade is: " + level);
43             }
44     }
45 }
practise

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325237595&siteId=291194637