The basic concepts of Java language Beginners

Question words: write this blog is to review the main programmer from dark horse to learn the basic concepts and deepen understanding of the language of java to get started, I hope for your help. Since the entry constants, variables, operators, methods, processes, controls are easy to understand and keep in mind, do not go the length summary.

1, data type conversion (byte, short, char -> int -> long -> float -> double)

Four categories of the eight basic data types:

Automatic conversion: the range of small type is automatically promoted to the range of large type. (Byte, when short, char directly enhance the operation of int)

Manual switch (cast): a large range of values ​​can be cast to a small range.

2, define and access arrays

(1) the definition of the array:

int[] arr = new int[3];
int [] arr = new int [] {1,2,3,4,5}; // already fixed element
int[] arr = {1,2,3,4,5};

Access (2) of the array: almost like calling c language.

(3) Frequently Asked Questions: array bounds exception (ArrayIndexOutOfBoundsException is thrown an exception), an array of a null pointer exception (NullPointerException is thrown abnormal), etc.

(4) the array as a parameter and return value

// address of the array memory arrays as a method parameter, parameters passed
public static void method name (int [] arr) {}

// array as the return value of the method, returns the memory address of the array
int[] arr = getArray();
public static int [] getArray(){
     int [] arr = {1,2,3,4,5};
     return arr;
}

(5) Summary: The basic method when the parameter type, a data value is passed, the parameter method is a reference type, the address value is passed.

3, with the object class, package, constructor

(1) Overview of Object-Oriented

    Process-oriented: emphasis on steps (to put clothes -> look for pots -> add detergent -> Add Water -> soak 10min -> rub -> Cleaning -> wring -> dry)

    Object-oriented: emphasis Object (Object: washing machine, put the clothes -> On automatic washing machine -> throw clothes -> button -> dry

    Features: The complex things simple, we will become a commander from the performer.

 (2) classes and objects

     Class: is a group of related properties and behavior of the collection.

     Properties: Status information for a class of things. (Such as: name, weight, age, color)

     Behavior: one thing can do. (Eg: walking, running, called)

    Object: a class is a concrete manifestation of things. Object is an instance of class.

     Member variables: Attribute member method corresponds thing: Thing and behavior.

Relations (3) and object class

     Class is a description of a class of things, is abstract.

     Objects are instances of a class of things, it is specific.

     Class is a template object, the object is a class of entities.

 (4) the difference between member variables and local variables

     

 

  (5) package (the hidden attribute, if the need to access a property, providing public access to its methods.)

        Encapsulating step:

        Use keywords to modify private member variables; (member method can also be modified, only be accessed in this category)

        Access to member variables need a pair of getxxx method, setxxx corresponding method.

        Optimization Package:

        1) this keyword: represents the current object class where references (address value), which own the referenced object. (Which object method is called, this method represents that object. That is who to call, who represented on this).

        2) construct: When an object is created when the method is used to initialize the object constructor, the object member variable assigned to the initialization value. (Whether you build a custom configuration method, java will automatically provide a no-argument constructor. Once own definition, java default is invalid)

   (6) ArrayList class (array-variable size, including a data storage element is called.)

        Format: ArrayList <String> list = new ArrayList <String> (); a second type of data in <>, jdk7 above can be left blank.

        Common elements operations (add, delete, search):

        1) add (E e): Add trailing elements of this collection.

        2) remove (int index): delete the element at the location.

        3) get (in index): Gets the element at the specified location.

        4) size (): Returns the number of elements in this set.

        ArrayList object can not be stored basic types, the type of data stored reference only.

        

4, String class   

      definition:

// constructor Code
// constructor with no arguments
String str = new String();

// array of characters by construction
char ch[] = {'a','b','c'};
String str2 = new String(ch);

// byte array configured by
byte b[] = {97,98,99};
String str3 = new String(b);

    Common Functions:

 // boolean type methods:
equals (object ob) // string with the specified object;
equalsIgnoreCase (String str): ignore case compare

// int type methods:
length (); // Returns the length of the string
; Indexof (String str); // Returns the substring of the first occurrence of the index

 // char, String type methods:
concat (String str); // str is connected to the end of the string;
charAt (int index); // Returns the index of the char value;
subString (int beginindex); // returns a substring from a string taken beginIndex start to end of the string;
replace(CharSequencetarget,CharSequencereplacement);//将与target匹配的字符串使用replacement字符串替换。

//数组类型的方法
public char[] toCharArray();//将此字符串转换为新的字符数组。
public byte[] getBytes();//使用平台的默认字符集将该String编码转换为新的字节数组。
publicString[]split(Stringregex);//将此字符串按照给定的regex(规则)拆分为字符串数组。

5、static关键字

    (1)概述:可以修饰成员变量和成员方法,被修饰的成员是属于类的,而不是单单是属于某个对象的。(即属于类就不需靠对象         来调用。)

      (2)修饰变量和方法

        修饰成员变量:该变量称为类变量,该类中每个对象都共享同一个类变量的值,任何对象都可以更改该类变量的值。

        修饰成员方法:习惯称为静态方法,直接用类名来调用。

        静态方法调用的注意事项:

        1)静态方法可以直接访问类变量和静态方法

        2)静态方法不能直接访问普通成员变量或成员方法。反之,成员方法可以直接访问类变量或静态方法。

        3)静态方法中不能使用this关键字。

       (3)静态代码块

           定义在成员位置,使用static修饰的代码块{};

           位置:类中方法外。

           执行:随着类的加载而执行一次,优先于main方法和构造方法的执行。

6、继承、super、this、抽象类

       (1)继承:多个类中存在相同属性和行为时,将这些内容单独写在一个类中,然后多个类就不需要再定义这些属性和行为,只           要继承那一个类即可。(子类继承父类的属性和行为,子类可以直接访问父类中的非私有的属性和行为。)

          特点:1)java只支持单继承,不支持多继承。2)java支持多层继承。(顶层父类是Object类。所有的类默认继承Object,作            为父类)3)子类和父类是一种相对的概念。

       (2)继承后的特点--成员变量

          成员变量不重名:如果子类父类出现不重名的成员变量,这时的访问是没有影响的。

          成员变量重名:子类中需要访问父类中非有私有成员变量时,需要使用super关键字,修饰父类成员变量,类似于之前的this

       (3)继承后的特点--成员方法

          成员方法不重名:如果子类父类中出现不重名的成员方法,这时的调用是没有影响的。

          成员方法重名(重写):子类出现与父类相同的方法时,会出现覆盖效果,声明不变,重新实现。

          注意事项:

          1)子类方法覆盖父类方法,必须要保证权限大于等于父类权限。

          2)子类方法覆盖父类方法,返回值类型、函数名和参数列表都要一模一样。

       (4)super和this

          super:代表父类的存储空间标识(可理解为父亲的引用)

          this:代表当前对象的引用(谁调用就代表谁)

          注意:子类的每个构造方法中均有默认的super(),调用父类的空参构造。手动调用父类构造会覆盖默认的super()。super()和            this()都 必须是在构造方法的第一行,所以不能同时出现。

       (5)抽象类

          抽象方法:没有方法的方法体;      抽象类:包含抽象方法的类。

         抽象的使用和注意事项:

         继承抽象类的子类必须重写父类所有的抽象方法。否则,该子类也必须声明为抽象类。最终,必须有子类实现父类的抽象方               法,否则,从最初的父类到最终的子类都不能创建对象,失去意义。

         1)抽象类不能创建对象,如果创建,编译无法通过而报错。只能创建其非抽象子类的对象。

          理解:假设创建了抽象类的对象,            调用抽象的方法,而抽象方法没有具体的方法体,没有意义。

         2)抽象类中,可以有构造方法,是供子类创建对象时,初始化父类成员使用的。

          理解:子类的构造方法中,有默认的super(),需要访问父类构造方法。

         3)抽象类中,不一定包含抽象方法,但是有抽象方法的类必定是抽象类

         理解:未包含抽象方法的抽象类,目的就是不想让调用者创建该类对象,通常用于某些特殊的类结构设计。

         4)抽象类的子类,必须重写抽象父类中所有的抽象方法,否则,编译无法通过而报错。除非该子类也是抽象类。

         理解:假设不重写所有抽象方法,则类中可能包含抽象方法。那么创建对象后,调用抽象的方法,没有意义。

7、接口、多态

      (1)接口:方法的集合,如果说类的内部封装了成员变量、构造方法和成员方法,那么接口的内部主要是封装了方法。 

         接口的定义与类的定义方式相似,但是使用interface关键字。它会被翻译成class文件,但一定明确它并不是类,而是另外一种           引用数据类型。

         接口的使用,它不能创建对象,但是可以被实现(implements,类似于被继承)。一个实现接口的类(可以看做事接口的子              类),需要实现接口中所有的抽象方法,创建该类对象,就可以调用犯法了,否则它必须是一个抽象类。

      (2)基本的实现

         类与接口的关系为实现关系,即类是实现接口,该类可以称为接口的实现类,也可以称为接口的子类。实现的动作类似继承,           格式相仿,只是关键词不同,实现使用implements关键字。

         非抽象子类是实现接口:

         1)必须重写接口中所有抽象方法。

         2)继承了接口的默认方法,即可以直接调用,也可以重写。

         接口中的其他成员特点:

         1)接口中,无法定义成员变量,但可以定义常量,其值不可以改变,默认使用public static final修饰。

         2)接口中,没有构造方法,不能创建对象。

         3)接口中,没有静态代码块。

       (3)多态(同一行为,具有多个不同表现形式)

          前提:

          1)继承或者实现。

          2)方法的重写。

          3)父类引用指向子类对象。

       (4)多态的体现:当使用多态方式调用方法时,首先检查父类是否有该方法,如果没有,则编译错误;如果有,执行的时子类            重写后方法。

       (5)多态引用类型转换(分向上转型和向下转型)

//向上转型:子类类型向父类类型向上转换的过程,这个过程是默认的。
父类类型 变量名 = new 子类类型();

//向下转型:父类类型向子类类型向下转换的过程,这个过程是强制的。
子类类型 变量名  = (子类类型) 父类变量名;

    为什么要转型:当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则编译错误。也就是说,不能调用子            类拥有,而父类没有的方法。编译都错误,更别说运行了。这也是多态给我们带来的一点"小麻烦"。所以,想要调用子类特有            的方法,必须做向下转型。

8、final、权限、内部类、引用类型

         (1)final关键字(用于修饰不可改变内容,可以修饰类、方法和变量)

            1)类:被修饰的类,不能被继承。

            2)方法:被修饰的方法,不能被重写。

            3)变量:被修饰的变量,不能被重新赋值。

         (2)权限修饰符

            

 

           编写代码时,如果没有特殊的考虑,建议这样使用权限:

           1) 成员变量使用private,隐藏细节。

           2)构造方法使用public,方便创建对象。

           3)成员方法使用public,方便调用方法。

        (3)内部类

           将一个类A定义在另一个类B里面,里面的那个类A就称为内部类,B则称为外部类。(一个事物内部还包含其他事物,就可以             使用内部类这种结构。)

           访问特点:

           1)内部类可以直接访问外部类的成员,包括私有成员。

           2)外部类要访问类的成员,必须要建立内部类的对象。

        (4)匿名内部类:内部类的简化写法。它的本质是一个带具体实现的父类或者父接口的匿名的子类对象。

           前提:匿名内部类必须继承一个父类或者实现一个父接口

Guess you like

Origin www.cnblogs.com/zws-bugging/p/12151002.html