201711671224 "Java Programming" in Chapter 4 learning summary

Learning content summary

  • Must be defined to produce an object class, the class is a design object, the object is an instance of the class
  • Class is a generalization from a few examples to abstraction process a large number of similar instances

4.1 Classes and Objects

  • The definition of class
    • 1. The first class is defined in the program class Clothes{String color;Char size;}(definitions colorand size2. The two objects create a new object data members) new Clothes() 3. Statement Reference Name (or reference variable reference) Clothes c1; 4. with reference to the new name of the object c1 CLothes c1 = new Clothes();
    • Categories: public and non-public classes, a source can have only one public class, but can have multiple class definitions, and the main document name must be the same document open class name (Do not write more than one meter class a class)
    • this: this.color = colorThe value of the parameter refers to the object of a parameter member ( this)
    • new: Create a new object
    • Two basic standard class
      • java.util.Scanner: ScannerFor each basic type has a corresponding nextxxx()method, parsing the string to a corresponding type, a direct access to the character string used next()to obtain the entire line of textnextLine()
     import java.util.Scanner;

    public class Guess {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int number = (int)(Math.random() * 10);
        int guess;

        do{
            System.out.print("猜数(0 ~ 9):");
            guess = scanner.nextInt();
        }while(guess != number);

        System.out.println("猜中了!");
    }
}
  • java.math.BigDecimalProvide plus(), subtract(), multiply(),divide()
import java.math.BigDecimal;

    public class DecimalDemo {
    public static void main(String[] args){
        BigDecimal operand1 = new BigDecimal("1.0");
        BigDecimal operand2 = new BigDecimal("0.8");
        BigDecimal result = operand1.subtract(operand2);
        System.out.println(result);
    }
}
  • Object development and equality
    • =: A basic type, the value is copied to a variable; an operation object name specified with reference to an object reference ==: for base types, comparing two variables stored values are identical; with the operation object, compares two reference name reference whether the same object equals(): the operation target, value comparing two objects are the same

      4.2 The basic types of packaging

  • The basic types of objects in the package, the basic types as objects to manipulate
  • Packer (the Wrapper):Long , , , , ,IntegerDoubleFloatBooleanByte
package cc.openhome;
public class IntegerDemo {
    public static void main(String[] args){
        int data1 = 10;
        int data2 = 20;
        Integer wrapper1 = new Integer(data1);
        Integer wrapper2 = new Integer(data2);
        System.out.println(data1 / 3);
        System.out.println(wrapper1.doubleValue() / 3);
        System.out.println(wrapper2.compareTo(wrapper2));
    }
}
  • Automatic packing (Autoboxing) Integer wrapper = 10;compiler automatically determines whether automatic packing int data1 = 10;Integer wrapper1 = new Integer(data1);can be rewritten using an automatic packingInteger data1 = 10;
  • Automatic unboxing (Auto Unboxing) information takeout basic form of the packerInteger i = 10;//自动装箱 int foo = wrapper;//自动拆箱
  • Automatic entry boxes are compiler honey, can not let the variable reference to null object on the operation and can not exceed the package's value range
  • Whether packed two variables which range value is the same value as long as the tag is packaged, () equals the result of the comparison is true will

    4.3 Array object

  • Java, an object is an array
  • An array is a data structure having indexed, if the access exceeds the index range, an error will be thrown
  • A method for successively taking each value in the array
    • the for loop (array lengthproperty can get number of elements in the array)
public class Score {
    public static void main(String[] args){
        int[] scores = {88,81,74,68,78,76,77,85,95,93};
        for(int i = 0; i < scores.length; i++){
            System.out.printf("学生分数:%d %n",scores[i]);
        }
    }
}
  • Enhanced for loop
public class EnhanceScore {
    public static void main(String[] args){
    int[] scores = {88,81,74,68,78,76,77,85,95,93};
    for(int score : scores){
        System.out.printf("学生分数:%d %n",score);
    }
}
}
  • Declared two-dimensional array, next to the type of keyword plus [] [], cords.lengththat there are few lines cords[x].lengththat each line has several elements
public class XY {
    public static void main(String[] args) {
        int[][] cords = {
                {1, 2, 3},
                {4, 5, 6}
        };
        for (int x = 0; x < cords.length; x++) {
            for (int y = 0; y < cords[x].length; y++) {
                System.out.printf("%2d", cords[x][y]);
            }
        }
    }
} 
  • With enhanced for loop
public class EnhanceXY {
    public static void main(String[] args) {
        int[][] cords = {
                {1, 2, 3},
                {4, 5, 6}
        };
        for (int [] row : cords){
            for(int value : row){
                System.out.printf("%d",value);
            }
            System.out.println();
        }

    }
}
  • Using java.util.Arraysthe fill()method of setting new array element values
import java.util.Arrays;

public class ArrayScore {
    public static void main(String[] args){
        int[] scores = new int[10];
        for(int score : scores){
            System.out.printf("%2d",score);
        }
        System.out.println();
        Arrays.fill(scores,60);
        for(int score : scores){
            System.out.printf("%3d",score);
        }
    }
}
  • If you do copy the array, the basic approach is to build a separate new array
  • System.arraycopy()The method uses native mode copies each index element, five parameters: the source array, the source of the starting index, the purpose of the array, the starting index object, copy lengths, copy shallow
  • Array.copyof()Establish a new method without a separate array shallow copy
import java.util.Arrays;

public class CopyArray {
    public static void main(String[] args){
        int[] scores1 = {88,81,74,68,78,76,77,85,95,93};
        int[] scores2 = Arrays.copyOf(scores1,scores1.length);
        for(int score : scores2){
            System.out.printf("%3d",score);
        }
        System.out.println();
        scores2[0] = 99;//不影响score2参考的数组对象
        for(int score : scores1){
            System.out.printf("%3d",score);
        }
    }
}
  • Shallow copy: only the premises c1 each index reference objects, but also to c2 each index to refer, when used in an array of class type declarations, are executed in shallow copy
  • Deep Copy: In fact, c1 each index reference object is copied, specified separately for each traction c2

    4.4 String object

  • String of characters is an array of packaged objects are java.lang.Stringinstances of the class
  • length()Get the length of the string, charAt()specify a character string made, toCharArray()the string char[]array returned

import java.util.Scanner;

public class Sum {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        long sum = 0 ;
        long number = 0;
        do{
            System.out.printf("输入数字:" );
            number = Long.parseLong(scanner.nextLine());
            sum += number;
        }while(number != 0);
        System.out.printf("总和:"+ sum);
    }
}
  • To "" String included, as long as the contents are the same, whether it appears in the program code several times, JVM will set up a String instance and maintain the string pool
  • String objects, once established, can not change any of the content object, the object is no way to change the contents of a string

    4.5 Java API query file

  • Java query various types of various methods

Other (perception, thinking, etc.)

Study concluded complement ...... next chapter is written by a o (╥﹏╥) o

Reference material

  • "Java Programming"

Guess you like

Origin blog.csdn.net/nemeziz/article/details/84543861