当前位置:网站首页>Common auxiliary classes
Common auxiliary classes
2022-04-23 03:34:00 【Chen Yu】
Java Summary of knowledge points : You can enter from here if you want to see
7.1 Number class
abstract class Number yes BigDecimal、BigInteger、Byte、Double、Float、Integer、Long、Short The parent of a class . They are the corresponding byte、double And other basic data types are encapsulated into classes ,Java Call it the wrapper class
7.1.1 Integer class
Integer and int The difference between :
int yes 8 One of the basic data in ,Integer yes java Is the basic data type int Provided encapsulation class . They have different characteristics and usages . 1、 size :int In memory stack , Occupy 4 byte , Fast storage .Integer On the memory heap , Large space and slow speed . 2、 The default value is :int by 0,Integer by null, You can distinguish between an unassigned value and a value of 0 The difference between , and int You can't . 3、Integer Is the class provided , Therefore, some methods and properties are provided internally . 4、int Mainly used in calculation , and Integer Used in type conversion 、 Access data to a collection . Methods provided
If you create an integer using an encapsulated class . Values in -128~127 Between time , No new objects will be created , Instead, it directly refers to the created objects in the constant pool .
Integer i1 = 100, i2 = 100, i3=200, i4=200; i1 and i2 Share one object ,i3 and i4 Use different objects
7.1.2 Boolean value
7.1.3 floating-point
7.1.4 Character
7.1.5 Automatic unpacking and packing
Automatic unpacking and packing
-
Packing : Change the basic data type into a wrapper class object .
-
Unpacking : Take out the basic data type of the package in the wrapper class .
-
When autoboxing, the compiler calls valueOf Convert the original type value to an object , At the same time, when the case is opened automatically , The compiler calls something like intValue()、
doubleValue() This kind of method converts the object to the original type value .
7.2 Math class
Contains properties and methods for performing basic mathematical operations , Such as the primary index 、 logarithm 、 Square root and trigonometric function .Math All methods are defined as static form , adopt Math Class can be called directly in the main function .
math class
Math.round(11.5) and Math.round(-11.5) Value
answer : round To round off ,Math.round(11.5)=12 and Math.round(-11.5)=-11
7.3 random class
Common methods :
7.4 BigInteger
7.5、 BigDecimal
7.6 Object
7.7、String Related classes
-
String: String class , By final The value of the modifier cannot be changed . Realized Serializable and Comparable Interface , Serialization and size comparison are supported ; Save a reference to an address after its definition , Every time the value is changed, a new memory address assignment is reallocated , Instead of modifying in the original address .
-
StringBuilder: String class , Its value can be modified , Thread unsafe , Fast .
-
StringBuffder: String class , Its value can be modified , Thread safety , Speed is relative to StringBuilder slow .
StringBuilder and StringBuffder At the bottom of the is a character array , The expandable length after creation is 16 Characters , When the modification scope exceeds 16 character , The capacity of the underlying array will be expanded . Create a new array with the size of the original 2 Doubling 2, Point the pointer to the new common array address .
StringBuilder and StringBuffder Common methods : 1、.append("").append("").....;: String concatenation 2、.delete(int start,int end): Delete the contents of the specified range ,[start,end) 3、.replace(int start, int end, String str): Replace the contents of the specified range [start,end) 4、.insert(int offset, xxx): Insert the specified content in the specified position 5、.reverse() : Reverse the current character sequence 6、public int indexOf(String str) : Returns the index of the first occurrence of a specified substring in the current string 7、public String substring(int start,int end) : Returns a substring of the specified range [start,end) 8、public int length() : Returns the length of the string 9、public char charAt(int n ) : Gets the character at the specified index 10、public void setCharAt(int n ,char ch) : Set the character at the specified index
Next, we can intuitively compare the use of String and StringBuilder The difference in speed
Calculate the additional 100,000,0 The time taken for the second character
-
Use String
public static void main(String[] args) { long start = System.currentTimeMillis(); String str = ""; for(int i=0; i<1000000; i++){ str += 1; } long end = System.currentTimeMillis(); System.out.printf(" Use String when :%d",end-start); }
-
Switch to StringBuilder
public static void main(String[] args) { long start = System.currentTimeMillis(); StringBuilder builder = new StringBuilder(); for(int i=0; i<1000000; i++){ builder.append(1); } long end = System.currentTimeMillis(); System.out.printf(" Use StringBuilder when :%d",end-start); }
-
Use StringBuffder
public static void main(String[] args) { long start = System.currentTimeMillis(); StringBuffer buffer = new StringBuffer(); for(int i=0; i<1000000; i++){ buffer.append(1); } long end = System.currentTimeMillis(); System.out.printf(" Use StringBuffer when :%d",end-start); }
It is obvious from the above that String When compared with the latter two, the speed is not a little slower , But many times the gap , and StringBuilder Comparison StringBuffder Faster, too .
7.8、 Time processing
版权声明
本文为[Chen Yu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230332246219.html
边栏推荐
- Redis (17) -- redis cache related problem solving
- List interface of collection
- JS calculates the display date according to the time
- 51 single chip microcomputer: D / a digital to analog conversion experiment
- PYMOL-note
- Test questions and some space wars
- Application and definition of interface
- String input problem
- Design and implementation of redis (6): how redis achieves high availability
- Using jsonserialize to realize data type conversion gracefully
猜你喜欢
Learn about I / O flow and file operations
Unity Basics
Redis(17) -- Redis缓存相关问题解决
C abstract class
Unity knowledge points (common core classes)
Unity knowledge points (ugui 2)
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
L3-011 直捣黄龙 (30 分)
Super easy to use [general excel import function]
Test questions and some space wars
随机推荐
Oracle JDK vs OpenJDK
JS - accuracy issues
On the principle of concurrent programming and the art of notify / Park
第四次作业
淺學一下I/O流和File類文件操作
Chapter 8 exception handling, string handling and file operation
常用的辅助类
mui. Plusready does not take effect
The content of the website is prohibited from copying, pasting and saving as JS code
Wechat payment iframe sub page has no response
The art of concurrent programming (6): explain the principle of reentrantlock in detail
Websites frequented by old programmers (continuously updated)
Optimization of especially slow startup in idea debugging mode
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
The art of concurrent programming (5): the use of reentrantlock
Identifier and type conversion
New ORM framework -- Introduction to beetlsql
Flink customizes the application of sink side sinkfunction
51 single chip microcomputer: D / a digital to analog conversion experiment
抽象类、接口、常用关键字