Java常用类

10 篇文章 0 订阅
1 篇文章 0 订阅

Java常用类



一、包装类

    包装类简单来说就是java基础数据类型中,使用对象的方式进行升级,它们既然都是类的方式存在,那他们理所当然具有类的特征,大多包装类均具有如下方法:


  • 带有基本值参数并创建对象的构造方法,如何可以使用Integer包装类创建对象
  • 带有字符串参数并创建包装类对象的构造方法
  • 对同一个类的两个对象进行比较的equals()方法,如obj1.equals(obj2).
  • 带生成哈希表代码的hasCode()方法
  • 将字符串转换为基本值的parseType()方法
  • 可生成对象基本值的typeValue()方法
基本数据类型 			对应包装类
boolean 			Boolean
byte 				Byte
short 				Short
int 				Integer
long 				Long
char 				Character
float 				Float
double 				Double
二、 Integer类
2.1 Integer类介绍

 Integer是int基本数据类型的包装类,其定义为:

public final class Integer
extends Number
implements Comparable<Integer>

 字段:

static int BYTES
用于二进制补码形式表示 int值的字节数。
static int MAX_VALUE
恒控股的最大值的 int可以有,2 三十一-1static int MIN_VALUE
恒持最小值的 int可以有,- 2 三十一。
static int SIZE
用于二进制补码形式表示 int值的比特数。
static<Integer> TYPE
类代表原始类型的实例 int

 构造方法:

Integer(int value)
构建了一个新分配的 Integer表示指定的 int价值。
Integer(String s)
构建了一个新分配的 Integer表示 int值表示的 String参数。

2.2 包装类的使用方法

 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同),例如:

// 包装类的使用与基本数据类型转换
public static void test1() {
    // 定义int基本数据类型
    int num1 = 100;
    // 把int基本数据类型创建为包装类型
    Integer num2 = new Integer(num1);
    Integer num3 = new Integer(1000);
    // 把Integer包装类型转换基本数据类型
    byte num7 = num3.byteValue();
    // 转换int类型
    int num4 = num3.intValue();
    // 转换为double类型
    double num5 = num3.doubleValue();
    // 转换float类型
    float num6 = num3.floatValue();
    Integer num8 = 100;
    System.out.println("num1=" + num1);
    System.out.println("num2=" + num2);
    System.out.println("num3=" + num3);
    System.out.println("num4=" + num4);
    System.out.println("num5=" + num5);
    System.out.println("num6=" + num6);
    System.out.println("num7=" + num7);
}

包装类型与字符串类型的转换:

// 包装类型转换为字符串类型转换
public static void test2() {
    // 从包装类型转换到字符串转换三种方式:
    // 方法1、toString()
    Integer num = new Integer(200);
    System.out.println(num.toString());
    // 方法2:
    String strNum2 = num + "";
    System.out.println("strNum2=" + strNum2);
    // 方法3:
    String strNum3 = String.valueOf(num);
    System.out.println("strNum4=" + strNum3);
}

字符串转换包装类型:

// 字符串类型转换为包装类型
public static void test3() {
    // 提供字符串一定为数字字符串,否则有异常
    String strNum = "23";
    // 从字符串类型转换到包装类型三种方式:
    // 方法1:
    Integer num1 = Integer.parseInt(strNum);
    System.out.println("num1=" + num1);
    // 方法2:
    Integer num2 = Integer.valueOf(strNum);
    System.out.println("num2=" + num2);
    // 方法3:
    Integer num3 = new Integer(strNum);
    System.out.println("num3=" + num3);
}

两个数的比较:

// 两个包装数值比较
public static void test4() {
    Integer num1 = new Integer(100);
    Integer num2 = new Integer(100);
    // 地址比较
    System.out.println("num1==num2:" + (num1 == num2));
    // 内容比较
    System.out.println("num1==num2--2:" + (num1.intValue() ==
    num2.intValue()));
    // 通过方法比较
    // 值 0如果这 Integer等于参数 Integer;值小于 0如果这 Integer数值小于参		  数
    Integer;和一个值大于 0如果这
    // Integer数值大于论证 Integer(签署的比较)。
    System.out.println("num1==num2--3:" + num1.compareTo(num2)); // 	返回0相等
}
三、String类
3.1 java中的字符串

在java中String类型类的定义:

public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence

String类型构造方法:

构造器 							描述
String() 					构造一个空字符串对象
String(byte[] bytes) 		通过byte数组构造字符串对象
String(char[] chars) 		通过字符数组构造字符串对象
String(String original) 	构造一个original,的副本即拷贝一个original
String(StringBuffer buffer) 通过StringBuffer数组构造字符串对象
3.2 字符串的不变性

String 对象创建后则不能被修改,是不可变的,所谓的修改其实是创建了新的对象,所指向的内存空间不同。例如:

// 字符串的不变性
public static void test1() {
    String str1 = "IT世界";
    String str2 = "IT世界";
    String str3 = new String("IT世界");
    String str4 = new String("IT世界");
    str1 = "欢迎您来到IT世界";
    System.out.println(str1 == str2);
    System.out.println(str1 == str3);
    System.out.println(str3 == str4);
}

3.3 String类常用方法

Java提供对应类很多方法,如:charAt()、compareTo()、concat()、copyValueOf()、endsWith()、getBytes()、getChars()、indexOf()、length()、replace()、subString()、toCharArray()、toLowerCase()等方法。根据API文档,最常用的:

// 字符串的常用方法-1
public static void test2() {
    // 定义字符串
    String str1 = "来 一起 学习java编程";
    // 字符长度
    System.out.println("字符串长度:" + str1.length());
    // j在字符串中的位置(从0开始计算,如果不存在返回-1)
    System.out.println(" j在字符串中的位置:" + str1.indexOf("j"));
    // 拆分成数组
    String[] strArray = str1.split(" ");
    System.out.println(Arrays.toString(strArray));
    // 获得字符串[3,7)
    // 使用 substring(beginIndex , endIndex) 进行字符串截取时,包括 beginIndex 位
    置的字符,不包括 endIndex 位置的字符
    System.out.println("获得[3,7)之间字符串:" + str1.substring(3,7));
    // 否则HelloWorld.java的文件名是否为合法的java源代码文件
    // 判断xiaoyi@qq.com是否为合法电子邮箱
}
// 字符串的常用方法-2
public static void test3() {
	String str1 = " 来 一起 学习java编程 ";
    System.out.println(str1.length());
    System.out.println("去掉字符串的空格:" + str1.trim().length() );
    System.out.println("转换大写:" + str1.toUpperCase());
    System.out.println("转换小写:" + str1.toLowerCase());
    // 从字符串获得单个字符
    System.out.println("从字符串中获取单个字符:" + str1.charAt(1));
    // 比较两个字符串的内容是否相同
    String str2 = new String(" 来 一起 学习java编程 ");
    System.out.println(str1.equals(str2));
    // 定义一个字符串
    // 统计指定字符串中字符 ‘a’ 出现的次数
    String s = "aljlkdsflkjsadjfklhasdkjlflkajdflwoiudsafhaasdasd";
    // 这里我看别人做的特别简单,s.split("a").length
    // 显然是有错误的,你可以试一试
}
四、StringBuffer类与StringBuilder类

由于String类型是一个不可变类型,在实际应用中,经常需要对字符串进行动态修
改,这时,String类的功能受到限制,所以StringBuffer类与StringBuilder类就很好解决此问题。


看了Java API文档,我看的是1.8版本的,此类中提供字符串动态添加、插入和替换等操作。


StringBuffer类是线程安全,如果不考虑线程安全的问题StringBuilder类的性能要好的多,所以推荐使用StringBuilder类。

/**************************
*
* @author chong
* 动态字符串测试
*
*/
public class StringBufferTestDemo {
    public static void main(String[] args) {
        // test1();
        // test3();
        test2();
    }
	// 动态字符串拼接
	public static void test1() {
    	// 创建一个空StringBuffer动态字符串
    	StringBuffer stringBuffer = new StringBuffer("你好啊!");
    	// 在后面追加
    	stringBuffer.append("和我一起学Java");
    	// 在后面追加
    	stringBuffer.append(520);
    	System.out.println("字符串长度:" + stringBuffer.length());
        System.out.println("插入前:" + stringBuffer);
        // 在指定位置插入字符串
        stringBuffer.insert(3, "xx");
        System.out.println("插入后:" + stringBuffer.toString());
    }
    // StringBuffer测试
    public static void test2() {
        StringBuffer stringBuffer = new StringBuffer("0123456789");
        // System.out.println("反转后:" + stringBuffer.reverse());
    }
    // StringBuiler的使用
    public static void test3() {
        StringBuilder builder = new StringBuilder();
        builder.append("hello,").append("world!");
        System.out.println(builder);
    }
}
五、Date与SimpleDateFormat的使用

java在日期类中封装了有关日期和时间的信息,类Date(时间)表示特定瞬间,精确到毫秒。

SimpleDateFormat对应日期格式化类。

 构造器:

Date()
分配一个 Date对象并将它初始化,它代表的时间分配给它,测量精确到毫秒。
Date(long date)
分配一个 Date对象并将它初始化为代表指定的毫秒数自基准时间被称为“时代”,即197011日,
00:00:00 GMT。

测试:

/*****************************
*
* @author chong java.util.Date的使用方法测试
*
*/
public class DateAndDateFormatTestDemo {
    public static void main(String[] args) {
        // test1();
        // test2();
        test3();
    }
    // Date对象测试
    public static void test1() {
        // 实例化java.util.Date对象
        Date now = new Date();
        System.out.println("now:" + now);
        System.out.println(now.getTime()); // 从1970至今的毫秒数
    }
    // Date类型转换测试
    public static void test2() {
        // 从字符串类型格式化为字符串类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	Date date = new Date();
    	// 按照格式进行格式Date类型为字符串类型
    	String strDate = dateFormat.format(date);
    	System.out.println(strDate);
    }
    // 字符串类型转换Date类型
    public static void test3() {
        String str = new String("2019-09-23");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
        	Date mydate = dateFormat.parse(str);
        	System.out.println(mydate);
        } catch (ParseException e) {
        	e.printStackTrace();
        }
    }
}

六、Calendar类使用

Calendar类在java中表示日历,但注意Calendar类是一个抽象类,所有不能直接通过new关键字创
建Calendar实例,可借助于该类提供的静态方法getInstance()方法获得Calendar对象。

测试:

/*****************************
*
* @author chong
* 日期类测试
*
*/
public class CalendarTestDemo {
    public static void main(String[] args) {
        // test1();
        test2();
    }
    public static void test1() {
        // 通过自身定义的静态方法创建对象,而不能自己new方法进行构造
        Calendar calendar = Calendar.getInstance();
        // 年
        System.out.println("year:" + calendar.get(Calendar.YEAR));
        // 月
        System.out.println("month:" + (calendar.get(Calendar.MONTH) + 1) );
        // 日
        System.out.println("day:" + calendar.get(Calendar.DAY_OF_MONTH));
        // 小时
        System.out.println("hours:" + calendar.get(Calendar.HOUR_OF_DAY));
        // 分钟
        System.out.println("min:" + calendar.get(Calendar.MINUTE));
        // 秒
        System.out.println("sencond:" + calendar.get(Calendar.SECOND));
    }
    // 测试
    public static void test2() {
        // 创建
        Calendar calendar = Calendar.getInstance();
        // 讲Calendar对象转换Date对象
        Date date = calendar.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        // 格式化转换
        String strDate = dateFormat.format(date);
        System.out.println(strDate);
   	}
}

七、Random类使用

Random类用来生产随机数,位于java.util包中。

测试:

/********************************
*
* @author chong 随机数类测试
*
*/
public class RandomTestDemo {
    public static void main(String[] args) {
    	test1();
    }
    public static void test1() {
        Random random = new Random();
        for(int i = 0;i < 10;i++) {
        	System.out.println(random.nextInt(10));
        }
    }
}

八、Math类使用

Math(算术运算)类是一个基于数学工具类,大部分提供是静态的方法完成基础的数学运算。

具体要用的API文档中写了很多方法,测试一下:

测试:

/************************
*
* @author chong
* 数学工具类使用方法
*/
public class MathTestDemo {
    public static void main(String[] args) {
    	test1();
    }
    public static void test1() {
        // PI
        double pi = Math.PI;
        System.out.println(pi);
        // 计算两个数最比较大
        int maxValue = Math.max(100, 201);
        System.out.println(maxValue);
    }
}

总结:

• 借助包装类,可以把基本数据类型包装为对象
• String字符串对象的内容一旦建立,是不可改变的
• StringBuffer通过字符串缓冲区实现了对字符串的直接修改
• 通过设置随机种子让Random类产生各种随机数
• Date表示确切的时间,大部分方法迁移到了Calendar类中
• Calendar抽象类为日历操作的主要入口,它有一个子类GregorianCalendar • SimpleDateFormat提供了对文本、日期的转化及格式化
• Math最终类的方法均是静态方法,方便数学运算使用



对于Java常用类还有很多,每天学一点记一点,总结一下,虽然也有很多地方不太明白,但多加练习,能够慢慢去懂!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值