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
-
Package: package
-
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
-
encapsulation
- Use objects and methods to hide implementation details
- Property private, get/set
-
inherit
- Single inheritance
- object
- super
- Method rewrite
-
Polymorphism, inheritance based
- The key to realizing polymorphism is interface.
- The class that implements the class override interface@ Override
- 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.
-
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>();
-
Override and reload
-
Override: the subclass overrides the parent class method (you can override @ Override) and implements the class override interface method (you need @ Override).
-
Overload: different parameter implementers of the same method call the same method, and different incoming parameters enter different implementations.
-
-
character string
-
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
-
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
-
other
-
-
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.
-
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
-
List interface common
- Implementation classes: ArrayList array sequence, LinkedList linked list
-
Queue interface queue
- Implementation class: LinkedList linked list
-
Set interface common
- HashSet hash set
-
Implement compareTo of the Comparable interface, indicating that the class is Comparable. To sort
-
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