Five basic Java, Java programming ideas (1-5)

I. Introduction to Object

1: polymorphic objects interchangeably

  Object-oriented programming language uses late binding concepts.

  When sending a message to an object, the code is invoked to determine until runtime. Also known as dynamic binding .

2: single inheritance structure

  All classes are ultimately inherited from a single base class, the name is the ultimate base classObject

3: Create and lifetime of the object

  Data objects in scope, Java completely dynamic memory is allocated (just a special case of the basic type)

Second, everything is an object

1: basic types

  The basic type is not a reference to "automatic" variable. This variable is stored directly, "value", and placed in the stack

2: The Java arrays

  Java ensure that the array will be initialized. And can not be accessed outside of its range

3: Scope (scope)

  Java objects and primitives do not have the same life cycle. When you create a Java object using new, it can survive outside the scope

  {

   String s = new String("a string");   } // End of scope

      • Quoted  s in scope end disappeared. However, the s point of String objects still continue to occupy memory space

import keyword

  Instructs the compiler to import a package, which is a class library, Java all code must be written in class. A particular class  java.lang will be automatically imported into every Java file.

5: static keyword

  • Just for a particular domain assigned a single storage space , not to consider how much space you want to create an object, do not even create any object.
  • Hope that a method does not contain any of its associated object class together. In other words, even if no object is created, you can call this method .
  • staticWhen applied to the field, it will change the way data is created
  • staticAn important use of the method is that the premise does not create any object you can call it;
  • staticThe method is not thisthe method, non-static method can not be called directly, but you can pass an object reference to the static method, and then (and by this reference thisthe same effect) to call non-static methods and access non-static data members. .

Third, the relational operator

1: == and  != the comparison is a reference to the object

2: Special Methods equals() of default behavior is relatively references

Fourth, control the flow of execution

 

1:if else

2:switch

  JDK1.7 start, switch began to support String as a selection factor. In a switch statement, String comparisons are using String.equals().

  Thus, to be noted that the switch to pass a String variable can not be null , while in the case clause string switch can not be used to null

V. initialization and cleanup

1: Method overloading

  Overloaded methods, the same method name, different formal parameter list (parameter list known parameters signature , including sequential type parameter, and the number of parameters of the parameter, as long as there is a different parameter list is called different)

2: Overload constraints

  • Declared final method can not be overridden
  • Declared as static methods can not be overloaded, but can be declared again.
  • Overload the return type may be the same or different, but only enough to become different types of return overloaded methods.

  class Animal {
    public static void walk() { System.out.println("Animal行走方法"); }
  }
  public class Horse extends Animal {
    public void walk(String s) {
    System.out.println("Horse四条腿行走");
    }
    public static void main(String [] args) {
      new Horse().walk("");
    }

  }

  Animal {class
    public static void Walk () {System.out.println ( "Animal walking method");}
  }
  public class Horse the extends Animal {
    public static void Walk () {  // redefined
    System.out.println ( "Horse four legs walking ");
    }
    public static void main (String [] args) {
    new new Horse () walk ();.
    }

  }

3: member initialization

  There are variables before using are receiving proper initialization.

  Java class member variables are automatically initialized, but local variables inside the method will not be automatically initialized

  Automatic initialization is performed, it will happen before the constructor is called

4: creation of objects

  Initialization> Executive member variables - -> the init method, the class loader checks the constructor is executed -> allocate memory -> Initialization zero value -> Set object header

5: array initialization

  initialization: 

    • int[] a = new int[3]; Initializing each element is 0, for the booleaninitial value false;
    • int[] a = {1, 2, 3}; Initializing elements respectively 1, 2, 3;

 

Guess you like

Origin www.cnblogs.com/sun-null/p/12359179.html