Java常见类及应用总结

Number & Math类

1、Number 类

        包装类及其对应基本数据类型:

        包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类 Number 的子类。 

        当内置数据类型被当作对象使用的时候,编译器会把内置类型装箱为包装类,进行进一步运算的时候需要将对象拆箱为内置数据类型。

public class Test{
 
   public static void main(String[] args){
      Integer x = 5;  //装箱
      x =  x + 10;  //拆箱
      System.out.println(x);  //15
   }
}

2、Math 类

        Math 类包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数,可以直接调用。

3、常用方法

        注意方法 floor、round、ceil 的差异。

Character 类

       用于对单个字符的操作,在对象中包含一个基本类型 char 的值。

Character ch = new Character('a');

        方法: 

String 类

        String 创建的字符串存储在公共池中,而 new 创建的字符串对象在堆上。

String s1 = "Runoob";              // String 直接创建
String s2 = "Runoob";              // String 直接创建
String s3 = s1;                    // 相同引用
String s4 = new String("Runoob");   // String 对象创建
String s5 = new String("Runoob");   // String 对象创建

 

        注:String 类是不可改变的,一旦创建了 String 对象,那它的值就无法改变,如果需要对字符串做很多修改,那么应该选择使用 StringBuffer 和 StringBuilder 类

1、字符串连接的两种方式

string1.concat(string2);

string1 + string2;  //更常用

eg:

"我的名字是 ".concat("Runoob");   // 我的名字是Runoob

"Hello," + " runoob" + "!"       // Hello, runoob!

2、创建格式化字符串

        String 类使用静态方法 format() 返回一个String 对象

//直接打印输出
/*System.out.printf("浮点型变量的值为 " +
                  "%f, 整型变量的值为 " +
                  " %d, 字符串变量的值为 " +
                  "is %s", floatVar, intVar, stringVar);*/

//可以返回 String 对象,提高代码复用性
String fs;
fs = String.format("浮点型变量的值为 " +
                   "%f, 整型变量的值为 " +
                   " %d, 字符串变量的值为 " +
                   " %s", floatVar, intVar, stringVar);

3、常用方法

StringBuffer & StringBuilder 类

        StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。

        StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。

StringBuilder常用方法:

https://www.runoob.com/manual/jdk11api/java.base/java/lang/StringBuilder.htmlStringBuilder 类:https://www.runoob.com/manual/jdk11api/java.base/java/lang/StringBuilder.html

https://www.runoob.com/manual/jdk11api/java.base/java/lang/StringBuffer.htmlStringBuffer 类:https://www.runoob.com/manual/jdk11api/java.base/java/lang/StringBuffer.html

Scanner 类

java.util.Scanner 是 Java5 的新特征,可以通过 Scanner 类来获取用户的输入。

Scanner s = new Scanner(System.in);  //从键盘接收数据

1、Scanner 类常用方法:

返回类型方法描述
booleanhasNext()

如果此扫描器的输入中有另一个标记,则返回true。

booleanhasNextLine()

如果此扫描器的输入中有另一行,则返回true。

booleanhasNextDouble()如果使用 nextDouble()方法将此扫描仪输入中的下一个标记解释为double值,则返回true。
Stringnext()

从此扫描仪查找并返回下一个完整令牌。

StringnextLine()

使此扫描器前进超过当前行并返回跳过的输入。

intnextInt()

将输入的下一个标记扫描为 int 。

StringtoString()

返回此 Scanner的字符串表示 Scanner 。

Scannerreset()

重置此扫描仪。

voidclose()

关闭此扫描仪。

2、next() 与 nextLine() 区别

next():

  • 1、一定要读取到有效字符后才可以结束输入。
  • 2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
  • 3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
  • next() 不能得到带有空格的字符串。

nextLine():

  • 1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
  • 2、可以获得空白。
import java.util.Scanner; 
 
public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);  
        // 从键盘接收数据:  "hello world"
 
        // next方式接收字符串
        /*System.out.println("next方式接收:");
        // 判断是否还有输入
        if (scan.hasNext()) {
            String str1 = scan.next();
            System.out.println("输入的数据为:" + str1);    //hello
        }*/

        // nextLine方式接收字符串
        System.out.println("nextLine方式接收:");
        // 判断是否还有输入
        if (scan.hasNextLine()) {
            String str2 = scan.nextLine();
            System.out.println("输入的数据为:" + str2);   //hello world
        }

        scan.close();
    }
}

        如果要输入 int 或 float 类型的数据,在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取。

import java.util.Scanner;
 
public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
        int i = 0;
        float f = 0.0f;
        System.out.print("输入整数:");
        if (scan.hasNextInt()) {
            // 判断输入的是否是整数
            i = scan.nextInt();
            // 接收整数
            System.out.println("整数数据:" + i);
        } else {
            // 输入错误的信息
            System.out.println("输入的不是整数!");
        }
        System.out.print("输入小数:");
        if (scan.hasNextFloat()) {
            // 判断输入的是否是小数
            f = scan.nextFloat();
            // 接收小数
            System.out.println("小数数据:" + f);
        } else {
            // 输入错误的信息
            System.out.println("输入的不是小数!");
        }
        scan.close();
    }
}

参考链接:Java 教程 | 菜鸟教程

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值