温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中有哪些自带的常用类

发布时间:2021-06-22 15:53:18 来源:亿速云 阅读:259 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关Java中有哪些自带的常用类,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

一、字符串相关类

        String类:构造字符串对象 
        常量对象:字符串常量对象是用双引号括起的字符序列。        例如:"hello"、"96.97"、"boy"等。
        字符串的字符使用Unicode字符编码,一个字符占两个字节
        String类较常用构造方法:
        String  s1 = new String();
        String  s2 = new String(String original);
        String  s3 = new String(char[] a);
        String  s4 =  new String(char[] a,int startIndex,int count)


1.1 字符串的特性

    String是一个final类,代表不可变的字符序列.字符串是不可变的。一个字符串对象一旦被配置,其内容是不可变的。
    判断:
    String s1 = "猿小猴子";     
    String s2 = "java";
    String s4 = "java";
    String s3 = new String("java");
    System.out.println(s2 == s3);
    System.out.println(s2 == s4);
    System.out.println(s2.equals(s3));
    String s5 = "猿小猴子java";
    String s6 = (s1 + s2).intern();
    System.out.println(s5 == s6);
    System.out.println(s5.equals(s6));


1.2 字符串对象操作

public int length()
public char charAt(int index)
public boolean equals(Object anObject)
public int compareTo(String anotherString)
public int indexOf(String s)
public int indexOf(String s ,int startpoint)
public int lastIndexOf(String s)
public int lastIndexOf(String s ,int startpoint)
public boolean startsWith(String prefix)
public boolean endsWith(String suffix)
public boolean regionMatches(int firstStart,String other,int otherStart ,int length)


1.3 字符串对象修改

public String substring(int startpoint)
public String substring(int start,int end)
pubic String replace(char oldChar,char newChar)
public String replaceAll(String old,String new)
public String trim()
public String concat(String str)
public String[] split(String regex)
// 根据给定正则表达式的匹配拆分此字符串。

1.4 字符串与基本数据的相互转化

        字符串转换为基本数据类型
        Integer包装类的public static int parseInt(String s):可以将由“数字”字符组成的字符串转换为整型。类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法可以将由“数字”字符组成的字符串,转化为相应的基本数据类型。
        基本数据类型转换为字符串
        调用String类的public String valueOf(int n)可将int型转换为字符串;相应的valueOf(byte b)、valueOf(long l)、valueOf(float f)、valueOf(double d)、valueOf(boolean b)可由参数的相应类到字符串的转换

1.5 字符串与字符、字节数组

        字符串与字符数组
        String 类的构造方法:String(char[]) 和 String(char[],int offset,int length) 分别用字符数组中的全部字符和部分字符创建字符串对象 
        String类提供了将字符串存放到数组中的方法:
        public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
        将字符串中的全部字符存放在一个字符数组中的方法:
        public char[] toCharArray() 
        字符串与字节数组 
        String(byte[])用指定的字节数组构造一个字符串对象。String(byte[],int offset,int length) 用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。
        public byte[] getBytes() 方法使用平台默认的字符编码,将当前字符串转化为一个字节数组。
        public byte[] getBytes(String charsetName) 使用参数指定字符编码,将当前字符串转化为一个字节数组。                  
 

二、StringBuffer类

        java.lang.StringBuffer代表可变的字符序列,可以对字符串内容进行增删。
        很多方法与String相同,但StingBuffer是可变长度的。
        StringBuffer是一个容器。


2.1 StringBuffer类有三个构造方法:

        1.StringBuffer()初始容量为16的字符串缓冲区
        2.StringBuffer(int size)构造指定容量的字符串缓冲区
        3.StringBuffer(String str)将内容初始化为指定字符串内容    


2.2 StringBuffer类的常用方法

StringBuffer append(String s),   StringBuffer append(int n) ,  
StringBuffer append(Object o) ,  StringBuffer append(char n),
StringBuffer append(long n),  StringBuffer append(boolean n),
StringBuffer insert(int index, String str) 
public StringBuffer reverse() 
StringBuffer delete(int startIndex, int endIndex) 
public char charAt(int n )
public void setCharAt(int n ,char ch)
StringBuffer replace( int startIndex ,int endIndex, String str) 
public int indexOf(String str)
public String substring(int start,int end)
public int length()

三、StringBuilder类

StringBuilder 和 StringBuffer 非常类似,均代表可变的字符序列,而且方法也一样
String:不可变字符序列
StringBuffer:可变字符序列、效率低、线程安全
StringBuilder(JDK1.5):可变字符序列、效率高、线程不安全
String使用陷阱:
 string s="a"; //创建了一个字符串 s=s+"b"; 
补充:实际上原来的"a"字符串对象已经丢弃了,现在又产生了一个字符串s+"b"(也就是"ab")。如果多次执行这些改变串内容的操作,会导致大量副本字符串对象存留在内存中,降低效率。如果这样的操作放到循环中,会极大影响程序的性能。

四、日期类

1.java.lang.System类

        System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
此方法适于计算时间差。

        计算世界时间的主要标准有:
        UTC(Universal Time Coordinated)
        GMT(Greenwich Mean Time)
        CST(Central Standard Time)

2. java.util.Date类

        表示特定的瞬间,精确到毫秒

        构造方法:
        Date( )使用Date类的无参数构造方法创建的对象可以获取本地当前时间。
        Date(long date)
        常用方法
        getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
        toString():把此 Date 对象转换为以下形式的 String: dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun, Mon, Tue, Wed, Thu, Fri, Sat),zzz是时间标准。

        Date类的API不易于国际化,大部分被废弃了,java.text.SimpleDateFormat类是一个不与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期->文本)、解析(文本->日期)
        格式化:
        SimpleDateFormat() :默认的模式和语言环境创建对象
        public SimpleDateFormat(String pattern):该构造方法可以用参数pattern指定的格式创建一个对象,该对象调用:
        public String format(Date date):方法格式化时间对象date
解析:
        public Date parse(String source):从给定字符串的开始解析文本,以生成一个日期。

3. java.util.Calendar(日历)类

        Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能。
        获取Calendar实例的方法
        使用Calendar.getInstance()方法
        调用它的子类GregorianCalendar的构造器。

        一个Calendar的实例是系统时间的抽象表示,通过get(int field)方法来取得想要的时间信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、MINUTE、SECOND
 

public void set(int field,int value)
public void add(int field,int amount)
public final Date getTime()
public final void setTime(Date date)

五、 Math类

        java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型。
abs     绝对值
acos,asin,atan,cos,sin,tan  三角函数
sqrt     平方根
pow(double a,doble b)     a的b次幂
log    自然对数
exp    e为底指数
max(double a,double b)
min(double a,double b)
random()      返回0.0到1.0的随机数
long round(double a)     double型数据a转换为long型(四舍五入)
toDegrees(double angrad)     弧度—>角度
toRadians(double angdeg)     角度—>弧度

六、BigInteger类

        Integer类作为int的包装类,能存储的最大整型值为231 —1,BigInteger类的数字范围较lnteger类的数字范围要大得多,可以支持任意精度的整数。
构造器
        BigInteger(String val)
常用方法
        public BigInteger abs()
        public Biglnteger add(Biglnteger val)
        public Biglnteger subtract(Biglnteger val)
        public Biglnteger multiply(BigInteger val)
        public Biglnteger divide(BigInteger val)
        public Biglnteger remainder(Biglnteger val)
        public Biglnteger pow(int exponent)
        public BigInteger[] divideAndRemainder(BigInteger val)

七、BigDecimal类

        一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类。BigDecimal类支持任何精度的定点数。
构造器
        public BigDecimal(double val)
        public BigDecimal(String val)
常用方法
        public BigDecimal add(BigDecimal augend)
        public BigDecimal subtract(BigDecimal subtrahend)
        public BigDecimal multiply(BigDecimal multiplicand)
        public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)

关于Java中有哪些自带的常用类就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI