当前位置:网站首页>常用的辅助类
常用的辅助类
2022-04-23 03:32:00 【辰 羽】
7.1 Number类
抽象类 Number 是BigDecimal、BigInteger、Byte、Double、Float、Integer、Long、Short类的父类。它们是将所对应的byte、double等基本数据类型封装成类,Java中将其称为包装类
7.1.1 整数类
Integer和int的区别:
int是8中基本数据之一,Integer是java为基本数据类型int提供的封装类。他们具有不同的特征和用法。 1、大小:int在内存栈,占用4字节,存储速度快。Integer在内存堆上,空间大速度慢。 2、默认值:int为0,Integer为null,可以区分未赋值和值为0的区别,而int不能。 3、Integer是提供的类,所以内部提供了一些方法和属性。 4、int主要用在计算中,而Integer用在类型转换、向集合中存取数据。提供的方法
如果使用封装类创建整数时。数值在-128~127之间时,不会创建新对象,而是直接引用常量池中的已经创建好的对象。
Integer i1 = 100, i2 = 100, i3=200, i4=200; i1和i2共用一个对象,i3和i4使用不同对象
7.1.2 布尔值
7.1.3 浮点型
7.1.4 字符型
7.1.5 自动拆箱和自动装箱
自动拆箱和自动装箱
-
装箱:将基本数据类型变为包装类对象。
-
拆箱:将包装类中包装的基本数据类型取出。
-
自动装箱时编译器调用valueOf将原始类型值转换成对象,同时自动拆箱时,编译器通过调用类似intValue()、
doubleValue()这类的方法将对象转换成原始类型值。
7.2 Math 类
包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。Math 的方法都被定义为 static 形式,通过 Math 类可以在主函数中直接调用。
math类提供的常用方法
Math.round(11.5)和 Math.round(-11.5)的值
答: round为四舍五入,Math.round(11.5)=12和 Math.round(-11.5)=-11
7.3 random类
常用方法:
7.4 BigInteger
7.5、 BigDecimal
7.6 Object
7.7、String相关类
-
String:字符串类,被final修饰其值不可改。实现了Serializable和Comparable接口,可支持序列化和可以比较大小;其定义后内保存一个地址的引用,每次改变的值都是重新分配一个新的内存地址赋值,而不是在原有的地址内修改。
-
StringBuilder:字符串类,其值可修改,线程不安全,速度快。
-
StringBuffder:字符串类,其值可修改,线程安全,速度相对StringBuilder慢。
StringBuilder和StringBuffder的底层是一个字符数组,创建后的可扩容长度为16个字符,当修改范围超过16字符,会对底层数组进行扩容。创建一个新的数组大小为原来的2倍加2,将指针指向新常见的数组地址。
StringBuilder和StringBuffder常用方法: 1、.append("").append("").....;:拼接字符串 2、.delete(int start,int end):删除指定范围的内容,[start,end) 3、.replace(int start, int end, String str):替换指定范围的内容[start,end) 4、.insert(int offset, xxx):在指定位置插入指定的内容 5、.reverse() :把当前字符序列逆转 6、public int indexOf(String str) : 返回指定子字符串在当前字符串中第一次出现处的索引 7、public String substring(int start,int end) :返回指定范围的子字符串[start,end) 8、public int length() : 返回字符串的长度 9、public char charAt(int n ) : 获取指定索引处的字符 10、public void setCharAt(int n ,char ch) : 设置指定索引处的字符
接下来我们可以直观的对比一下使用String和StringBuilder在速度上的区别
计算一下追加 100,000,0次字符所用的时间
-
使用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("使用String用时:%d",end-start); }
-
换成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("使用StringBuilder用时:%d",end-start); }
-
使用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("使用StringBuffer用时:%d",end-start); }
从上面可以明显的看出来使用String的时候相比较后两种速度慢的不是一点半点的,而是很多倍的差距了,而StringBuilder相比较StringBuffder也快了一些。
7.8、时间处理
版权声明
本文为[辰 羽]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yuandfeng/article/details/117371288
边栏推荐
- Common exceptions
- . NETCORE sets the API post mode, which can accept parameters directly in parentheses
- MySql分组查询规则
- 第四次作业
- Redis(17) -- Redis缓存相关问题解决
- Download and configuration of idea
- Deep learning notes (II) -- principle and implementation of activation function
- Unity games and related interview questions
- 场景题:A系统如何使用B系统的页面
- Téléchargement en vrac de fichiers - téléchargement après compression
猜你喜欢
Visual programming - Experiment 1
Quartz. Www. 18fu Used in net core
【微服务】(十)—— 统一网关Gateway
Unity basics 2
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
Unity knowledge points (common core classes)
Database SQL -- simulate inserting a large amount of data, importing / exporting database scripts, timestamp conversion and database basics
浅学一下I/O流和File类文件操作
. net webapi access authorization mechanism and process design (header token + redis)
AWS from entry to actual combat: creating accounts
随机推荐
Leetcode 617 merge binary tree
Test questions and some space wars
New ORM framework -- Introduction to beetlsql
L3-011 直捣黄龙 (30 分)
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
Batch download of files ---- compressed and then downloaded
PWA I'm here
Identifier and type conversion
Problem a: face recognition
Log4net is in Net core usage
Application and definition of interface
Redis (17) -- redis cache related problem solving
Supersocket is Use in net5 - concept
Chapter 8 exception handling, string handling and file operation
. net 5 Web custom middleware implementation returns the default picture
场景题:A系统如何使用B系统的页面
Do you really understand hashcode and equals???
String input problem
全新的ORM框架——BeetlSQL介绍
WinForm allows the form form to switch between the front and active states