Java基础之常用类库---数字与字符串

所有的基本类型,都有对应的类类型

byte类型封装类Byte,short类型封装类Short,int类型封装类Integer,long类型封装类Long,char类型封装类Character,float类型封装类Float,double类型封装类Double

public class TestNumber {

    public static void main(String[] args) {

        int i = 5;

        //把一个基本类型的变量,转换为Integer对象

        Integer it = new Integer(i);

        //把一个Integer对象,转换为一个基本类型的int

        int i2 = it.intValue();

    }

}

装箱和拆箱

自动装箱:不需要调用构造方法,通过=符号自动把 基本类型 转换为 类类型 就叫装箱

public class TestNumber {

    public static void main(String[] args) {

        int i = 5;

        //基本类型转换成封装类型

        Integer it = new Integer(i);

        //自动转换就叫装箱

        Integer it2 = i;

    }

}

自动拆箱:不需要调用Integer的intValue方法,通过=就自动转换成int类型,就叫拆箱

public class TestNumber {

    public static void main(String[] args) {

        int i = 5;

        Integer it = new Integer(i);

        //封装类型转换成基本类型

        int i2 = it.intValue();

        //自动转换就叫拆箱

        int i3 = it;

    }

}

int的最大值,最小值可以通过其对应的封装类Integer.MAX_VALUE和Integer.MIN_VALUE

数字转字符串:

方法1: 使用String类的静态方法valueOf 

方法2: 先把基本类型装箱为对象,然后调用对象的toString

public class TestNumber {

    public static void main(String[] args) {

        int i = 5;

        //方法1

        String str = String.valueOf(i);

        //方法2

        Integer it = i;

        String str2 = it.toString();

    }

}

字符串转数字:调用Integer的静态方法parseInt

public class TestNumber {

    public static void main(String[] args) {

        String str = "999";

        int i= Integer.parseInt(str);

        System.out.println(i);

    }

}

数学方法:java.lang.Math提供了一些常用的数学运算方法,并且都是以静态方法的形式存在

public class TestNumber {

    public static void main(String[] args) {

        float f1 = 5.4f;

        float f2 = 5.5f;

        //5.4四舍五入即5

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

        //5.5四舍五入即6

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

        //得到一个0-1之间的随机浮点数(取不到1)

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

        //得到一个0-10之间的随机整数 (取不到10)

        System.out.println((int)( Math.random()*10));

        //开方

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

        //次方(2的4次方)

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

        //π

        System.out.println(Math.PI);

        //自然常数

        System.out.println(Math.E);

    }

}

 格式化输出:如果不使用格式化输出,就需要进行字符串连接,如果变量比较多,拼接就会显得繁琐

使用格式化输出,就可以简洁明了,知道了解就行。

%s 表示字符串

%d 表示数字

%n 表示换行

public class TestNumber {

    public static void main(String[] args) {

        String name ="盖伦";

        int kill = 8;

        String title="超神";

        //直接使用+进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差

        String sentence = name+ " 在进行了连续 " + kill + " 次击杀后,获得了 " + title +" 的称号";

        System.out.println(sentence);

        //使用格式化输出

        //%s表示字符串,%d表示数字,%n表示换行

        String sentenceFormat ="%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n";

        System.out.printf(sentenceFormat,name,kill,title);

    }

}

字符串:字符串即字符的组合,在Java中,字符串是一个类,所以我们见到的字符串都是对象 ,String 被修饰为final,所以是不能被继承的。

常见创建字符串手段: 

1. 每当有一个字面值出现的时候,虚拟机就会创建一个字符串 

2. 调用String的构造方法创建一个字符串对象 

3. 通过+加号进行字符串拼接也会创建新的字符串对象 

public class TestString {

    public static void main(String[] args) {

        String garen ="盖伦"; //字面值,虚拟机碰到字面值就会创建一个字符串对象

        String teemo = new String("提莫"); //创建了两个字符串对象

        char[] cs = new char[]{'崔','斯','特'};

        String hero = new String(cs);//  通过字符数组创建一个字符串对象

        String hero3 = garen + teemo;//  通过+加号进行字符串拼接

    }

}

字符串常用方法:

charAt    获取字符

toCharArray    获取对应的字符数组

subString    截取子字符串

split    分隔

trim    去掉首尾空格

toLowerCase    全部变成小写 

toUpperCase    全部变成大写

indexOf    判断字符或者子字符串出现的位置

lastIndexOf    判断字符或者子字符串最后出现的位置

contains    是否包含子字符串

replaceAll    替换所有的

replaceFirst    只替换第一个

判断字符串对象是否相等用==,判断字符串内容是否相等用equals()

public class TestString {

    public static void main(String[] args) {

        String str1 = "the light";

        String str2 = new String(str1);

        String str3 = str1.toUpperCase();

        //==用于判断是否是同一个字符串对象

        System.out.println( str1  ==  str2);

        System.out.println(str1.equals(str2));//完全一样返回true

        System.out.println(str1.equals(str3));//大小写不一样,返回false

        System.out.println(str1.equalsIgnoreCase(str3));//忽略大小写的比较,返回true

    }

}

startsWith //以...开始,endsWith //以...结束

public class TestString {

    public static void main(String[] args) {

        String str1 = "the light";

        String start = "the";

        String end = "Ight";

        System.out.println(str1.startsWith(start));//以...开始

        System.out.println(str1.endsWith(end));//以...结束

    }

}

StringBuffer:StringBuffer是可变长的字符串

StringBuffer的常用方法:append追加 ,delete 删除 ,insert 插入 ,reverse 反转

public class TestString {

    public static void main(String[] args) {

        String str1 = "let there ";

        StringBuffer sb = new StringBuffer(str1); //根据str1创建一个StringBuffer对象

        sb.append("be light"); //在最后追加

        System.out.println(sb);

        sb.delete(4, 10);//删除4-10之间的字符

        System.out.println(sb);

        sb.insert(4, "there ");//在4这个位置插入 there

        System.out.println(sb);

        sb.reverse(); //反转

        System.out.println(sb);

    }

}

StringBuild:StringBuild也是可变的字符串长度

大部分情况,StringBuild和StringBuffer方法相同,通过new新建StringBuilder,通过append添加字符串,然后通过toString获取构建完成的字符串。

StringBuilder sb = new StringBuilder();//创建StringBuild

sb.append(",探索编程本质");//添加字符串

System.out.println(sb.toString());//获取构建后的字符串,通过toString方法

经典面试题:String,StringBuffer,StringBuild有什么区别?(曾经就被阿里的电面问了这个,那个时候还是个小白,没答出来)

String、StringBuffer与StringBuilder之间区别

个人觉得这个博客讲得挺明了,初学者都能懂!

你可能感兴趣的:(Java基础之常用类库---数字与字符串)