Java核心类库篇2——lang

Java核心类库篇2——lang

1、Object

该类是所有类的父类,每个类都使用它作为超类,没有任何属性

方法声明功能介绍
Object()使用无参方式构造对象
boolean equals(Object obj)用于判断调用对象是否与参数对象相等。 该方法默认比较两个对象的地址是否相等
int hashCode()用于获取调用对象的哈希码值(内存地址的编号)
String toString()用于获取调用对象的字符串形式 该方法默认返回的字符串为:包名.类名@哈希码值的十六进制
Class getClass()用于返回调用对象执行时的Class实例,反射机制使用
  • equals:与 == 运算符的结果一致,若希望比较两个对象的内容,则需要重写该方法。 若该方法被重写后,则应该重写hashCode方法来保证结果的一致性
  • hashCode:若两个对象调用equals方法相等,则各自调用该方法的结果必须相同,若两个调用对象equals方法不相等,则各自调用该方法的结果应该不相同,为了使得该方法与equals方法保持一致,需要重写该方法
  • toString:为了返回更有意义的数据,需要重写该方法 使用print或println打印引用或字符串拼接引用都会自动调用该方法

2、包装类

基本类型并不具有对象的性质,为了与其他对象“接轨”就出现了包装类型(如我们在使用集合类型Collection时就一定要使用包装类型而非基本类型),它相当于将基本类型“包装起来”,使得它具有了对象的性质,并且为其添加了属性和方法,丰富了基本类型的操作。

基本类型包装类
booleanjava.lang.Boolean
charjava.lang.Character
bytejava.lang.Byte
shortjava.lang.Short
intjava.lang.Integer
longjava.lang.Long
floatjava.lang.Float
doublejava.lang.Double
  • 基本类型的优势:数据存储相对简单,运算效率比较高
  • 包装类的优势:有的容易,比如集合的元素必须是对象类型,满足了java一切皆是对象的思想
  • 声明方式不同,基本类型不适用new关键字,而包装类型需要使用new关键字来在堆中分配存储空间;
  • 存储方式及位置不同,基本类型是直接将变量值存储在堆栈中,而包装类型是将对象放在堆中,然后通过引用来使用;
  • 初始值不同,基本类型的初始值如int为0,boolean为false,而包装类型的初始值为null
  • 使用方式不同,基本类型直接赋值直接使用就好,而包装类型在集合如Collection、Map时会使用到

2.1、包装类的装箱拆箱

public class Test {
    public static void main(String[] args) {
        //自动装箱拆箱
        int a=10;
        Integer integer=a;
        int b=integer;
        System.out.println(a);
        System.out.println(integer);
        System.out.println(b);
        //手动装箱拆箱
        int a1=10;
        Integer integer1=new Integer(a1);
        int b1=integer1.intValue();
        System.out.println(a);
        System.out.println(integer);
        System.out.println(b);
    }
}

2.2、包装类与字符串的转换

public class Test {
    public static void main(String[] args) {
        //解析字符串
        String a="100";
        //Java为了提高拆装箱效率,在执行过程中提供了一个缓存区(对象池)【类似于常量数组】
        //如果传入的参数是,-128<参数<127会直接去缓存查找数据,如果有久直接产生,如果没有就隐式调用new方法产生
        System.out.println(Integer.parseInt(a));
        String b="100.2";
        System.out.println(Float.parseFloat(b));
        String c="true";
        System.out.println(Boolean.parseBoolean(c));
        //转为字符串
        Integer d=100;
        System.out.println(d.toString());
    }
}

3、Math

3.1、属性

属性介绍
Math.PIπ
Math.Ee
public class Test {
    public static void main(String[] args) {
        System.out.println("math属性================");
        System.out.println(Math.PI);
        System.out.println(Math.E);
    }
}
math属性================
3.141592653589793
2.718281828459045

3.2、三角函数方法

方法声明功能介绍
public static double sin(double radians)正弦函数
public static double cos(double radians)余弦函数
public static double tan(double radians)正切函数
public static double toRadians(double degree)度转换成弧度
public static double toDegree(double radians)弧度转换成度
public static double asin(double a)反正弦
public static double acos(double a)反余弦

3.2、指数函数方法

方法声明功能介绍
public static double exp(double x)e^x
public static double log(double x)ln(x)
public static double log10(double x)log 10(x)
public static double pow(double a,double b)a^b
public static double sqrt(double x)√x

3.3、取整方法

方法声明功能介绍
public static double ceil(double x)天花板的意思,就是逢余进一
public static double floor(double x)地板的意思,就是逢余舍一
public class Test {
    public static void main(String[] args) {
        double a=2.5;
        System.out.println(Math.ceil(a));
        System.out.println(Math.floor(a));
    }
}
3.0
2.0

3.4、min、max、abs方法

方法声明功能介绍
public static int abs(int a)返回一个数的绝对值
public static int min(int a, int b)返回两个数的最小值
public static int max(int a, int b)返回两个数的最大值
public static double random()生成大于等于0.0且小于1.0的double型随机数
public class Test {
    public static void main(String[] args) {
        int a=5;
        int b=9;
        System.out.println(Math.max(a,b));
        System.out.println(Math.min(a,b));
        int c=-9;
        System.out.println(Math.abs(c));
        System.out.println(Math.random());
    }
}
9
5
9
0.30741550461739375

4、String

  • String类被final关键字修饰,意味着String类不能被继承,并且它的成员方法都默认为final方法
  • 字符串一旦创建就不能再修改
  • String类实现了Serializable、CharSequence、 Comparable接口
  • String实例的值是通过字符数组实现字符串存储的

4.1、构造方法

方法声明功能介绍
String str=“hello world!”字面量创建
public String()无参构造方法
public String(String original)用已知的字符串value创建一个String对象
public String(char value[])用字符数组value创建一个String对象
public String(char value[], int offset, int count)用字符数组chars的offset开始的count个字符创建一个String对象
public String(byte bytes[])用byte数组values创建一个String对象

常用的为字面量创建

4.2、字符串长度

方法声明功能介绍
public int length()字符串的长度
public class Test {
    public static void main(String[] args) {
        String str="hello world!";
        System.out.println(str.length());
    }
}
12

4.3、字符串中指定位置的字符

方法声明功能介绍
public char charAt(int index)字符串中指定位置的字符
public class Test {
    public static void main(String[] args) {
        String str="hello world!";
        System.out.println(str.charAt(1));
    }
}
e

4.4、提取子串

方法声明功能介绍
public String substring(int beginIndex)从beginIndex位置起,从当前字符串中取出剩余的字符
public String substring(int beginIndex, int endIndex)从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符
public class Test {
    public static void main(String[] args) {
        String str="hello world!";
        System.out.println(str.substring(2));
        System.out.println(str.substring(2,4));
    }
}
llo world!
ll

4.5、字符串比较

方法声明功能介绍
public int compareTo(String anotherString)对字符串内容按字典顺序进行大小比较
public int compareToIgnore(String anotherString)同上,忽略大小写
public boolean equals(Object anotherObject)比较当前字符串和参数字符串
public boolean equalsIgnoreCase(String anotherString)比较字符串,忽略大小写
public class Test {
    public static void main(String[] args) {
        String str="a";
        String str1="A";
        System.out.println(str.compareTo(str1));
        System.out.println(str.compareToIgnoreCase(str1));
        System.out.println("===========================");
        System.out.println(str.equals(str1));
        System.out.println(str.equalsIgnoreCase(str1));
    }
}
32
0
===========================
false
true

4.6、字符串连接

方法声明功能介绍
public String concat(String str)将字符串str连接到当前字符串的后面,效果等价于"+"

与直接+一致

4.7、字符串中单个字符查找

方法声明功能介绍
public int indexOf(int ch/String str)查找当前字符串中字符或子串,返回第一次出现的位置
public int indexOf(int ch/String str, int fromIndex)从fromIndex开始查找当前字符串中字符或子串,返回第一次出现的位置
public int lastIndexOf(int ch/String str)倒着查找当前字符串中字符或子串,返回第一次出现的位置
public int lastIndexOf(int ch/String str, int fromIndex)倒着从fromIndex开始查找当前字符串中字符或子串,返回第一次出现的位置
public class Test {
    public static void main(String[] args) {
        String str="hello world!";
        System.out.println(str.indexOf('o'));
        System.out.println(str.lastIndexOf('o'));
    }
}
4
7

4.8、字符串大小写转换

方法声明功能介绍
public String toLowerCase()所有字符转换成小写
public String toUpperCase()所有字符转换成大写
public class Test {
    public static void main(String[] args) {
        String str="Hello World!";
        System.out.println(str.toUpperCase());
        System.out.println(str.toLowerCase());
    }
}
HELLO WORLD!
hello world!

4.9、字符串中字符的替换

方法声明功能介绍
public String replace(char oldChar, char newChar)用字符newChar替换当前字符串中所有的oldChar字符
public String replaceFirst(String regex, String replacement)用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串
public String replaceAll(String regex, String replacement)用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串
public class Test {
    public static void main(String[] args) {
        String str="Hello World!";
        System.out.println(str.replace('o','v'));
        //正则表达式会独立开新篇
    }
}
Hellv Wvrld!

4.10、其他类方法

方法声明功能介绍
public String trim()截去字符串两端的空格,但对于中间的空格不处理
public boolean startsWith(String prefix)判断当前字符串是否以指定字符串开始
public boolean endWith(String suffix)判断当前字符串是否以指定字符串结束
public contains(String str)判断参数s是否被包含在字符串中
public String[] split(String str)将str作为分隔符进行字符串分解
public class Test {
    public static void main(String[] args) {
        String str=" Hello World!    ";
        System.out.println(str.trim());
        System.out.println(str.contains("ello"));
        System.out.println(str.startsWith(" Hello"));
        System.out.println(str.endsWith("World!    "));
    }
}
Hello World!
true
true
true

4.11、基本类型转为字符串

方法声明功能介绍
public static byte parseByte(String s)String转为byte
public static short parseShort(String s)String转为short
public static short parseInt(String s)String转为int
public static long parseLong(String s)String转为long
public static float parseFloat(String s)String转为float
public static double parseDouble(String s)String转为double

4.12、字符串转为基本类型

方法声明功能介绍
public static String valueOf(char data[])byte转String
public static String valueOf(char data[], int offset, int count)char[]转String
public static String valueOf(boolean b)boolean转String
public static String valueOf(char c)char转String
public static String valueOf(int i)int转String
public static String valueOf(long l)long转String
public static String valueOf(float f)float转String
public static String valueOf(double d)double转String

4.13、StringBuffer

  • StringBuffer类的对象能够被多次的修改,并且不产生新的未使用对象

  • 属于线程安全的类,效率比较低

4.13.1、构造函数
方法声明功能介绍
public StringBuffer()构造一个字符串缓冲区,其中没有字符,初始容量为16个字符
public StringBuffer(CharSequence seq)构造一个包含与指定字符相同的字符串缓冲区
public StringBuffer(int capacity)构造一个字符串缓冲区,其中没有字符,但是包含指定的初始容量capacity
public StringBuffer(String str)构造一个指定字符串内容的字符串缓冲区
4.13.2、方法
方法声明功能介绍
public StringBuffer append(String str)将指定的字符串追加到此字符序列
public StringBuffer append(StringBuffer sb)将指定的内容附加StringBuffer到此序列
public int capacity()返回当前容量
public char charAt(int index)返回char指定索引处的此序列中的值
public StringBuffer delete(int start, int end)删除此序列的子字符串中的字符
public StringBuffer deleteCharAt(int index)删除指定位置字符
public int indexOf(String str)指定子字符串第一次出现的字符串中的索引
public StringBuffer insert(int offset, String str)将字符串插入此字符序列
public int length()返回该字符串的长度
public StringBuffer replace(int start, int end, String str)用指定的字符替换此序列的子字符串中的字符String
public StringBuffer reverse()此字符序列的反向替换
public void setCharAt(int index, char ch)指定索引处的字符设置为ch
public void setLength(int newLength)设置字符序列的长度
public String toString()此序列中数据的字符串

4.14、StringBuilder

  • StringBuilder类的对象能够被多次的修改,并且不产生新的未使用对象
  • 属于非线程安全的类,效率比较高
4.14.1、构造方法
方法声明功能介绍
public StringBuilder()构造一个没有字符的字符串构建器,初始容量为16个字符
public StringBuilder(CharSequence seq)构造一个包含与指定的相同字符的字符串构建器
public StringBuilder(int capacity)造一个没有字符的字符串构建器,由构capacity参数指定的初始容量
public StringBuilder(String str)构造一个初始化为指定字符串内容的字符串构建器
4.14.2、方法
方法声明功能介绍
public StringBuilder append(String str)将指定的字符串附加到此字符序列
public StringBuilder append(StringBuffer sb)将指定 StringBuffer追加到这个序列
public int capacity()当前容量
public char charAt(int index)获取指定索引位的字符
public StringBuilder delete(int start, int end)删除指定索引间的字符串
public StringBuilde deleteCharAt(int index)删除指定位置的字符
public int indexOf(String str)指定子字符串第一次出现的字符串内的索引
public StringBuilder insert(int offset, String str)将字符串插入到此字符序列中
public int length()返回长度
public StringBuilder replace(int start, int end, String str)用指定的String中的字符替换此序列的子字符串中的String
public StringBuilder reverse()字符串顺序颠倒
public void setCharAt(int index, char ch)指定索引处的字符设置为ch
public void setLength(int newLength)设置字符序列的长度
public String substring(int start)返回指定位置后的字符串
public String toString()此顺序中的数据的字符串

5、System

主要用于获取系统的属性数据

5.1、属性

属性介绍
PrintStreamerr标准错误输出流
InputStreamin标准输入流
PrintStreamout标准输出流

5.2、方法

方法声明功能介绍
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)将指定源数组从指定位置复制到目标数组的指定位置
public static long currentTimeMillis()返回当前时间(以毫秒为单位)
static void exit(int status)终止当前运行的Java虚拟机
public static void gc()提醒运行垃圾回收器
public static String getenv(String name)获取指定环境变量的值
static Properties getProperties()获取当前的系统属性
static String getProperty(String key)获取指定键指示的系统属性
static String lineSeparator()系统相关的行分隔符字符串
static void setProperties(Properties props)将系统属性设置为Properties参数
static void setSecurityManager(SecurityManager s)设置系统安全性
public class Test {
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        System.out.println(System.getenv());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

眼眸流转

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值