java 常用类

包装类与基本数据类型

package com.mashibing;

* 包装类与基本数据类型
*   包装类是将基本数据类型封装成一个类,包含属性和方法
*   使用:
*       在使用过程中,会涉及到自动装箱和自动拆箱
*       装箱:将基本数据类型转换成包装类
*       拆箱:将包装类转换成基本数据类型
*
*
*
*
* */
public class IntegerDemo {
    public static void main(String[] args) {
//        int a = 10;
//        Integer i = new Integer(10);
//        //通过方法进行类型的转换
//        Integer i2 = Integer.valueOf(a);
//        int i3 = i.intValue();
//        System.out.println(a == i);
//        Float f1 = new Float(3.14);
//        Double d1 = new Double(3.14);
//        int i =100;
//        Integer i1 = 100;
//        Integer i2 = 100;
//        Integer i3 = 200;
//        Integer i4 = 200;
//        System.out.println(i1==i2);
//        System.out.println(i3==i4);

//        Double d1 = 1.0;
//        Double d2 = 1.0;
//        Double d3 = 2.0;
//        Double d4 = 2.0;
//        System.out.println(d1==d2);
//        System.out.println(d3==d4);

        Integer i = 10;
        int a = i;
        System.out.println(a==i);
    }
}

字符串

package com.mashibing;

* 注意:常量池在1.7之后放置在了堆空间之中
*       字符串的使用:
*           1、创建
*               String str = "abc";
*               String str2 = new String("abc");
*               两种方式都可以用,只不过第一种使用比较多
*           2、字符串的本质
*               字符串的本质是字符数组或者叫做字符序列
*               String类使用final修饰,不可以被继承
*               使用equals方法比较的是字符数组的每一个位置的值
*               String是一个不可变对象
*
* */
public class StringDemo {
    public static void main(String[] args) {
        String str = "abc";
        String str2 = new String("abc");
//        str2 = str2.intern();
        System.out.println(str==str2);
        System.out.println(str.equals(str2));
        System.out.println(str.charAt(0));
        //数组的复制过程
        System.out.println(str.concat("cde"));
        //返回指定下标的元素
        System.out.println(str.indexOf("a"));
        String s = "abcdefghijklmn";
        System.out.println(s.substring(3));
        //在截取字符串的时候,需要注意是左闭右开区间
        System.out.println(s.substring(3,5));
        System.out.println(s.length());
        System.out.println("-----------------");
//        String a = "abc";
//        String b = new String("abc");
//        b = b.intern();
//        System.out.println(a==b);

        String a = "abc";
        String b = "def";
        String c = "abcdef";
        String d = (a+b).intern();// 首先检查字符串池中是否有abcdef,如果有,返回这个字符串的引用
        String e = "abc"+"def";
        System.out.println(c==d);
        System.out.println(c==e);
        String f = "a" + "b" +"c";
        String a1 = "a";
        String a2 = "b";
        String a3 = "c";
        String f1 = a1+a2+a3;


    }
}

StringBuffer和StringBuilder

package com.mashibing;

* 可变字符串
*   StringBuffer:线程安全,效率低
*   StringBuilder: 线程不安全,效率高
*   StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
*
* */
public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(1).append(1.234).append("abc").append(true);
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.length());
        System.out.println(stringBuffer.capacity());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("123").append(1).append(false);
        System.out.println(stringBuilder);
    }
}

Date

package com.mashibing;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateDemo {
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        System.out.println(date);
        System.out.println(date.getTime());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将Date类按照规范转换为字符串格式
        String str = dateFormat.format(date);
        System.out.println(str);
        //将字符串转换成对应的日期类
        Date d1 = dateFormat.parse("2010-10-10 20:20:20");
        System.out.println(d1);

        //获取的是当前系统的时间
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);
        //设置指定时间的日历类
        calendar.setTime(d1);
        System.out.println(calendar);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));

    }
}
原文地址:https://www.cnblogs.com/hulian425/p/14332798.html