Java note 1 - data types

1, Java data types

Eight basic data types

Integer: byte, short, int, long

Character type: char

Boolean: Boolean

Packing class of basic type:

Packaging mainly provides two types of methods:
1. Method for converting this type to other basic types
2. Method for converting string to this type and wrapper class
	byte=>Byte
    short=>Short
    int=>Integer
    long=>Long
    float=>Float
    double=>Double
    char=>Character
    boolean=>Boolean
Integer.toString(int)//Numeric to string
Integer.parseInt(str)//String to value

Three referenced data types

Class, interface, array

1. Array

Definition of array:

int[] arr = new int[5];
int[] arr = new int[]{1,2,3,4,5};
int[] arr = {1,2,3,4,5};
for(int a:arr){
    sout(a);
}		

Arrays class operates on arrays:

Arrays..sort(arr);
Arrays.toString(arr);

2. Class ctrl+h

  1. Package: package

  2. Access modifier:

Public: public, accessible anywhere

Protected: protected. Classes and inherited subclasses of the same package can be accessed

Default: by default, classes of the same package can be accessed

Private: private, which can only be accessed inside the class

  1. encapsulation

    1. Use objects and methods to hide implementation details
    2. Property private, get/set
  2. inherit

    1. Single inheritance
    2. object
    3. super
    4. Method rewrite
  3. Polymorphism, inheritance based

    1. The key to realizing polymorphism is interface.
    2. The class that implements the class override interface@ Override
    3. By dynamically generating different implementation classes of the interface through parameters and executing the methods of the interface, different phenomena are obtained. You don't need to know which implementation classes are specific.
  4. ArrayList replaces array, which is a necessary class for each Java project

    //The same type is not mandatory
    List list = new ArrayList();//The front list is the interface type, and the rear ArrayList is the implementation class. java.util.List/ArrayList
    list.Add("xxx");//Append at the end of the list
    list.add(0,"xxx");//Append from the list header and move other members back
    list.get(index);Take it out.
    list.remove(index);Delete.
    list.size();//number
    =>Force the method of the specified type:generic paradigm  
    List<String> list = new ArrayList<String>();
    
  5. Override and reload

    1. Override: the subclass overrides the parent class method (you can override @ Override) and implements the class override interface method (you need @ Override).

    2. Overload: different parameter implementers of the same method call the same method, and different incoming parameters enter different implementations.

  6. character string

    1. common method

      length indexOf2 lastIndexOf2 subString2 trim equals toLowerCase toUpperCase charAt split getBytes
          
      ==: Judge whether the first address of two strings in memory is the same, that is, judge whether they are the same string object
      equals(): Compare whether the contents stored in the two string objects are consistent
      
    2. StringBuilder class

      StringBuffer is thread safe, while StringBuilder does not implement thread safe function, so its performance is slightly higher.

      Therefore, in general, if you need to create a string object with variable content, you should give priority to using the StringBuilder class.

      append insert toString length
      
    3. other

  7. Use the Date and SimpleDateFormat classes to represent time

    There are various problems with the Date class. It is more recommended to use the Calendar class to process time and Date.

  8. Using the Math class to manipulate data

3. Collective framework

Generic: Specifies that a collection can hold only specific types of objects.

Collection interface

Collections tool class: java.util.Collections. A tool class that operates on the collection framework. Such as sort method

Map interface

Comparable interface

Comparator interface

Collection interface

  1. List interface common

    1. Implementation classes: ArrayList array sequence, LinkedList linked list
  2. Queue interface queue

    1. Implementation class: LinkedList linked list
  3. Set interface common

    1. HashSet hash set
  4. Implement compareTo of the Comparable interface, indicating that the class is Comparable. To sort

  5. Comparator interface. Compare method

Map interface < key, value >

Implementation class: HashMap

Key value pair Entry

4. Throwable

### 1.Error errors are unpredictable

Virtual machine error

GUI error

2.Exception exceptions are predictable

IO exception: EOF FileNotFind

Runtime exception

The active exception thrown is throw, and the method is shrows

Keywords: Java

Added by sheckel on Wed, 24 Nov 2021 06:23:49 +0200