java foundation (2)

String method

  Two methods of string instantiation
        - direct assignment: will open up a space in the heap memory, and automatically enter the pool
        - construction method: will open up two memory spaces in the heap memory, will not automatically enter the pool, to achieve the pool , to use intern()

 Replace the content of the string that meets the conditions
 public String replaceAll(String regex,String replacement) replace all strings that meet the conditions
 public String replaceFirst(String regex,String repalcement) Replace the first string that meets all the conditions

 Intercept the content of the string
 public String substring(int beginIndex) intercepts all the content from the specified index after the specified index
 public String substring(int beginIndex, int endIndex) intercepts the content from the specified start index to the end index

 Splitting of strings
 public String[] split(String regex) splits into string arrays according to the specified characters
 public String[] split(String regex, int limit) splits according to the specified character marks and splits them into specified sizes array of

  Partial splitting
  For the splitting of some sensitive characters, escape can be used before splitting, such as: "\\|", "\\."

  Convert letter size
  public String toLowerCase() to convert uppercase to lowercase
  public String toUpperCase() to convert lowercase to uppercase

  ·public String trim() cancels the spaces at both ends of the characters, and the spaces in the middle will not be canceled.
  String objects cannot be changed frequently, and a lot of garbage will be generated. Anonymous objects have heap memory but no stack memory.

  Putting anonymous objects in front of string comparisons can avoid null pointer exceptions

  Passing of reference data
    A heap memory space can be pointed to by multiple stack memory addresses
    A stack memory can only point to a heap memory space

Methods of StringBuffer

-Construction method:

· public StringBuffer (String str)
- implements string appending
· public StringBuffer append(String str)
- implements string inversion
· public StringBuffer reverse(String str)    
- adds data at the specified position
· public StringBuffer insert(int offset, String str)
- delete some data
public StringBuffer delete(int start, int end)

After the String declaration, the content is immutable. StringBuffer considers thread safety, while StringBuilder does not consider thread safety        

. Use StringBuffer (memory overflow problem)  

interface

如果一个类中只有全局常量和抽象方法,但是没有构造方法,那么这个类就是接口,在接口中可以定义静态方法,但是没有意义
·实例变量:在类中声明但是没有使用static修饰的常量
·静态变量:(类变量)在类中声明但是使用了static修饰的常量
·局部变量:在方法中声明的变量,一旦方法被执行完就会被销毁
·全局常量:在类中声明,并且使用static和final修饰的变量
    接口要有意义就必须被子类实现,要使用interface关键字,如果子类不是抽象类,那么要实现接口中的所有抽象方法
    接口解决了单继承的局限性可以多实现,接口之间也可以使用继承关键字extends,接口也可以实现向上转型
继承
    继承解决了代码的重复问题,在实例化子类的时候会默认调用父类的构造方法,如果定义了有参的构造方法那么无参的就不存在了
    父类封装的属性可以被子类继承,这样的继承被称为隐式继承。
覆写与重载
    覆写(override)表示方法的重写
        -方法名,参数列表,返回类型相同
        -发生在继承关系中
        -子类不能拥有比父类更严格的访问权限
    重载(overload)表示方法的重载
        -方法名相同,参数列表不同
        -对返回值没有要求,但是再开发中返回值要求保持一致
        -对访问权限没有要求,但是在开发中要求保持一致        
    方法的递归
        -方法的递归就是方法自己调用自己    
多态
    多态实现了类参数的统一,要实现多态就必须以继承或者实现为基础,那么多态的表现形式有两种:
        -方法的多态:重载,重写
        -对象的多态:向上转型,向下转型
            ·向上转型:父类 变量=子类实例化对象
            ·向下转型:子类 变量=(强转)父类对象【先必须实现向上转型】    
     多态机制的原理:可以使用父类或者接口的引用变量指向具体子类的实例化对象,在程序调用方法时是在运行时动态绑定的所绑定的方法是具体子类或者实例对象中的方法,而不是引用类型中的方法

向下转型存在类型转换错误的风险,用instanceof关键字来判断一个类是否是它的子类,如 person instanceof Student    

       抽象类
         抽象类有自己的构造方法,但是不能实现不能直接实例化对象,如果子类不是抽象类,那么子类必须覆写抽象类的所有方法
         抽象类可以没有抽象方法,但是有抽象方法的一定是抽象类或者接口,在开发中抽象类可以设计成模版模式
         静态属性
         static定义的属性就是静态属性,静态属性不依赖实例化对象的存在,可以使用类名.属性名调用。静态方法中不能访问
         非静态属性、非静态方法,静态方法中不能使用this关键字,非静态方法可以访问静态方法。 

 

Guess you like

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