Java工具类

Object

Java API :JDK提供的各种功能的Java类。

  • HashCode()
    返回对象的哈希码,哈希码是一个代表对象十六进制的整数。实际上默认的哈希码是将对象的内存地址通过某种转换得到的,所以不同对象会有不同的哈希码。
    public int hashCode(){};
  • equals()
    比较两个对象引用的值是否相等(比较地址)。
    方法:public boolean equals(Object obj){};
    equals()方法和运算符“ = = “的区别
    默认情况下equals只能比较引用类型 ,”= ="而可以比较引用类型和数值类型。如下代码是源码之一:
    public boolean equals(Object obj) { return (this == obj);}
    Java中有个规定:**如果equals( )返回两个对象是相等的,那这两个对象上调用hashCode( )返回的整数必须相等。**否则在使用Hash类型集合时就会产生错误。

注意:覆盖equals( )方法同时,还要记得覆盖hashCode( )方法。需要说明,如果equals( )返回两个对象不等,它们的hashCode( )也可以返回相同的整数。但是最好让它们的hashCode( )返回不同的整数,这有利于提高Hash类型集合的性能。

  • toString()
    返回类名@hashcode;事实上返回这样的字符串没有什么实际的意义。一般子类都会覆盖该方法,让它返回有意义的文本
    public String toString(){};

包装类

Everything is object.

  • 基本数据类型和对应包装类
数据类型包装类
booleanBoolean
byteByte
charCharacter
doubleDouble
floatFloat
intInteger
shortShort
longLong
  • 包装类常用方法属性
    使用Character对字符判断
Character()方法作用
static boolean isDigit(char ch)判断字符ch是否为数字
static boolean isLetter(char ch)判断字符ch是否为字母
static boolean isLowerCase(char ch)判断字符ch是否为小写字母
static boolean isSpaceChar(char ch)判断字符ch是否为Unicode中的空格
  • 字符串和基本数据类型转换为包装类
    基本数据类型转换为包装类
int a = 500;
Integer b = new Integer(a);

字符串转换为包装类

//字符串通过构造方法转换为包装类
String s = "xiaochen";
Integer w = new Integer(s);
//字符串通过包装类的ValueOf(String s)转换为包装类
String s = "xiaochen";
Integer w = Integer.ValueOf(s);

注意:字符串不能通过以上两种方式转换为Character

  • 包装类转换为其他数据类型
    包装类转换为基本数据类型
Integer i = new Integer(500);
int a = i.intValue();

包装类转换为字符串(通过toString()方法)

Integer i = new Integer(500);
String s = i.toString();

字符串转换为基本数据类型(通过parseXXX(String s)方法)

String s = "500";
int i = Integer.parseInt(s);

字符串类

  • String和StringBuffer源码
//String 源码  final修饰
public final class String
//StringBuffer
public String(StringBuffer buffer) {
        synchronized(buffer) {
            this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
        }
    }
  • String的创建
    静态创建(像给变量赋值形式)
String s1 = "abc"; 
String s2 = "abc";

动态创建(动态的内存分配,使用new运算符)

String s3= new String("abc"); 
String s4= new String("abc");

区别

使用静态方式创建的字符串,在方法区的常量池中只会产生唯一一个字符串对象,使用该方式产生同样一个字符串时,内存中不再开辟另外一块空间,而是两个引用变量指向同一个字符串对象。
使用动态方式创建的字符串,在堆内存中会产生出不同的对象。

  • 字符串类特点
    1.任何一个String对象在创建之后都不能对它的内容作出任何改变
    2.连接、获得子串和改变大小写等操作,如果返回值同原字符串不同,实际上是产生了一个新的String对象
    3.在程序的任何地方,相同的字符串字面常量都是同一个对象
    4.String类重置了equals方法,用于比较两个字符串的内容

  • String类常用方法

方法作用
boolean equals(String)判断两个字符串对象的内容是否相等
boolean equalsIgnoreCase(String)比较两个字符串的内容是否相等,忽略大小写
String toUpperCase()将String对象中的所有字符都转换为大写
String toLowerCase()将String对象中的所有字符都转换为小写
char charAt(int)返回指定索引处的 char 值
String substring(int begin)返回一个新字符串,该字符串是从begin开始的字符串的内容
String substring(int begin,int end)返回一个新字符串,该字符串是从begin开始到end-1结束的字符串的内容
int indexOf/lastIndexOf(char)返回指定字符在此字符串中第一次/最后一次出现处的索引
int indexOf/lastIndexOf(char,int)从指定的索引开始搜索,返回在此字符串中第一次/最后一次出现指定字符处的索引
int indexOf/lastIndexOf(String)返回第一次出现的指定子字符串在此字符串中的索引
int indexOf/lastIndexOf(String,int)从指定的索引开始搜索,返回在此字符串中第一次/最后一次出现指定字符串处的索引
String trim()返回新的字符串,忽略前导空白和尾部空白
int length()返回此字符串的长度
String[] split(String regex)根据给定正则表达式的匹配拆分此字符串。
String replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的
  • StringBuffer类常用方法
方法作用
int capacity()返回当前容量
int length()返回长度(字符数)
StringBuffer reverse()将此字符序列用其反转形式取代
void setCharAt(int,char)将给定索引处的字符设置为 ch
StringBuffer delete(int begin,int end)移除此序列的子字符串中的字符
char charAt(int)返回此序列中指定索引处的 char 值
String toString()将StringBuffer对象转换成相应的String
StringBuffer append(String str)将 int 参数的字符串表示形式追加到此序列
StringBuffer append(int num)将int参数的字符串表示形式追加到此序列
StringBuffer append(Object o)追加Object参数的字符串表示形式
StringBuffer insert(int index,String str)将字符串插入此字符序列中
StringBuffer insert(int index,char ch)将字符插入此字符序列中
StringBuffer insert(int index,Object o)将 Object 参数的字符串表示形式插入此字符序列中
  • StringBuffer使用(StrigBuffer节省内存开销)
    想要判断两个StringBuffer变量的内容是否相等,应先用toString()将他们转换成String对象。
  • StringBuilder
    简介:StringBuilder类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候
方法作用
int capacity()返回当前容量
int length()返回长度(字符数)
StringBuilder reverse()将此字符序列用其反转形式取代
void setCharAt(int,char)将给定索引处的字符设置为 ch
StringBuilder delete(int begin,int end)移除此序列的子字符串中的字符
char charAt(int)返回此序列中指定索引处的 char 值
String toString()将StringBuilder对象转换成相应的String
StringBuilder append(String str)将 int 参数的字符串表示形式追加到此序列
StringBuilder append(int num)将int参数的字符串表示形式追加到此序列
StringBuilder append(Object o)追加Object参数的字符串表示形式
StringBuilder insert(int index,String str)将字符串插入此字符序列中
StringBuilder insert(int index,char ch)将字符插入此字符序列中
StringBuilder insert(int index,Object o)将Object参数的字符串表示形式插入此字符序列中
  • StringBuffer类和String类比较
    同:Java中定义了String与StringBuffer两个类来封装对字符串的各种操作;String类与StringBuffer类都被放到了java.lang包中。
    异:1.String类对象中的内容初始化不可以改变2.StringBuffer类对象中的内容可以改变
  • StringBuffer类和StringBulider类比较
    同:StringBuffer和StringBuilder都是长度可变的字符串。两者的操作基本相同
    异:1.StringBuffer类是线程安全的,StringBuilder类是线程不安全的。
    2.StringBuffer在JDK1.0中就有,而StringBuilder是在JDK5.0后才出现的
    3.StringBuilder的一些方法实现要比StringBuffer快些。

Math类

Math类提供了大量用于数学运算的方法,Math类是final类,因此不能从Math类继承,Math类中的方法都是static方法,因此不必创建Math类的对象就可以直接使用该类的方法.
源码代码实现:

public final class Math(){
	//Math类中的常量
	public static final double E = 2.7182818284590452354;
	public static final double PI = 3.14159265358979323846;
	//abs(int)
	public static int abs(int a) {
        return (a < 0) ? -a : a;
    }
	//abs(double)
	public static double abs(double a) {
        return (a < 0) ? -a : a;
    }
	//ceil(double)
	public static double ceil(double a) {
        return StrictMath.ceil(a); // default impl. delegates to StrictMath
    }
	//floor(double)
	public static double floor(double a) {
        return StrictMath.floor(a); // default impl. delegates to StrictMath
    }
	//max(int a,int b)
	public static int max(int a, int b) {
        return (a >= b) ? a : b;
    }
	//min(int a,int b)
	public static int max(int a, int b) {
        return (a <= b) ? a : b;
    }
	//min(double a,double b)
	public static double max(double a, double b) {
        return (a <= b) ? a : b;
    }
	//random()
	private static final class RandomNumberGeneratorHolder {
        static final Random randomNumberGenerator = new Random();
    }
    public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
    }
	//round()
	public static int round(float a) {
        int intBits = Float.floatToRawIntBits(a);
        int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
                >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
        int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
                + FloatConsts.EXP_BIAS) - biasedExp;
        if ((shift & -32) == 0) { // shift >= 0 && shift < 32
            // a is a finite number such that pow(2,-32) <= ulp(a) < 1
            int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                    | (FloatConsts.SIGNIF_BIT_MASK + 1));
            if (intBits < 0) {
                r = -r;
            }
            // In the comments below each Java expression evaluates to the value
            // the corresponding mathematical expression:
            // (r) evaluates to a / ulp(a)
            // (r >> shift) evaluates to floor(a * 2)
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
            return ((r >> shift) + 1) >> 1;
        } else {
            // a is either
            // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
            // - an infinity or NaN
            return (int) a;
        }
    }
	//sin/cos/tan(double)
	//sin
	public static double sinh(double x) {
        return StrictMath.sinh(x);
    }
    //cos
    public static double cosh(double x) {
        return StrictMath.cosh(x);
    }
    //tan
    public static double tanh(double x) {
        return StrictMath.tanh(x);
    }
	//sqrt(double)
	public static double sqrt(double a) {
        return StrictMath.sqrt(a); // default impl. delegates to StrictMath
                                   // Note that hardware sqrt instructions
                                   // frequently can be directly used by JITs
                                   // and should be much faster than doing
                                   // Math.sqrt in software.
    }
}

日期类型

我们应该多使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和分析日期字符串;Date 中的相应方法已废弃

方法作用
boolean after(Date when)测试此日期是否在指定日期之后
boolean before(Date when)测试此日期是否在指定日期之前
int compareTo(Date anotherDate)比较两个日期的顺序。如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。
boolean equals(Object obj)比较两个日期的相等性
  • Calendar类
    1.常用的日期处理对象。可以设置直接的时区和国际化格式
    2.是一个抽象类
    3.Calendar 抽象类定义了足够的方法,让我们能够表述日历的规则
    4.获取Calendar对象的实例:
    Calendar c = new Calendar.getInstance();
    5.设置Calendar时间
    c.set(1997,7,28);
    6.获取指定时间属性
    c.get(Calendar.YEAR);
属性含义
static int HOUR小时时间
static int MINUTE分时间
static int SECOND秒时间
static int DATE日期的日部分
static int MONTH日期的月部分
static int YEAR日期的年部分
  • SimpleDateFormat类
    SimpleDateFormat使得可以选择任何用户定义的日期-时间格式的模式
    1.获取SimpleDateFormat的实例
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
    2.将日期格式成指定的字符串
    sdf.format(new Date());
    3.将格式化的字符串转换成日期对象
    sdf.parse(“2011-07-16”);
  • SimpleDateFormat类中的模式字母
字母日期或时间元素
y
M年中的月份
d月份中天数
E星期中天数
aAm/pm标记
H一天中的小时数(0-23)
han/pm中的小时数(1-12)
m小时中的分钟数
s分钟中的秒数
S毫秒数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值