java note 6 常用类

目录

 

字符串相关

时间相关

其它


学习代码
https://github.com/starrQWQ/java_code/tree/master/javaSE基础

字符串相关

122-124 String类
public final class String  ——底层类型
private final char value[]
不可继承,不可变。

string s1 = "abc";
s1 = "def"; 
s1没有final,编译通过。

使用缓存提升字符串访问效率
java所有双引号括起来的字符串都在方法区的字符串常量池中创建一份。

string s1 = "abc";
string s2 = "abc";  //从常量池中拿来用
这时s1==s2 

注意:比较字符串不能用“==”。

string s3 =new String("abc");
string s4 =new String("abc");
s3!=s4.
s4.equals(s5);//true

string s6 = "abc";
string s7 = "def";
string s8 = s6+s7;
在常量池创建了3个对象。
所以不要用“+”做频繁连接,否则给垃圾回收gc带来压力。

string s1 = "abc";
string s2 = new String("abc");
推荐使用第一种。因为第二种在方法区和堆区各创建了一个对象,浪费内存。

string s1 = new String("abc");
string s2 = new String("abc");
共创建了3个对象。方法区常量池1个,堆区2个。


125创建字符串
查询api


126-127常用String方法
Object o = null;
System.out.println(o);     //String.valueOf(o)
System.out.println(o.toString()); //空指针异常。

 

128 re
'^'开始,'$'结束
\d    数字
\D    非数字
\w    英文字母
\W    非英文字母


129-130StringBuffer和StringBuilder
java.lang.StringBuffer
java.lang.StringBuilder
两个都是字符串缓冲区
原理:预先向内存申请一块空间容纳字符序列,不足则自动扩容。

和String区别:
String不可变,存在常量池中。
StringBuffer底层是无final的char数组,可变,可自动扩容。

两者默认初始化容量是16。super(16)
(look up the api constructor)


如何优化StringBuffer和StringBuilder?
扩容时底层数组拷贝很低效。
预测存储字符容量,指定初始化容量,减少底层数组拷贝,提高效率。

字符串频繁拼接要使用StringBuffer和StringBuilder。

StringBuffer和StringBuilder区别
StringBuffer是线程安全的
StringBuilder线程非安全

131-132包装类型
8中基本类型对应的包装类型

byte    java.lang.Byte;
short    java.lang.Short;
int    java.lang.Integer;
long    java.lang.Long;

float    java.lang.Float
double    java.lang.Double

boolean    java.lang.Boolean;

char    java.lang.Character;

想让m1()接受任何一种数据类型
注意:Object是引用类型,不能接受基本类型。

public class T{
    public static void main(String[] args){
        byte i = 1;
        Byte b = new Byte(i);
        m1(b);
    }

    public static void m1(Object o){
        System.out.println(o);
    }
}

 

Byte将toString()重写。

java.math.BigDecimal

133-134 Integer类

public class IntegerTest{
    public static void main(String[] args){
        System.out.println("min: "+Integer.MIN_VALUE);
        System.out.println("max: "+Integer.MAX_VALUE);

        /*
        Integer i = new Integer("a12");
        System.out.println(i);
        */

        Integer i1 = new Integer("123");
        int i2 = i1.intValue();
        System.out.println("i2: "+i2);

        int i3 = Integer.parseInt("25");
        System.out.println(i3);

        double d1 = Double.parseDouble("3.5");
        System.out.println(d1+1);

    }
}

查询Integer类型api构造方法。
熟悉int,Integer,String的类型转化
重要:
parseInt()
toHexString()
toBinaryString()
toOctalString()
valueOf()

/*
    int
    Integer
    String
*/

public class TypeChange{
    public static void main(String[] args){
        //int-->Integer
        Integer inte1 = Integer.valueOf(1);

        //Integer-->int
        int i1 = inte1.intValue();

        //int-->String
        String s1 = i1+"";

        //String-->int
        int i3 = Integer.parseInt("123");

        //Integer-->String
        String s2 = inte1.toString();

        //String-->Integer
        Integer inte2 = Integer.valueOf("2");

    }
}


135-136自动装箱/自动拆箱
JDK5.0特性:auto-boxing/unboxing

在这之前
    //int-->Integer(boxing)
    Integer inte1 = new Integer(1);

    //Integer-->int(unboxing)
    int i1 = inte1.intValue();

JDK5.0后
    //auto-boxing
    Integer inte2 = 1;
    //auto-unboxing
    int i2 = inte2;

public static void m1(Object o){
    //auto-boxing
    System.out.println(o);
}

public static void m2(Integer inte1,Integer inte2){
    //理论两个引用类型不能运算
    //auto-unboxing
    return inte1 + inte2;
}


自动装箱/自动拆箱是编译阶段概念,与程序运行无关,目的是方便编码。

public class T{
    public static void main(String[] args){
        Integer inte1 = new Integer(1);
        Integer inte2 = new Integer(1);

        //不会自动拆箱
        System.out.println(inte1 == inte2);  //false

        Integer inte3 = 128;
        Integer inte4 = 128;
        //相当于new Integer();
        System.out.println(inte3 == inte4);//false

        Integer inte5 = 127;
        Integer inte6 = 127;
        System.out.println(inte5 == inte6);//true
        //java在方法区引入整型常量池,存储-128-127,该范围不会在堆中创建对象。
    }
}

 

 

时间相关

137获取当前系统时间的毫秒数
java.lang.System.currentTimeMillis()
自1970年1月1日0点到现在的毫秒数。

public class Ms{
    public static void main(String[] args){
        long now = System.currentTimeMillis();

        System.out.println(now);
    }
}

138 SimpleDateFormat格式化日期

SimpleDateFormat + java.text.DateFormat.format():
    Date类型——>String类型

import java.util.Date;
import java.text.SimpleDateFormat;

public class GetTime{
    public static void main(String[] args){
        Date now1 = new Date();
        Date now2 = new Date(System.currentTimeMillis());
        System.out.println(now1);
        System.out.println(now2);

        SimpleDateFormat sdf = new SimpleDateFormat("hh 'o''clock' a, zzzz");

        String strTime = sdf.format(now1);
        System.out.println(strTime);
    }
}

139 SimpleDateFormat解析字符串

SimpleDateFormat + java.text.DateFormat.parse():
    String类型——>Date类型

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;  //!!!!!!!!!!!!!!!!!!!!!!!!

public class Str2Date{
    public static void main(String[] args) throws ParseException{
        String strTime = "2008.08.08 08:08:08";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

        Date now = null;
        now = sdf.parse(strTime);

        /*
        try{
            now = sdf.parse(strTime);
        }catch(ParseException e){
            e.printStackTrace();
        }
        */
        

        System.out.println(now);
    }
}


140当前时间的前十分钟

import java.util.Date;
import java.text.*;

public class PreXMin{
    public static void main(String[] args){
        
        Date d1 = new Date(System.currentTimeMillis());
        Date d2 = new Date(System.currentTimeMillis()-1000*60*10);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss zzzz");
        System.out.println(sdf.format(d1));
        System.out.println(sdf.format(d2));
    }
}

141日历Calendar

142 DecimalFormat
java.text.DecimalFormat

    #    任意数字
    ,    千分位
    .    小数点
    0    不够补0

import java.text.DecimalFormat;

public class DecimalFormatText{
    public static void main(String[] args){
        DecimalFormat df1 = new DecimalFormat("###,###.##");
        System.out.println(df1.format(1111111.123));

        DecimalFormat df2 = new DecimalFormat("###,###.0000");
        System.out.println(df2.format(1111111.123));
    }
}

其它

143 BigDecimal
java.math.BigDecimal
数据精确度高,适合做财务软件。
double类型精确度太低。

import java.math.BigDecimal;

public class BigDecimalText{
    public static void main(String[] args){
        BigDecimal bd1 = new BigDecimal(10);
        BigDecimal bd2 = new BigDecimal(3);

        BigDecimal bd3 = bd1.add(bd2);
        System.out.println(bd3);
    }
}

144 Random

import java.util.Random;

public class RandomText{
    public static void main(String[] args){

         Random r = new Random();

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

    }
}


145 enum

/*
    divide():if sucess,return 1,else 0. 
*/

public class EnumText1{
    public static void main(String[] args){
        int retValue = divide(10,1);
        System.out.println(retValue);
    }

    public static int divide(int a,int b){
        try{
            int c = a/b;
            return 1;
        }catch(Exception e){
            return 0;
        }
    }
}

上面这样写有风险,divide返回的值范围太广泛,可以通过枚举类型改进。
能在编译时解决的问题就不要留在运行期。


/*
    divide():if sucess,return 1,else 0. 
*/

public class EnumText2{
    public static void main(String[] args){
        Result retValue = divide(10,1);
        System.out.println(retValue);
    }

    public static Result divide(int a,int b){
        try{
            int c = a/b;
            return Result.SUCCESS;
        }catch(Exception e){
            return Result.FAIL;
        }
    }
}

enum Result{
    SUCCESS,FAIL
}


枚举的要求是元素有限,如颜色,一周,四季。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值