Java常用类

基本数据类型包装类

package datatype;
/**
 * 装箱:
 * 将基本类型自动转为包装类型
 * 底层默认调用valueOf()
 * 拆箱:
 * 将包装类型转为基本类型
 * 底层默认调用intValue()
 * */
public class IntegerDemo2 {
    /**
     * 当自动装箱时,默认调用Integer类中valueOf(),
     * 这个方法内部对 -128~+127 之间进行缓存(数组),
     * 在此区间自动装箱,不会创建新的Integer对象,
     * 直接从数组中获取,
     * 超出此区间,每次都会new新的Integer对象
     * */
    public static void main(String[] args) {
        //两种方法都是装箱
        Integer a = 19;
        Integer b = Integer.valueOf(19);//true
        System.out.println(a == b);
        Integer c = 129;
        Integer d = 129;
        System.out.println(c == d);//false
        //拆箱
        int i = a;
        System.out.println(i);
    }
}

String类

package stringdemo;
/**
 * java.lang.String  代表字符串
 * Java中凡是“ ”括起来的都是String类的实例(对象)
 * 字符串的值是不可改变的,一旦字符串对象被创建,值便不能改变
 *   底层存储字符串数组,是被final修饰的:private final char value[];
 *   必须在对象创建之初由构造方法对其赋值
 * */
public class StringDemo1 {
    public static void main(String[] args) {
        String s = "abc";
        s += "qwertyuiop";
        s += "asdfghjkl";    //每次+=,其实都是创建了一个新字符串对象
        System.out.println(s);
    }
}
package stringdemo;
/**
 * Java中创建字符串的两种方式:
 * 方式一:(一旦出现要创建的字符串对象内容与之前一致,返回拿到的是同一地址)
 *       String s1 = "abc";
 *       String s2 = "abc";
 *       在每一次创建变量s1时,会去内存中一个名为字符常量池的空间检索是否存在一个此内容的字符串对象,如果没有,便会在池中创建一个对象,并把地址赋给s1,
 *       在第二次创建s2时,也会去字符常量池检索是否存在一个此内容的字符串对象,如果存在,直接将之间创建过的地址赋给s2
 * 方式二:(无论是否存在相同内容的字符串对象,都会创建一个新的对象)
 * */
public class StringDemo2 {
    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = "abc";
        System.out.println(s1 == s2);  //true
        System.out.println(s1.equals(s2));  //true

        String s3 = new String("qwe");
        String s4 = new String("qwe");
        System.out.println(s3 ==s4);  //false
        System.out.println(s3.equals(s4));  //true
    }
}
package stringdemo;

import java.util.Arrays;

/**
 * String 构造方法
 * String();
 * String("abc");
 * String(byte[] b);
 * String(char[] c);
 * */
public class StringDemo3 {
    public static void main(String[] args) {
        String s1 = new String();
        String s2 = new String("abc");
        System.out.println(s1);
        System.out.println(s2);
        String s3 = new String("abc你好");
        byte[] bytes = s3.getBytes();  //把字符串转型成byte类型   转码
        System.out.println(Arrays.toString(bytes));
        String s4 = new String(bytes);  //从byte类型转回String类型  解码
        System.out.println(s4);

        String s5 = new String("cab你好");
        char[] chars = s5.toCharArray();  //字符串——>数组
        Arrays.sort(chars);
        String s6 = new String(chars);  //数组——>字符串
        System.out.println(s6);
    }
}
package stringdemo;
/**
 * 判断功能
 * */
public class StringDemo4 {
    public static void main(String[] args) {
        String s = "abcd";
        System.out.println(s.equals("abc"));  //false
        System.out.println(s.equalsIgnoreCase("abCD"));  //true  忽略大小写
        System.out.println(s.contains("ab"));  //判断是否包含指定子串(子串必须是连续的)
        System.out.println(s.contains("ac"));
        System.out.println(s.isEmpty());  //判断是否是空串  空串!=null   null是关键字,“ ”是空串
        System.out.println(s.startsWith("a"));  //是否以指定子串开头
        System.out.println(s.endsWith("c"));  //是否以指定子串结尾
    }
}
package stringdemo;

/**
 * 获取功能
 * */
public class StringDemo5 {
    public static void main(String[] args) {
        String s = "abcdcde";
        System.out.println(s.length());  //获取长度
        System.out.println(s.charAt(3));  //c  通过位置(索引)查找字符
        System.out.println(s.indexOf("d"));  //返回指定字符首次出现的位置
        System.out.println(s.indexOf("ac"));  //返回首字母首次出现的位置
        System.out.println(s.indexOf("c",0));  //从指定位置开始查找
        System.out.println(s.lastIndexOf("a"));  //从后向前查找

        String s1 = s.substring(2);    //从指定位置开始截取字符串,返回一个新的子字符串
        System.out.println(s1);
        String s2 = s.substring(0,3);  //从开始位置到结束位置截取字符串,返回一个新的子字符串
        System.out.println(s2);
    }
}
package stringdemo;

import java.util.Arrays;
/**
 * 转换功能
 * */
public class StringDemo6 {
    public static void main(String[] args) {
        Integer a = 10;
        String s = String.valueOf(a);  //将其他类型转换为字符串类型,建议使用,避免出现空指针异常
        System.out.println(s);

        String s2 = "abcDEF你好";
        System.out.println(s2.toLowerCase());  //转小写
        System.out.println(s2.toUpperCase());  //转大写

        String s3 = s2.concat("www");
        System.out.println(s3);  //将指定字符串拼接到字符串末尾,返回一个新的字符串

        String s4 = "ab;cde;fg";
        String[] strings = s4.split(";");
        System.out.println(Arrays.toString(strings));  //使用指定的字符将字符串分隔为数组,正则表达式
    }
}
package stringdemo;
/**
 * 替换功能
 * */
public class StringDemo7 {
    public static void main(String[] args) {
        String s = " abc d efgd ";
        System.out.println(s.replace("a","w"));
        System.out.println(s.replace("ab","yz"));
        System.out.println(s.replaceAll(" ", "DD"));  //使用正则表达匹配需要替换的内容
        System.out.println(s.replaceFirst("d","p" ));  //替换第一个出现字符
        System.out.println(s.length());
        System.out.println(s.trim().length());  //去除字符串头尾空白符
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值