Getting Started with Java SE (16) - Advanced API

Getting Started with Java SE (16) - Advanced API
  iwehdio the blog garden: https: //www.cnblogs.com/iwehdio/

1., Object class

Root class is the class hierarchy, each class using the Object as a superclass

  • Any class by default inherit the Object class.
  • getClass()Returns a bytecode object.
  • String toString()method:
    • Returns a string representation of the object.
    • The output of an object is the default output of this object toString () method.
    • Output formats: hexadecimal memory address of the package name @ name of the class object.
    • General to rewrite more meaningful toString () method.
    • Eclipse quickly generate toString () method: Right> Source> Generate toString ().
  • boolean equals(Object obj)method:
    • Whether to use == to compare two objects are equal. Basic types of comparative value, the type of comparative reference address.
    • General to rewrite more meaningful toString () method. Such as string class overrides the equals method.
    • By comparing the incoming parameter whether the object is an object in the same class if(this.getClass()==o.getClass()).
    • Eclipse quickly generate equals () method: Right> Source> Generate equals ().

2, System class

Some useful class field (modified static member variables) and methods, can not be instantiated

  • static void arraycopy(Object src,int srcPos,Object dest,int desPos,int length): Copy the array. Incoming parameter (starting position of the source array index, starting index position of the source array, an array of targets, the target array, the array length of replication).
  • static long currentTimeMillis(): To return to the current system time is milliseconds. Based 1970-1-1.
  • static void exit(int status): Terminate the running Java virtual machine. Usually 0 indicates normal termination, 0 represents a non-abnormal termination.
  • static void gc(): Runs the garbage collector.

3, Date class

It represents a specific moment, with millisecond precision. The system time may be set by the method indicated.

  • Construction method:
    • Data(): Create a Date object represents the current system time.
    • Date(long dat): Create Date object according to the specified time (milliseconds).
  • A constructor object instance, and then directly output.
  • long getTime(): Get milliseconds time.
  • void setTime(long time): The time value in milliseconds.

4, DateFormat class

Date format, is an abstract class. Concrete classes have subclasses SimpleDateFormat.

  • Construction method:
    • SimpleDateFormat(): The default mode.
    • SimpleDateFormat(String pattern): Specified pattern.
  • String format(Date data):format

  • Date parse(String source): Resolution. Parsing format to be consistent with the format structure.

  • Example:

    SimpleDateFormat sdf = new SimpleDateFormat();
    /*一种指定格式
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    */
    Date d = new Date();
    String s = sdf.format(date);
    System.out.println(s);

5, Calender class

It is an abstract class, or conversion operations calendar object (date time).

  • static Calender getInstance(): Get a calendar.

  • int get(int field): The return value of a given calendar field.

    Calendar c = Calender.getInstance();
    int year = c.get(Calender.TEAR);
    int month = c.get(Calender.MONTH) + 1;    //月份从0开始
  • void set(int field,int value): Modify the specified field to the value specified.

  • void add(int field,int amount): Add the value specified in the specified field.

6. Wrapper class

Encapsulating the basic data type classes provide more sophisticated methods and variables

  • Corresponding to the basic data types of packaging:

    Basic data types Wrapper class
    byte Byte
    short Short
    char Character
    int Integer
    long Long
    float Float
    double Double
    boolean Boolean
  • Integer categories:

    • Construction method:
      • Integer(int value)
      • Integer(String s)
    • int intValue(): Returns an int type Integer object.
    • static int parseInt(String s): String turn int, without the need to create objects (static).
    • String toString(): Returns a String Integer object.
    • static String toString(int i): Int turn String, without the need to create objects (static).
  • Packing: basic data types into packaging; unpacking: packaging into basic type armor.

  • Autoboxing:Integer i = value .

    • Example integer i = 10;equivalent Integer i = new Integer(10);.
  • Auto-unboxing:int a = i;

    • Cases integer i = 10;``int a = i;. Equivalent int a = i.intValue();.
  • Automatic boxing and unboxing, for example:

    • Solve the original reference data type can not be added up:

      Integer i1 = 10;
      Integer i2 = 20;
      Integer i3 = i1 + i2;
    • Solving elements collection must ArrayList object:

      ArrayList list = new ArrayList;
      //自动装箱,相当于list.add(new Integer(1));
      list.add(1);    

7, regular expressions

A set of rules for matching string

  • boolean matches(String regex): Oh determining current string matches the specified regular expression (String category).

  • Common regular expression:

Regular Expressions meaning
x Character x
\ Backslash character
[abc] a or b or c
[^abc] In addition to any character a or b or c
[A-zA-Z] or a to z A to Z (including both ends)
x? Once or useless
x* Zero or more times
x+ One or more times
x{n} Exactly n times
x{n,} At least n times
x{n,m} At least not more than n times m times
  • Example: 5 to 15 numbers match, the first digit is not 0

    String s = "123456789";
    boolean flag = s.mathcs("[1-9][0-9]{4,14}");



iwehdio the blog garden: https: //www.cnblogs.com/iwehdio/

Guess you like

Origin www.cnblogs.com/iwehdio/p/12242334.html