java 常用类

1. 掌握Java的八种基本数据类型的包装类

八种数据类型:

包装类:

2. 理解Integer类的源码

Java中Integer是基本数据类型int的包装类。也就是每一个Integer对象包含一个int类型的属性,是抽象类Number类的子类,位于java.lang包下。

源码:

public final class Integer extends Number implements Comparable<Integer> {

 

@Native public static final int MIN_VALUE = 0x80000000;

@Native public static final int MAX_VALUE = 0x7fffffff;

 

private final int value;

 

public Integer(int value) {

this.value = value;

}

 

public Integer(String s) throws NumberFormatException {

this.value = parseInt(s, 10);

}

}

 

3. 掌握String类的使用

字符串广泛应用 在 Java 编程中,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。

创建:

String str = "Runoob";

String 创建的字符串存储在公共池中,而 new 创建的字符串对象在堆上:

String s1 = "Runoob"; // String 直接创建

String s2 = "Runoob"; // String 直接创建

String s3 = s1; // 相同引用

String s4 = new String("Runoob"); // String 对象创建

String s5 = new String("Runoob"); // String 对象创建

 

4. 掌握Stringbuffer和StringBuilder的常用方法

Stringbuffer:

用于存储数据的容器。类似于字符串缓冲区,池子不能修改,但是“内容”可以通过方法修改,最主要的两个方法就是 append 和 insert 方法

特点:

1.长度是可变的

2.可以存储不同类型数据

3.最终要转成字符串进行使用

4.可以对字符串进行修改

方法:

StringBuffer append(data);尾部添加

StringBuffer insert(index,data);指定未知插入

 

eg:

StringBuffer sb=new StringBuffer(“abc”);

sb.append(“de”);

//打印sb 为abcde

 

StringBuffer delete(int start, int end)

StringBuffer deleteCharAt(int index):删除指定位置的元素

 

eg:

sb.delete(0,sb.length());

StringBuffer replace(start,end,string);

void setCharAt(index,char);

char charAt(index);

int indexOf(string);

 

StringBuilder:

功能和StringBuilder几乎一模一样,但是无线程安全,从1.5版本开始,为了提高效率,在单线程中尽量使用StringBuilder。

 

5. 掌握Date、DateFormat的使用

SimpleDateFormat 可以对日期时间进行格式化,(由于Date默认输出的时间格式不友好因此需要转换)如可以将日期转换为指定格式的文本,也可将文本转换为日期。 1. 使用format()方法将日期转换为指定格式的文本 Date d = new Date(); SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//指定转换的目标格式,"yyyy-MM-dd HH:mm:ss"为预定义字符串。 String today = s.format(d);//结果如:2014-06-11 09:55:48 2. 使用parse()方法将文本转换为日期 String day = "2014年02月14日 10:30:20"; SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//“yyyy年MM月dd日 HH:mm:ss” 指定了字符串的日期格式,调用 parse() 方法将文本转换为日期。 Date date = s.parse(day);//结果如:Fri Feb 14 10:30:20 CST 2014 注意: 1、调用SimpleDateFormat对象的parse()方法时可能会出现转换异常,即ParseException,因此需要进行异常处理。 2、指定日期格式中的月MM和小时HH必须大写,小写结果会不同的。 3、使用Date 类时需要导入java.util包,使用SimpleDateFormat时需要导入java.text包。

 

6. 了解Calendar类

Java中日历类(Calendar类)的用途如下:Calendar类的静态方法getInstance()可以初始化一个日历对象:Calendar now = Calendar.getInstance();    

获取时间:

// 使用默认时区和语言环境获得一个日历

Calendar cal = Calendar.getInstance();

// 赋值时年月日时分秒常用的6个值,注意月份下标从0开始,所以取月份要+1

System.out.println("年:" + cal.get(Calendar.YEAR));

System.out.println("月:" + (cal.get(Calendar.MONTH) + 1));

System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH));

System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY));

System.out.println("分:" + cal.get(Calendar.MINUTE));

System.out.println("秒:" + cal.get(Calendar.SECOND));

设置时间:

Calendar cal = Calendar.getInstance();

cal.set(2018, 1, 15, 23, 59, 59); //年-月-日-时-分-秒

时间计算:

Calendar cal = Calendar.getInstance();

System.out.println(cal.getTime());

cal.set(2018, 1, 15, 23, 59, 59);

cal.add(Calendar.SECOND, 1);

System.out.println(cal.getTime());

 

7. 了解Math、Random类基本用法

Math:

位于java.lang包中,可直接使用

 

/**

* 测试Math类

* @author dxt

*

*/

public class TestMath {

public static void main(String[] args){

//1. 取整相关操作

System.out.println(Math.ceil(3.2));

System.out.println(Math.floor(3.2));

System.out.println(Math.round(3.2));

System.out.println(Math.round(3.8));

//2. 绝对值、开方、a的b次幂等操作

System.out.println(Math.abs(-45));

System.out.println(Math.sqrt(64));

System.out.println(Math.pow(5, 2));

//3. Math类中常用的常量

System.out.println(Math.PI);

System.out.println(Math.E);

//4. 随机数

System.out.println(Math.random());

}

}

Random:

专门用来生成随机数的类。位于java.util包中。

 

import java.util.Random;

 

/**

* 测试Random类的使用

* 专门用来生成随机数的类

* @author dxt

*

*/

public class TestRandom {

public static void main(String[] args){

Random rand = new Random();

//1. 随机生成[0,1)之间的double类型的随机数

System.out.println(rand.nextDouble());

//2. 随机生成int型允许范围内的整型数据

System.out.println(rand.nextInt());

//3. 随机生成[0,1)之间float型的数据

System.out.println(rand.nextFloat());

//4. 随机生成false或者true

System.out.println(rand.nextBoolean());

//5. 随机生成[0,10)之间的int类型的数据

System.out.println(rand.nextInt(10));

//6. 随机生成[20,30)之间的int类型的数据

System.out.println(20 + rand.nextInt(10));

//7. 随机生成[20,30)之间的int类型的数据

System.out.println(20 + (int)(rand.nextDouble() * 10));

}

}

 

8. 了解BigInteger类和BigDecimal类的基本用法

BigInteger:

在Java的整数类型里面,byte为8位,short为16位,int为32位,long为64位。正因为这些数值的二进制位数已经固定,所以它们能表示的数值大小就有一定的范围限制。因此,Java中提供BigInteger类来处理更大的数字。

方法:

BigInteger add(BigInteger val):加法运算。

BigInteger subtract(BigInteger val):减法运算。

BigInteger multiply(BigInteger val) :乘法运算。

BigInteger divide(BigInteger val) :除法运算,可能会产生除零异常。

public BigInteger[] divideAndRemainder(BigInteger val):获取商值和余数组成的数组,初始元素是商值,最终元素是余数。

String toString():将BigInteger对象的数值转换成字符串。

double doubleValue():将BigInteger对象中的值以双精度数返回。

float floatValue():将BigInteger对象中的值以单精度数返回。

long longValue():将BigInteger对象中的值以长整数返回。

int intValue():将BigInteger对象中的值以整数返回。

示例:

public void test() {

BigInteger a = new BigInteger("5");

BigInteger b = new BigInteger("1");

System.out.println("add >>> " + a.add(b));

System.out.println("subtract >>> " + a.subtract(b));

System.out.println("multiply >>> " + a.multiply(b));

System.out.println("divide >>> " + a.divide(b));

System.out.println("divideAndRemainder[0] >>> " + a.divideAndRemainder(b)[0]);

System.out.println("divideAndRemainder[1] >>> " + a.divideAndRemainder(b)[1]);

}

BigDecimal:

虽然Java的基本类型提供了float和double类型,但他们在执行浮点运算的时候,只是提供了一个较为精确的结果,不能用于要求精确度很高的环境中。

因此,Java提供了BigDecimal类来保证结果的精确度。BigDecimal都是不可变的(immutable)的,在进行每一步运算时,都会产生一个新的对象,所以在做运算时千万要保存操作后的值。使用BigDecimal的坏处是性能比double和float差,在处理庞大,复杂的运算时尤为明显,因根据实际需求决定使用哪种类型。

构造方法:

BigDecimal(int):创建一个具有参数所指定整数值的对象。

BigDecimal(double):创建一个具有参数所指定双精度值的对象(不建议使用)。

BigDecimal(String):创建一个具有参数所指定以字符串表示的数值的对象。

方法:

BigDecimal add(BigDecimal augend):加法运算。

BigDecimal subtract(BigDecimal subtrahend):减法运算。

BigDecimal multiply(BigDecimal multiplicand):乘法运算。

BigDecimal divide(BigDecimal divisor):除法运算,可能会产生除零异常和不能整除异常。

BigDecimal divide(BigDecimal divisor, int scale, int roundingMode):除法运算,可传入精确小数位数scale,和舍入模式roundingMode。

BigDecimal[] divideAndRemainder(BigDecimal divisor):获取商值和余数组成的数组,初始元素是商值,最终元素是余数。

BigDecimal setScale(int newScale, int roundingMode):进行舍入操作。

String toString():将BigDecimal对象的数值转换成字符串。

double doubleValue():将BigDecimal对象中的值以双精度数返回。

float floatValue():将BigDecimal对象中的值以单精度数返回。

int intValue():将BigDecimal对象中的值以整数返回。

示例:

public void test() {

BigDecimal a = new BigDecimal(10);

BigDecimal b = new BigDecimal(3);

System.out.println("add >>> " + a.add(b));

System.out.println("subtract >>> " + a.subtract(b));

System.out.println("multiply >>> " + a.multiply(b));

// System.out.println("divide >>> " + a.divide(b));// Non-terminating decimal expansion; no exact representable decimal result.

System.out.println("divide >>> " + a.divide(b, 2, BigDecimal.ROUND_HALF_UP));

System.out.println("divideAndRemainder[0] >>> " + a.divideAndRemainder(b)[0]);

System.out.println("divideAndRemainder[1] >>> " + a.divideAndRemainder(b)[1]);

}

 

9. 了解枚举的特点和使用

定义:

枚举的定义与类和常量定义非常类型。使用 enum 关键字替换 class 关键字,然后在 enum 中定义 “ 常量 ” 即可。

优点:

1.代码安全:规范了参数的形式,调用时类型取值范围确定,不用考虑类型的不匹配,显示的替代了int型参数可能带来的模糊概念,减少程序中因为类型引发的问题

2.耦合性低,扩展性高:便于增加类型,与原代码耦合性低;相比较于定义常量,扩展性更高;

3.编码专业性:合理的情况使用合理的方法

public class WeekTest {

public void getTime(int day){

if (day<0||day>7){

System.out.println("您输入的星期有误");

return;

}

switch(day){

case 1 :

case 2 :

case 3 :

case 4 :

case 5 :

System.out.println("今天是工作日,敲代码呀!!!");

break;

case 6:

case 7:

System.out.println("今天是周末,冲鸭,敲代码呀!!!");

break;

}

 

}

public static void main(String[] args) {

WeekTest weekTest = new WeekTest();

weekTest.getTime(7);

}

}

 

10. 掌握File类的使用

Java.IO.File类表示文件或目录,只用于表示文件或目录得信息,不能用于文件的访问。

常用的API:

1.创建File对象:File file=new File(String path);注意:File.seperater();获取系统分隔符,如:”\“.

2.boolean file.exists();是否存在.

3.file.mkdir();或者file.mkdirs();创建目录或多级目录。

4.file.isDirectory()或者file.isFile()判断是否是目录或者是否是文件。

5.file.delete();删除文件或目录。

6.file.createNewFile();创建新文件。

7.file.getName()获取文件名称或目录绝对路径。

8.file.getAbsolutePath()获取绝对路径。

9.file.getParent();获取父级绝对路径。

10.file.getSize();获取文件大小。

11.file.getFormat();获取文件格式名。

12.file.list() 返回的是 字符串数组 直接子的名称,不包含子目录下的内容

13.file.listFiles() 返回当前目录下的所有子目录和文件的文件数组名称

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值