第五章常用类

本文介绍了Java中的API,特别是基本数据类型包装类的作用和方法,如Integer的构造、比较和转换。同时,讲解了Object类的toString和equals方法,Arrays类的equals和sort方法。此外,还讨论了String的特性、构造和操作方法,以及StringBuffer类在字符串操作中的效率优势。最后,提到了Math类的数学运算和Random类的随机数生成。
摘要由CSDN通过智能技术生成

API(Application Programming Interface)应用程序编程接口

是对java预先定义的类或接口功能和函数功能的说明文档,目的是提供给开发人员进行使用帮助说明.

基本数据类型封装类

包装类

两种用途

作为和基本数据类型对应的类类型存在。

包含每种基本数据类型的相关属性如最大值、最小值等,以及相关的操作方法

以java.lang.Integer为例

public static final int MAX_VALUE 最大的int型数(231-1)

public static final int MIN_VALUE 最小的int型数(-231)

构造方法

Integer(int a);

Integer(String a);

比较方法

static int compareTo(Integer a);

boolean equals(Object);

int max(int a,int b);

int min(int a,int b);

转换方法

static toBinaryString(int i);

static String toHexString(int i);

static String toOctalString(int i);

int intValue();

static int parseInt(String s);

String toString();

static Integer valueOf(int i)

static Integer valueOf(String s)

装箱和拆箱Auto-boxing/unboxing

装箱

自动将基本数据类型转换为包装器类型

装箱的时候自动调用的是Integer的valueOf(int)方法

拆箱

自动将包装器类型转换为基本数据类型

拆箱的时候自动调用的是Integer的intValue方法

Object类

Object类是所有Java类的祖先(根基类)。每个类都使用 Object 作为超类(父类)。所有对象(包括数组)都实现这个类的方法

toString方法

Object类中定义有public String toString()方法,其返回值是 String类型,描述当前对象的有关信息。

在进行String与其它类型数据的连接操作时(如System.out.println(“info”+person)),将自动调用该对象类的toString()方法

equals方法

public boolean equals(Object obj)方法提供定义对象是否“相等”的逻辑。Object 的 equals 方法 定义为:x.equals ( y ) ,当 x 和 y是同一个对象的引用时返回 true 否则返回 false。JDK提供的一些类,如String,Date等,重写了Object的equals方法,调用这些类的equals方法,x.equals (y) ,当x和y所引用的对象是同一类对象且属性内容相等时(并不一定是相同对象),返回true 否则返回 false。

Arrays类

equals方法

比较两个非同一数组是否相等,而数组本身的equals判断另一个数组是否它本身。

声明:public static boolean equals(type[]a,type[]a2)

参数的类型可以是原生数据类型和引用类型的任意一种类型。

返回:如果两个相等,则返回true,否则返回false

sort -排序

作用于数组的所有元素

public static void sort(type[] a)作用于数组指定范围内的元素

public static void sort(type[] a, int fromIndex(包括), int toIndex(不包括))

将指定的类型(除boolean以外的任意原生数据类型)数组所有元素(或指定范

围内的元素)按数字升序进行排序。

object型数组,根据元素的自然顺序,对指定对象数组进行升序排序。

fromIndex==toIndex,则排序范围为空)

binarySearch -使用二分搜索算法搜索指定数组

声明:

public static int binarySearch(type[] a, type key)

public static int binarySearch(long[] a,int fromIndex,int toIndex,long key)

描述:

使用二分搜索算法搜索指定的type型数组,以获得指定的值。

参数:

a - 要搜索的数组。

key - 要搜索的值。

fromIndex - 要排序的第一个元素的索引(包括)。

toIndex - 要排序的最后一个元素的索引(不包括)。

type -byte、double、float、object、long、int、short、char

如果key在数组中,则返回搜索值的索引;否则返回-1或者”-“(插入点)

toString() 方法

声明:public static String toString(type[] a)

描述:返回指定数组内容的字符串表示形式。

基本数组,字符串表示形式由数组的元素列表组成,括在[],相邻元素用“,”(逗号加空格)分隔。

String类

字符串是由多个字符组成的一串数据(字符序列)的字符串常量,java中所有字符串都是String类的实例

第一种

String s = "abc";先在栈中创建一个对String类的对象引用变量s,然后去字符串常量池中查找有没有"abc", 如果没有则在常量池中添加”abc”, s引用变量指向常量池中的”abc”,如果常量池中有,则直接指向改地址即可,不用重新创建。

第二种

一概在堆中创建新对象,值存储在堆内存的对象中。String s = new String("abc");

构造方法

public String()

public String(String str)

public String(byte[] bytes)

public String(char[] value)

判断功能

boolean equals(Object obj)

boolean equalsIgnoreCase(String str)

boolean contains(String str)

boolean isEmpty()

boolean startsWith(String prefix)

boolean endsWith(String suffix)

获取功能

int length()

char charAt(int index)

int indexOf(String str)

int indexOf(String str,int fromIndex)

String substring(int start)

String substring(int start,int end)

转换功能

byte[] getBytes()

char[] toCharArray()

static String valueOf(char[] chs)

String toLowerCase()

String toUpperCase()

String concat(String str)

Stirng[] split(分割符);

替换功能

String replace(char old,char new)

String replace(String old,String new)

replaceAll(String regex, String replacement)

replaceFirst(String regex, String replacement)

去除字符串两空格

String trim()

StringBuffer类

我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题线程安全的可变字符序列。

StringBuffer和String的区别

构造方法

public StringBuffer()

public StringBuffer(String str)StringBuffer类

添加功能

public StringBuffer append(String str)

public StringBuffer insert(int offset,String str)

删除功能

public StringBuffer deleteCharAt(int index)

public StringBuffer delete(int start,int end)

替换功能

public StringBuffer replace(int start,int end,String str)

反转功能

public StringBuffer reverse()

截取功能

public String substring(int start)

public String substring(int start,int end)

StringBuilder类功能和StringBuffer功能完全一致, StringBuffer是线程安全的

Math类

abs 绝对值

sqrt 平方根

pow(double a, double b) a的b次幂

max(double a, double b)

min(double a, double b)

random() 返回 0.0 到 1.0 的随机数

long round(double a) double型的数据a转换为long型(四舍五入)

Random类

此类用于产生随机数

构造方法

public Random()

成员方法

public int nextInt()

public int nextInt(int n)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值