包装类型,String,记得踩哦

包装类

java提供两种类型
1.基本数据类型
2.引用数据类型:
使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类

基本类型对应的包装类 (java.lang包)
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

装箱和拆箱

什么是装箱和拆箱? 基本数据类型和包装数据类型的来回转换的过程。
1.**装箱:**从基本类型转换为对应的包装类对象。
2.**拆箱:**从包装类对象转换为对应的基本类型。
举例

public class test0 {
    public static void main(String[] args) {
        //基本数值---->包装对象
        Integer i = new Integer(1); //第一种
        //输出为  1
        System.out.println(i);
        Integer integer = Integer.valueOf(4);//第二种
        //输出为 4
        System.out.println(integer);

        //包装对象---->基本数值
        int i2 = i.intValue();
        System.out.println(i2);
    }
}

自动装箱和拆箱

从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可
以自动完成

Integer i = 4;//自动装箱。相当于Integer i = Integer.valueOf(4); 
i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5;
//加法运算完成后,再次装箱,把基本数值转成对象。

String类

String类代表字符串
string特点

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

        String s="abc";
        s+="s";
        System.out.println(s);//输出为 abcs
        /**
         *  public final class String(string底层是final修饰的,final修饰的属性不能改变)
         *  也就是说String是常量,定义后值不能改变
         *
         *  为什么上面输出后结果变了呢?
         *  String的值存放在常量池里,现在常量池里有两个对象(abc和abcs)
         *  s从指向"abc",改变指向,指向了"abcd"。
         */

         String s1="abc";
         //相当于
        char data[] = {'a', 'b', 'c'};
        String str = new String(data);
        /**
         * 应为String底层是靠字符数组实现的。
         */
           }
}

String常用方法

public class test2 {
    public static void main(String[] args) {
        String s="abc";

        //length():获取字符串的长度,其实也就是字符个数
        System.out.println(s.length());//输出为  3

        //concat:将将指定的字符串连接到该字符串的末尾
        String s1="abc";
        String s2 = s1.concat("defg");
        System.out.println(s2);//输出为  abcdefg

        //charAt:获取指定索引处的字符
        String s3="1234";
        System.out.println(s3.charAt(0));//输出为 1
        System.out.println(s3.charAt(1));//输出为 2
        System.out.println(s3.charAt(2));//输出为 3
        System.out.println(s3.charAt(3));//输出为 4

        //substring:从?开始截取字符串到字符串结尾
        String s4="123456789";
        System.out.println(s4.substring(1));//输出为 23456789
        System.out.println(s4.substring(6));//输出为 789

         //substring:从?到?截取
        String s5="0123456";
        System.out.println(s5.substring(0,5));//输出为 01234
    }
}

String的转换工能

public class test3 {
    public static void main(String[] args) {
        String s="123";

        //toCharArray():把字符串转换为字符数组
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            System.out.println(c[i]);
            /**
             * 输出为
             * 1
             * 2
             * 3
             */

        }
        //getBytes():把字符串转换为字节数组
        byte[] bytes = s.getBytes();
        for(int x = 0; x < bytes.length; x++) {
            System.out.println(bytes[x]);//输出为 49  50  51
        }

        //replace():替换字母?为大写?
        String s1="abcdefg";
        String replace = s1.replace("defg", "DEFG");
        System.out.println(replace);//输出为 abcDEFG
    }
}

分割工能

public class test {
    public static void main(String[] args) {
        String s="11,22,33";
        String[] split = s.split(",");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
            /**
             *   输出为
             *   11
             *   22
             *   33
             */

        }
    }
}

统计字符个数

public class test4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个字符串");
        String s = scanner.nextLine();

        int a=0;
        int b=0;
        int c=0;
        for (int i = 0; i < s.length(); i++) {
            //获取输入的每一个字符
            char c1 = s.charAt(i);
            //获取大写字母个数量
            if (c1>='A'&&c1<='Z') {
                a++;

                //统计小写字母数量
            }else if (c1>='a'&&c1<='z'){
                b++;
                //统计数字数量
            }else if(c1>='0'&&c1<='9'){
                c++;
            }else {
                System.out.println("字符"+c1+"不合法");
            }

        }
        //输出结果
        System.out.println("大写字符:"+a+"个");
        System.out.println("小写字符:"+b+"个");
        System.out.println("数字字符:"+c+"个");
    }
}
请输入一个字符串
1g61w6g1we6g1QW56RFRQW61FQ6F
大写字符:10个
小写字符:6个
数字字符:12个

Process finished with exit code 0
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值