Java基础知识笔记总结之包装类(三)

本文目标 : Java包装类:

  • 1.理解String类,以及字符不变性
  • 2.熟练使用StringBuffer类
  • 3.熟练使用Random类
  • 4.掌握Date类
  • 5.熟练使用Calendar类
  • 6.掌握SimpleDateFormat类用法
  • 7.了解Math最终类

// 1.理解String类,以及字符不变性
public static void main(String[] args) {
// integer是int类型的基本包装类型
Integer obj = new Integer("-0010");
System.out.println(obj);
// -10  把被字符串包裹的数字转换为整型数字
  1. 对比数字大小 compareTo
// Integer i = new Integer(99);
Integer i = 99;
System.out.println("i.compareTo:"+ i.compareTo(99)); 
// 输出:0 表示相等
System.out.println("i.compareTo:"+ i.compareTo(100)); 
// 输出:-1 表示小于100
System.out.println("i.compareTo:"+ i.compareTo(98)); 
// 输出:1 表示大于98
  1. 返回一个整数的16进制形式字符串
int num = 54321;
System.out.println("Integer.toString(int i, int radix):" + Integer.toString(i,16));
 // 输出结果: Integer.toString(int i, int radix):63

3. 总结了几个方法,附带小案例可以帮助更好的记忆包里提供的方法

☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯

  • 1.通过字符串的下标,返回对应字符
  • 2.字符串拼接
  • 3.找字符下标
  • 4.字符串长度
  • 5.字符串替换
  • 6.截取字符串,从输入的下标开始截取
  • 7.截取下标 0-2 的字符
  • 8.转换为小写
  • 9.转换为大写
    ☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯☯
  // 3. Character类在对象中包装一个基本类型 char 的值
        String result = "abcD";
        System.out.println(result.charAt(3));// 输出:D  通过字符串的下标,返回对应字符
        System.out.println(result.concat("sos"));// 输出: abcDsos 字符串拼接
        System.out.println(result.indexOf("b")); // 输出: 1 找字符下标
        System.out.println(result.length()); // 输出:4 字符串长度
        System.out.println(result.replace('a','b')); // 输出: bbcD 字符串替换
        System.out.println(result.substring(1));// 输出:bcD 截取字符串,从输入的下标开始截取
        System.out.println(result.substring(0,2));// 输出: ab 截取下标 0-2 的字符
        System.out.println(result.toLowerCase());// 输出:abcd 转换为小写
        System.out.println(result.toUpperCase());// 输出:ABCD 转换为大写 
        

拓展案例 : 分别输出字符串中的大小写字母:

public class UpperLowerCase {
    public static void main(String []args) {
        String StrA="I am Tom.I am from China.";
        String StrB="";
        String StrC="";
        for(int i=0;i<StrA.length();i++){
            if(Character.isUpperCase(StrA.charAt(i)))
                StrB +=StrA.charAt(i)+"  ";
            if(Character.isLowerCase(StrA.charAt(i)))
                StrC +=StrA.charAt(i)+"  ";
            }
        System.out.println("字符串中大写字母有:"+StrB);
        System.out.println("字符串中小写字母有:"+StrC);
    }
}

输出:
字符串中大写字母有:I T I C
字符串中小写字母有:a m o m a m f r o m h i n a

4. 随机数Random方法 : random() 方法用于返回一个随机数,随机数范围为 0.0 =< Math.random < 1.0。

注意: 左闭右开 ==> 使用的时候看情况要不要加一, 这样才能取到右边的值

public class Test{
    public static void main(String args[]){
        System.out.println( Math.random() );
        System.out.println( Math.random() );
        
        // 输出: 
        // 0.9982527938660647
        // 0.7777067597597204
    }
}
  • 新建实例对象,
Random b = new Random();
System.out.println(b.nextInt(10)); //获取10以内的整数随机数

5. Math运算类:
Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数

public class Test {  
    public static void main (String []args)  
    {  
        System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2));  
        System.out.println("0度的余弦值:" + Math.cos(0));  
        System.out.println("60度的正切值:" + Math.tan(Math.PI/3));  
        System.out.println("1的反正切值: " + Math.atan(1));  
        System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));  
        System.out.println(Math.PI);  
    }  
}

输出值:
在这里插入图片描述

6. Math 的 floor,round 和 ceil 方法实例比较(常用)

public class Test {   
  public static void main(String[] args) {   
    double[] nums = { 1.4, 1.5, 1.6, -1.4, -1.5, -1.6 };   
    for (double num : nums) {   
      test(num);   
    }   
  }     
  private static void test(double num) {   
    System.out.println("Math.floor(" + num + ")=" + Math.floor(num));   
    System.out.println("Math.round(" + num + ")=" + Math.round(num));   
    System.out.println("Math.ceil(" + num + ")=" + Math.ceil(num));   
  }   
}

输出结果:
Math.floor(1.4)= 1.0 向下取整
Math.round(1.4)= 1 四舍五入
Math.ceil(1.4)= 2.0 向上取整

Math.floor(1.5)= 1.0 同上
Math.round(1.5)= 2
Math.ceil(1.5)= 2.0

Math.floor(1.6)= 1.0
Math.round(1.6)= 2
Math.ceil(1.6)= 2.0

Math.floor(-1.4)= -2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0

Math.floor(-1.5)= -2.0
Math.round(-1.5)= -1
Math.ceil(-1.5)= -1.0

Math.floor(-1.6)= -2.0
Math.round(-1.6)= -2
Math.ceil(-1.6)= -1.0

总结:

  • Math.floor 是向下取整。
  • Math.ceil 是向上取整。
  • Math.round 是四舍五入取整

但是需要注意的是:

  1. 参数的小数点后第一位小于 5,运算结果为参数整数部分。
  2. 参数的小数点后第一位大于 5,运算结果为参数整数部分绝对值 +1,符号(即正负)不变。
  3. 参数的小数点后第一位等于 5,正数运算结果为整数部分 +1,负数运算结果为整数部分。

7. 1两个非 new 生成的 Integer 对象进行比较,如果两个变量的值在区间 [-128,127] 之间,比较结果为 true;否则,结果为 false。

// 比较两个非new生成的Integer变量
public class Test {
    public static void main(String[] args) {
        Integer i1 = 127;
        Integer ji = 127;
        System.out.println(i1 == ji);//输出:true
        Integer i2 = 128;
        Integer j2 = 128;
        System.out.println(i2 == j2);//输出:false
    }
}

7.2 Integer 变量(无论是否是 new 生成的)与 int 变量比较,只要两个变量的值是相等的,结果都为 true。

// 比较Integer变量与int变量
public class Test {
    public static void main(String[] args) {
        Integer i1 = 200;
        Integer i2 = new Integer(200);
        int j = 200;
        System.out.println(i1 == j);//输出:true
        System.out.println(i2 == j);//输出:true
    }
}

忠告1:
如果寒暄只是打个招呼就了事的话,
那与猴子的呼叫声有什么不同呢?
事实上,
正确的寒暄必须在短短一句话中明显地表露出你对他的关怀。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值