实用类的简单使用

本文详细介绍了Java中的包装类,包括它们的作用、构造方法、常用方法以及装箱和拆箱的概念。此外,还讨论了字符串的操作,如连接、提取方法,以及Date和Calendar类在处理日期时间方面的应用。最后提到了提高字符串操作效率的StringBuffer类。
摘要由CSDN通过智能技术生成

包装类

包装类把基本类型数据转换为对象:每个基本类型在java.lang包中都有一个相应的包装类。

包装类的作用

1.提供了一系列实用的方法

2.集合不允许存放基本数据类型数据,存放数字时,要用包装类型

包装类的构造方法

包装类的构造方法的使用

1.所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例

public Type(type value)如:Integer i=new Integer(1);

2.除Character类外,其他包装类可将一个字符串作为参数构造它们的实例

public Type(String value)如:Integer i=new Integer("123");

注意事项:

Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false

当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常

包装类的常用方法

1.XXXValue():包装类转换成基本类型

byteValue()、intValue()longValue()、shortValue()doubleValue()、floatValue()charValue()、booleanValue()

    //byte by = 12;
    //Byte byte1 = new Byte(by);
    Byte byte1 = new Byte("12");
    byte num1 = byte1.byteValue();
    System.out.println(num1);
        
    Boolean boolean1 = new Boolean("True");
    boolean num2 = boolean1.booleanValue();
    System.out.println(num2);
        
    Character character = new Character('a');
    char num3 = character.charValue();
    System.out.println(num3);
​

2.toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)

    byte num4 = 10;
    String str1 = Byte.toString(num4);
    System.out.println(str1);
        
    String str2 = Boolean.toString(false);
    System.out.println(str2);

3.parseXXX():把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)

    int num5 = Integer.parseInt("123");
    System.out.println(num5);
            
    boolean num6 = Boolean.parseBoolean("qwe");

4.valueOf()

1.所有包装类都有如下方法(基本类型->包装类)

public static Type valueOf(type value)

如:Integer intValue = Integer.valueOf(21);

2.除Character类外,其他包装类都有如下方法(字符串->包装类)

public static Type valueOf(String s)

如:Integer intValue = Integer.valueOf("21");

装箱和拆箱

实现的是基本数据类型和包装类类型之间的自动转换

装箱:基本类型转换为包装类的对象

拆箱:包装类对象转换为基本类型的值

装箱:

基本数据类型直接赋值给包装类类型的对象

    byte num1 = 10;
    Byte byte1 = num1;

拆箱:

包装类类型数据直接赋值给基本类型的变量

    Integer int1 = new Integer("123");
    int num2 = int1;

包装类的特点

1.JDK1.5后,允许基本数据类型和包装类型进行混合数学运算

2.包装类并不是用来取代基本数据类型的,在基本数据类型需要用对象表示时使用

Math类

Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

double pi = Math.PI;
        System.out.println(pi);
        
        System.out.println(Math.max(100, 200));
        System.out.println(Math.min(100, 200));
        System.out.println(Math.abs(-7.9));
        
        System.out.println(Math.floor(9.4));//返回最大的(最接近正无穷大) double 值,该值小于等于参数,并等于某个整数。
        System.out.println(Math.ceil(-9.9));//返回最小的(最接近负无穷大) double值,该值大于等于参数,并等于某个整数。
        System.out.println(Math.round(10.6));//返回最接近参数的int值
        
        //随机数方法:Math.random():随机返回一个介于0.0(包括)~1.0(不包括)之间的double类型的数据
​
        double num1 = Math.random();
        System.out.println(num1);
        /*随机返回一个[num1 , num2)之间的int类型的整数( num2>num1)
        *int num = (int)(Math.random()*(num2-num1 )+num1 ) ;*/

Random类

生成随机数的其他方式

包:java.util.Random

//创建Random类对象
        
        Random random = new Random();
        int num1 = random.nextInt();
        System.out.println(num1);
        System.out.println(random.nextBoolean());
        
        //创建Random类对象
        Random random2 = new Random(10);
        System.out.println(random2.nextInt());
        
        //创建Random类对象
        Random random3 = new Random();
        System.out.println(random3.nextInt(10));
        
        //用同一个种子值来初始化两个Random 对象,然后用每个对象调用相同的方法,得到的随机数也是相同的
        Random r1 = new Random(-100);
        Random r2 = new Random(-100);
        System.out.println(r1.nextBoolean());
        System.out.println(r2.nextBoolean());

String类

使用String对象存储字符串

String s = "Hello World";
String s = new String();
String s = new String("Hello World");

String类位于java.lang包中,具有丰富的方法,如;计算字符串的长度、比较字符串、连接字符串、提取字符串

length()方法

String类提供了length()方法,确定字符串的长度,返回字符串中的字符数

equals()方法

String类提供了equals( )方法,比较存储在两个字符串对象的内容是否一致

equals()方法比较原理

equals():检查组成字符串内容的字符是否完全一致

“==”和equals()有什么区别呢?

==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象

equals():检查组成字符串内容的字符是否完全一致

字符串比较的其他方法

1.使用equalsIgnoreCase()方法

2.使用toLowerCase()方法

3.使用toUpperCase()方法

        String str = "qwertyuiop";
​
        // 获取字符串长度
        System.out.println(str.length());
​
        /*
         * 获取数组长度:数组名.length获取集合长度:集合名.size()获取字符串长度:字符串对象名.length()
         */
        // 比较两个字符串的内容是否相同
​
        String str1 = "qew";
        String str2 = "qew";
        System.out.println(str1.equals(str2));// true
        System.out.println(str1 == str2);// true
​
        String str3 = new String("abc");
        String str4 = new String("abc");
        System.out.println(str3.equals(str4));// true
        System.out.println(str3 == str4);// false
​
        // 其他比较字符串的方法
        String str5 = "qwert";
        String str6 = "qWert";
        System.out.println(str5.equals(str6));// false
​
        System.out.println(str5.equalsIgnoreCase(str6));// true,不区分大小写
​
        // 字符串对象调用方法后,不会改变自身的内容,相当于是对它的复制品
        String result = str6.toLowerCase();
​
        System.out.println(str6); // QWERt
​
        System.out.println(result); // qwert
​
        System.out.println(str5.toUpperCase());

字符串连接

方法1:使用“+”

方法2:使用String类的concat()方法

System.out.println("qwert");
​
        int num = 10;
        System.out.println(num);
        // +是一个连接符,连接字符串与变量的值
        System.out.println("num里面存储的值是:" + num);
​
        System.out.println("我的班级是:" + "Java2217");// 我的班级是:Java2217
        System.out.println(10 + 20 + "good");// 30good
        System.out.println(10 + "good" + 20);// 10good20
        System.out.println("good" + 10 + 20);// good1020
​
        String str1 = "qwert";
        String str2 = "yuiop";
        String result = str1.concat(str2);
        System.out.println(str1);// qwert
        System.out.println(str2);// yuiop
        System.out.println(result);// qwertyuiop
​

字符串常用提取方法

方法名说明
public int indexOf(int ch)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int indexOf(String value)
public int lastIndexOf(int ch)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
public int lastIndexOf(String value)
public String substring(int index)提取从位置索引开始的字符串部分
public String substring(int beginindex, int endindex)提取beginindex和endindex之间的字符串部分
public String trim()返回一个前后不含任何空格的调用字符串的副本
// 字符串常用提取方法
        String str1 = "abcdefghijklmnabcd";
        System.out.println(str1.length());// 14
​
        // public int indexOf(int ch) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
        // public int indexOf(String value) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
        int index = str1.indexOf(110);
        System.out.println(index);
        System.out.println(str1.indexOf("bc"));
​
        // public int lastIndexOf(int ch) 搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
        // public int lastIndexOf(String
        // value)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
        System.out.println(str1.lastIndexOf(97));
        System.out.println(str1.lastIndexOf("bcd"));
​
        // public String substring(int index)提取从位置索引开始的字符串到末尾部分
        System.out.println(str1);
        System.out.println(str1.substring(3));
        // public String substring(int beginindex, int
        // endindex)提取beginindex(包括)和endindex(不包括)之间的字符串部分
        System.out.println(str1);
        System.out.println(str1.substring(3, 6));
​
        // public String trim()返回一个前后不含任何空格的调用字符串的副本
        String str2 = "    qwert    yuiop      ";
        System.out.println(str2);
        System.out.println(str2.trim());
​
        // 字符串拆分方法
        String str3 = "长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
        String[] strs = str3.split(" ");
        for (String string : strs) {
            System.out.println(string);
        }
​
        String str4 = "我爱你但是你不爱我可是我依然爱你";
        String[] strs2 = str4.split("爱");
        System.out.println(Arrays.toString(strs2));
​
        // 判断字符串是否以指定内容结尾
        String str5 = "HelloWorld.javaa";
        System.out.println(str5.endsWith(".java"));
        // 判断字符串是否以执行内容开头
        System.out.println(str5.startsWith("He"));
​
        // 将字符串变成byte类型的数组
        byte[] bytes = str5.getBytes();
        for (byte b : bytes) {
            System.out.print((char) b);

StringBuffer

概念:对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率

        String str1 = "qwertyuiop";
        StringBuffer sb1 = new StringBuffer("qwert");
        
        //String类对象调用方法之后,对原来String对象中的内容没有影响
        System.out.println(str1);
        System.out.println(str1.substring(2));
        System.out.println(str1);
        
        System.out.println("---------------");
        //StringBuffer类对象调用方法返回结果还是StringBuffer的,这时候会改变原来StringBuffer对象里的内容
​
        System.out.println(sb1);
        
        System.out.println(sb1.insert(2, "qwe"));
        System.out.println(sb1);
        
        System.out.println(sb1.toString());
        
        System.out.println("-------------------------");
        //在末尾追加内容
        System.out.println(sb1);
        System.out.println(sb1.append("asdfghjkl"));
        System.out.println(sb1);

String类&StringBuffer类

String是不可变对象

1.经常改变内容的字符串最好不要使用String

2.StringBuffer是可变的字符串字符串

3.经常改变的情况可使用StringBuffer,更高效

4.JDK5.0后提供了StringBuilder,等价StringBuffer

操作日期时间

Date类:获取当前日期

java.util.Date类:表示日期和时间,提供操作日期和时间各组成部分的方法

java.text.SimpleDateFormat类

//创建Date类对象
Date date = new Date();
System.out.println(date);
//Date类中很多方法都已经过时,使用Calendar类替代
//System.out.println(date.getYear()+1900);
//System.out.println(date.getMonth()+1);
    
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));

Calendar类

抽象类,java.util.Calendar

用于设置和获取日期/时间数据的特定部分

方法或属性说明
int get(int field)返回给定日历字段的值
MONTH指示月
DAY_OF_MONTH指示一个月中的某天
DAY_OF_WEEK指示一个星期中的某天
//Calendar类是一个抽象类不能实例化,可以通过该类中的一个静态方法创建其引用
Calendar cal = Calendar.getInstance();
//获取年
int year = cal.get(Calendar.YEAR);
System.out.println(year);
//获取月
int month = cal.get(Calendar.MONTH);
System.out.println(month+1);
        
//获取日
int day = cal.get(Calendar.DATE);
System.out.println(day);
        
//获取时分秒
                    System.out.println(cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));
        
//获取星期
int day_of_week = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(day_of_week-1);
        
//获取当前日期是这一年中的第多少天
        System.out.println(cal.get(Calendar.DAY_OF_YEAR));
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值