第六章、java.lang包常用类

第六章、java.lang包常用类

Object的几个方法

Object只有一个无参构造方法

getClass()

Student stu = new Student();
stu.getClass();
//得到class com.zte.testApi.Student
stu.getClass().getSimpleClass();
//得到类名Student

获取对象的类结构

hashCode()

Student stu = new Student();
stu.hashCode();

获取对象在内存中的地址并将地址通过哈希码编码(哈希算法)成十进制值

equals()

Student stu = new Student();
stu.equals();

重写父类的equals方法用来根据自己的需求判断对象是否相等

注意:一般重写了equals方法也会重写hashCode方法,以保证相同的对象有相同的哈希码

toString()

Student stu = new Student();
stu.toString();

父类方法获取类完整名和@以及hashCode方法返回的值进行拼接

子类根据需求重写方法,以便返回对象的自我介绍

Clone

public class Student implements Cloneable{}
Student stu = new Student();
Student stu1 = stu.clone();

类得克隆方法(调用java的底层(C++)方法来进行对象的克隆)

要克隆的类必须实现Cloneable接口

要在类中重写clone方法,因为父类的clone方法权限修饰符是protected,不重写无法调用

String类

被final修饰的类不可以被继承,所有的方法都不改变原字符串,以下方法全部都有返回值

concat()

str.concat("aaa");

在字符串的尾部添加一个字符串

trim()

str.trim();

删除字符串中头部和尾部的空格

replace()

str.replace("old","new");

将字符串中的字符(字符串)替换为新的字符(字符串)

toLowerCase()

str.toLowerCase();

将字符串全部转为小写

toUpperCase()

str.toUpperCase();

将字符串全部转为大写

subString()

str.subString(5);
str.subString(5,10);

将字符串中从指定位置开始后的所有字符串(指定位置范围)切割

chatAt()

str.index(0);

获取字符串中指定下标的字符

indexOf()

str.indexOf('a');
str.indexOf("as");
str.indexOf('a',3);

根据字符或字符串从头(指定位置)开始匹配,如果找到,则返回索引,否则返回-1

startsWith()

str.starsWith("asad")

查看字符串是否以某字符串开头,是则true,否则返回false

endsWith()

str.endsWith("dsawd");

查看字符串是否以某字符串结尾,是则返回true,否则返回false

lastIndexOf()

str.lastIndexOf("sd");

从后往前搜索,查看是否有此字符(字符串)优则返回索引,如没有则返回-1

isEmpty()

str.isEmpty();

擦看是不是空字符串,返回布尔值

contains()

str.contains("d");

查看是否包含此字符串

compareto()

str.copareto("hello");

与hello字符串进行比较,如果相等则返回0,如果比hello大则返回一个大于0的值,如果更小则返回一个负数

comparetoIgnoreCase()

str.comparetoIgnoreCase("hello");

与hello字符串进行比较(忽略大小写),如果相等则返回0,如果比hello大则返回一个大于0的值,如果更小则返回一个负数

split()

str.split(" ");

已“ ”为标记将字符串进行切割

equals()

str.equals("dsf");

将字符串的值与dsf进行值比较,返回布尔值

toCharArray()

str.toCharArray();

将字符串转换为字符数组

StringBuffer和StringBuilder类

四个构造方法,无参表示开辟一个16字节的缓冲,传入字符串则开辟一个字符串的空间,缓冲根据需要自动扩充

开辟一个缓冲区,来存放可变字符串,StringBuffer是线程安全的,StringBuilder是线程不安全的

所有操作都是在原对象的基础上操作,不产生新字符串

append()

StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer("dsdes");
sb.append("afsf");

将一个字符串拼接到字符串的后面

insert()

sb.insert(3,"dhsjn");

传入两个参数,一个要插入的位置,一个要插入的值,插入的位置的字符往后放

deleteCharAt()

sb.deleteCharAt(0);

删除指定位置索引的字符

delete()

sb.delete(3,6);

将指定位置的字符串删除

subString()

sb.subString(3,5);

截取其中的一个子字符串

replace()

sb.replace(0,3,"dse");

将指定为字的字符串替换成输入的字符串

reverse()

sb.reverse();

将字符串进行逆转

charAt()

sb.charAt(0);

获取指定索引位置的字符

indexOf()

sb.indexOf("dfffrd");

查找字符串,并返回位置(第一个位置的索引)

lastIndexOf()

sb.lastIndexOf("dsfrdrf");

从后往前找指定的字符串,如果找到则返回位置索引(第一个字符的索引)

toString()

sb.toString();

转换成字符串类型

包装类

八个基本类型的和他们的包装类,将八个基本类型包装成对象

此处只介绍Integer其他七个类似

Integer

基本类型和他们的包装类以及String之间的转换
//int --> Integer
Integer i1 = new Integer(10);
Integer i2 = Integer.valueOf(10);

//int --> String
String s1 = 10+"";
String s2 = String.valueOf(20);
String s3 = Integer.toString("100");

//Integer --> int
int ii = i1.intValue();

//Integer --> String
String s4 = i1.toString();

//String --> int
int i = Integer.parseInt("100");

//String --> Integer
Integer i3 = new Integer("200");
Integer i4 = Integer.valueOf("300");

将int包装成对象

compareto()

将两个Integer对象进行比较大于返回1,相等返回0,更小返回-1(Character进行比较时返回之间的差值)

Character

一些字符比较方法

isDigit()

isLetter()

isLetterOrDigit()

isLowerCase()

isUpperCase()

isspaceChar()

isWhitespace()

toLowerCase()

toUpperCase()

int i = c1.compareTo(c2); // 差值
boolean isDigit = Character.isDigit('a'); //确定指定字符是否为数字
boolean isLetter = Character.isLetter('a');//确定指定字符是否为字母
boolean letterOrDigit = Character.isLetterOrDigit('a'); //确定指定字符是否为字母或数字
boolean lowerCase = Character.isLowerCase('a'); //确定指定字符是否为小写字母
boolean upperCase = Character.isUpperCase('a'); //确定指定字符是否为大写字母
boolean spaceChar = Character.isSpaceChar(' '); // 是否是空格
boolean whitespace = Character.isWhitespace(' ');// 是否是空格
char toLowerCase = Character.toLowerCase('A'); // 转小写
char toUpperCase = Character.toUpperCase('a'); // 转大写

自动拆装箱

自动装箱

Integer i = 10;

自动拆箱

Integer m = Integer.ValueOf(10);
Integer n = new Integer(20);
int k = m+n;

其他类

Math类

一下全是静态方法

ceil()

向上取整

floor()

向下取整

round()

四舍五入

Random类

nextInt()随机去一个范围内的整数

Random r = new Random();
int nextInt = r.nextInt(101);

System

System.out

输出流

System.in

输入流

System.err

错误输出流,输出字体问红色

System.exit(0)

退出虚拟机

System.currentTimeMillis()

获取当前时间的毫秒值(时间从1970年开始计时)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值