Java classes in common: packaging, String, StringBuffer, StringBuilder, Math, System, Arrays, BigInteger, BigDecimal, Data, Calendar

First, the packaging √

Two, String class ★

Three, StringBuffer and StringBuilder class ★

Four, Math Class

Five, System class

Six, Arrays class

Seven, BigInteger and BigDecimal class category

Eight, Date Date class, Calendar Calendar class and the new date

First, the packaging

1. Concept : of 8 large basic data types provide the corresponding reference data types , the data types of these references referred packaging

2. Data type :

1) The basic data types

2) reference data types

  • Array
  • With class definitions
  • Use interface definitions

3. requirements such as :

① get the maximum value of type int

② to convert an integer to a hexadecimal form

int i=10;

Integer i2=new Integer(i);

4. Category

byte———————————— Byte
short———————————— Short
int————————————— Integer
long———————————— Long
float———————————— Float
double——————————— Double
char———————————— Character
boolean——————————  Boolean

5. The transition between the basic types and package type

jdk5.0 before:

Manual Packing :

Way a: Integer i = new Integer (10);

Second way: Integer i2 = Integer.valueOf (10);

 

Manual unboxing :

int j = i.intValue();

jdk5.0 after:

Autoboxing:

Integer i = 10;

Auto-unboxing:

int j = i; 

6. The transition between the package and the type of type String

Package Type ----> String type

Integer i = 10;
//方式1:
String s1= i.toString();

//方式2:
String s2 = String.valueOf(i);

//方式3:
String s3 = i+"";

System.out.println(s3);

String ----> packaging

@ Mode. 1: 
Integer J = new new Integer (S1); 

// mode 2: 
Integer J2 = Integer.valueOf (S2);

7. The transition between the base and the type of type String

Basic types ----> String type

int i = 10;
//方式1:
String s=i+"";
//方式2:
String s2 = String.valueOf(i);

String ----> basic types (except char type)

int j = Integer.parseInt(s);

char c = s.charAt(0);

8. packaging types common API

1) General method:

valueOf: String to convert to the corresponding primitive types and type of packaging
parseXX: String to convert to the corresponding type of basic types
xxValue: converting the package type corresponding to the basic type

A common method 2) Character class:
toUpperCase
toLowerCase
isUpperCase
isLowerCase
isDigit
isLetter
isWhiteSpace

Two, String class

1. Understanding

String String Class for storing a set of sequences

"John", "" are the equivalent of a string constant object

2. Create Object

Mode 1: Direct assignment

String s = "hello";

2 ways: by calling the constructor

String s = new String("hello");

[Difference] face questions String objects created in two ways

One way: go to the pool to see if there is constant "hello" character sequence, if not, create, if there is a direct reference to the address, s point is the object of constant pool

Second way: the need to create a heap object that maintains a value attribute, value points to the constant pool "hello", if not constant pool "hello", is created, and then point to; if you already have, directly with point value
s is pointed object in the heap

A common method 3.String class

Method name use
 length   Gets the number of characters
 charAt  Get the character at the specified index
 toUpperCase  Turn uppercase
 toLowerCase  Small letter
 indexOf  Gets the index of the first occurrence of a character or string, or -1 if not found
 lastIndexOf  Gets the index of the last occurrence of a character or string, or -1 if not found
 startsWith  Determining whether to begin with xx
 endsWith  In order to determine whether the end of XX
 regionMatches  Determining whether a sub-string within the same range
 contains  Substring determine whether there is, returns true | false
 compareTo  Compare two string size
 equals/equalsIgnoreCase  Determining whether the contents of the string are equal
 substring  Taken substring
 replace/replaceAll  replace
 trim  To a space before and after
 split  Split
 concat  String concatenation
 toCharArray  Converted into an array of characters

Three, StringBuffer class

1.String class of contrast and StringBuffer

The same point: for saving all strings, or deletions can be of some other processing operation on the character string

difference:

  • String Class for storing string constants
  • StringBuffer class for storing string variables

2.StringBuffer class to create objects

You can only create an object by calling the constructor!

new new the StringBuffer ();       // configured with an initial capacity of 16 char array 
new new the StringBuffer (String);    // configured with an initial capacity of the array string.length + 16, and initializes the value String 
new new the StringBuffer (Capacity); // Constructs an initial capacity of the char array capacity

3.StringBuffer conversion between classes and the String class

1)StringBuffer——>String

StringBuffer buffer = new StringBuffer("john");

//方式一:
String s1 = buffer.toString();

//方式二:
String s2 = new String(buffer);

2)String————>StringBuffer

String s = "Jiu Mozhi" ;
 // mode a: 
the StringBuffer B1 = new new the StringBuffer (S); 

// Second way: 
the StringBuffer B2 = new new the StringBuffer (); 
b2.append (S);

Class method 4.StringBuffer

By append

删 delete

Change replace

查 indexOf/lastIndexOf

Insert insert

Reverse reverse

Length length 

Comparison of 5.StringBuffer and StringBuilder

  Common version Thread Safety (Synchronous) efficiency
StringBuffer Save the string variable old Safety Lower
StringBuilder Save the string variable new Unsafe Higher
  • Thread safety

The String object is immutable, it can be understood as a constant thread safe. AbstractStringBuilder StringBuilder and StringBuffer is common parent class defines the basic operation of some of the string, such as expandCapacity, append, insert, indexOf other public methods. StringBuffer method adds genlock or method call added a synchronization lock, so are thread-safe. StringBuilder and no method plus genlock, so not thread-safe. 

  • performance

Every time a change of type String, generates a new String object, then the pointer to the new String object. StringBuffer StringBuffer object itself every time to operate, instead of generating new object and change the object reference. Use StringBuilder StringBuffer compared to using only get about 10% to 15% performance increase under the same circumstances, but they bear the risk of multithreading unsafe.

  • For the use of the three summary:
  1. Operating a small amount of data: For String
  2. Operation single-threaded operating string buffer large amounts of data: for StringBuilder
  3. Operating a multi-threaded operating string buffer large amounts of data: applicable StringBuffer

(效率:StringBuilder>StringBuffer>String)

Four, Math Class

It contains a series of static methods for mathematical operations.

1. Common methods

Method name effect
sqrt Prescribing requirements
pow Exponentiation
ceil Improvement ToSei
floor Rounding down
round Rounding and returns an int
abs Absolute value
random random number
max Maximum
me Minimum

Five, Arrays class

An array comprising a series of methods for management and operation of the

Common methods:

sort (T []): the elements of the array natural ordering requirements must implement the Comparable elements

sort (T [], Comparator): the elements of the array be customized sorting element itself may not implement Comparable

binarySearch (T [], key): to find the array by the binary search algorithm, if the key is found, return the index, otherwise negative. Requirements: To find the array must be sorted in advance!

copyOf (T [], length): Copies the elements of the array

equals (T [], T []): the contents of the two arrays are equal is determined

fill (T [], key): the value of each element of the array filled key

toString (): array elements spliced ​​return String

Six, System class

Common methods:

arrayCopy 复制数组元素,一般使用Arrays.copyOf代替
exit:退出程序
currentTimeMillens:获取当前时间距离1970-1-1的毫秒数。
gc:运行垃圾回收器

七、BigDecimal和BigInteger类

常见方法:

add 加法
substract减法
multiply乘法
divide除法,注意:可以添加参数2设置四舍五入模式

八、日期类

1.第一代日期

java.util.Date类
java.text.SimpleDateFormat类

2.第二代日期
java.util.Calendar类

3.第三代日期(jdk8新特性)
LocalDate|LocalTime|LocalDateTime类:类似于Calendar
Instant:类似于Date
DateTimeFormatter:类似于SimpleDateFormat

Guess you like

Origin www.cnblogs.com/MWCloud/p/11223514.html