Chapter 10: Object Oriented Thinking
1. Object-oriented
1. The process-oriented paradigm lies in design methods, and the object-oriented paradigm lies in coupling data and methods together.
2. The abstraction of a class means that the realization of the class is separated from the use of the class, and the details of the realization are encapsulated and hidden from the user, which is called the encapsulation of the class.
2. The relationship between classes
The relationship between classes is association, aggregation, composition and inheritance
1. Association: A common binary relationship that describes the activities between two classes
2. Aggregation is a special form of association, which represents the attribution relationship between two objects, such as students and addresses. One object can be owned by multiple other aggregate objects
3. If an object belongs to an aggregated object, then the relationship between it and the aggregated object is called a combination, for example: student and name
3. Treat basic types as objects
1. Basic type: int double char……
Packaging type: Integer Double Character……
Each wrapper class contains methods such as intValue(), doubleValue(), etc. to "convert" objects into basic type values
You can use basic type values and strings that represent numeric values to construct wrapper classes
The compareTo() method returns 1, 0, -1 corresponding to greater than or equal to less than
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer i = new Integer(3);
Double d = new Double(3.5);
Double d2 = new Double("3.5");
Double d3 = new Double("1.5");
Double d4 = new Double("8.5");
System.out.println(i.intValue());//3
System.out.println(d.doubleValue());//3.5
System.out.println(d2.doubleValue());//3.5
System.out.println(Double.valueOf("6.5"));//6.5
System.out.println(d.compareTo(d2));//0
System.out.println(d.compareTo(d3));//1
System.out.println(d.compareTo(d4));//-1
}
Convert string to binary, octal, decimal, hexadecimal
//语法:
Integer i=Integer.parseInt(字符串,进制)//进制可以为2,8,10,16
int i2=Integer.parseInt("011", 2);
System.out.println(i2);//3
//转换为16进制的第二种方法
System.out.println(String.format("%x", 26));//1a
Note that the
packaging class does not have a parameterless structure
2. Automatic conversion between basic type and packaging type
Basic -> Packaging (boxing)
Packaging -> Basic ( unboxing )
Integer x=new Integer(2);//等价于 Integer x=2 (自动装箱)
//但是:
Double d=3;//错误
Double d=3.0//正确
Four. BigInteger and BigDecimal objects
1.BigInteger and BigDecimal classes can represent integers or decimal numbers of any size and any precision
2. Created by new BigInteger (string)
public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger bi = new BigInteger("1234567845678984327");
System.out.println(bi);//1234567845678984327
BigDecimal bd = new BigDecimal("3.14159265358979323846264338327950288");
System.out.println(bd);//3.14159265358979323846264338327950288
}
3. Use add substract multiply divids remainder to perform addition, subtraction, multiplication, and division remainder operations
4. Note:
BigDecimal will throw an exception if the result cannot be terminated. You can use overloaded divide (BigDecimal d, digits after the decimal point, divisor)
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(fact(50));//返回50的阶乘结果
//608281864034267560872252163321295376887552831379210240000000000
}
public static BigInteger fact(long x) {
BigInteger sum=BigInteger.ONE;//BigInteger.ONE=1;
for(int i=1;i<x;i++) {
sum=sum.multiply(new BigInteger(i+""));//使用multiply做乘法
}
return sum;
}
Five.String class
The String object is immutable. Once the string is created, the content cannot be changed. The
String class has 13 construction methods and multiple methods for processing strings.
char[] c= {
'j','a','v','a'};
String s=new String(c);
System.out.println(s);//java
String s1="welcome";//创建了一个对象,将Welcome放入了常量池中
String s2=new String("welcome");//新的对象 如果常量池中不存在welcome,则词语局创建了两个对象,分别是将Welcome放入常量池中,将new的对象放入堆中
String s3="welcome";//和s1指向同一个地址
System.out.println(s1==s2);//false
System.out.println(s1==s3);//true
Common methods in strings:
String s="hello java";
System.out.println(s.replace('a', 'e'));//hello jeve
System.out.println(s.replaceFirst("l", "y"));//heylo java
System.out.println(s.replaceAll("l", "y"));//heyyo java
String[] list=s.split("o");
for(String i:list)
System.out.print(i+" ");//hell java
Six. Regular expressions
Regular expression: use a pattern to match, replace and split a string
Convert a string to a character array:
String s="hello java";
char[] a=s.toCharArray();
for(char i:a)
System.out.print(i+" ");//h e l l o j a v a
Convert the array to a string
, as mentioned above
Usually use matches, replace, replaceAll and other methods, it is recommended to program for Baidu, more comprehensive
Seven. StringBuilder and StringBuffer class
1. You can add, insert, and append new content to the StringBuilder and StringBuffer classes
2. StringBuffer is suitable for concurrent execution of multitasking, because it modifies the buffer synchronously, while StringBuilder performs single task more efficiently, and the remaining two are exactly the same
3. StringBuilder has 3 construction methods and more than 30 methods for processing strings
StringBuilder s=new StringBuilder();//创建一个容量为16的空字符串构造器
StringBuilder s2=new StringBuilder(20);//指定容量
StringBuilder s3=new StringBuilder("hello java");//指定字符串
System.out.println(s.capacity());//16
System.out.println(s2.capacity());//20
prompt:
If a string does not need to be changed, use String, because String optimizes shared strings
In the computer, the string constructor is a character array, and the capacity of the constructor is the size of the array. If the capacity is exceeded, an array with a capacity of 2* (existing capacity + 1) will be created
If the capacity is too large, you can use trimToSize() to reduce the capacity to the actual size
In the following example, you can see that the string and its inversion are actually equal! ! !
StringBuilder s=new StringBuilder("java is fun");
System.out.println(s.reverse());//nuf si avaj
System.out.println(s.equals(s.reverse()));//true
The reason is that StringBuilder does not override the equals method, but always points to the original object.
The toString method needs to be added to achieve comparison.
System.out.println(s.toString().equals(s.reverse().toString()));//false
Other methods of StringBuilder will not be introduced too much, and will be used frequently in the future
[Later supplement]-2020-03-06
1. The content of the String class will not change.
StringBuilder can dynamically construct a string, and a thread-unsafe
StringBuffer can dynamically construct a string, which is thread-safe
2. Comparison of the three in terms of speed and memory consumption
Speed: StringBuilder>StringBuffer>String
memory: String>StringBuffer>StringBuilder
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuilder s = new StringBuilder();
long remain1 = Runtime.getRuntime().freeMemory();// 剩余内存
long begin = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
s.append(i);
}
long end = System.currentTimeMillis();
long remain2 = Runtime.getRuntime().freeMemory();// 字符串拼接完成后剩余内存
System.out.println("使用毫秒数:" + (end - begin) + "\t使用内存:" + (remain1 - remain2));
//结果:StringBuilder:使用毫秒数:10 使用内存:1032240
//StringBuffer:使用毫秒数:12 使用内存:1032240
//String:使用毫秒数:4196 使用内存:-114659128
}
3. Use scenarios:
- A small amount of data does not change the string-String
- Operate large amounts of data in a single-threaded string buffer-StringBuilder
- Operate large amounts of data under multi-threaded operation string buffer-StringBuffer
8. Summary
Through the study of this chapter, I have learned about the characteristics of object-oriented and the four relationships of classes, learned the mutual transformation of basic types and packaging types, learned automatic boxing and unboxing, learned the basic usage of BigInteger and BigDecimal classes, and learned After arriving at the String class’s processing and storage mechanism for strings, I learned the differences and similarities between StringBuilder and StringBuffer. It can be said to be a full-service for strings, and each has its own advantages and disadvantages. The regular expression is only mentioned in this chapter. This is A very useful function, but I have limited abilities, and I worry that the explanation is not comprehensive enough. You can program this aspect of Baidu for a large amount.
Come on! Chapter 11 To Be More...