JAVA-实用类

来源:互联网 发布:网络协议基础知识 编辑:程序博客网 时间:2024/05/18 18:04

常用类

一)Scanner

1)JDK5以后出现,键盘录入的类2)构造方法    A:System.in是什么 InputStream 的子类3)基本使用方法    A:hashNextXxx()    B: nextXxx()4)注意的问题    先调用nextXxx(),然后使用nextLine()    nextLine()可以接受空格,字符串    可以使用next() 替换

二)String类的概述和使用

1)多个字符组成的一串数据。2)构造方法:    A:public String()    B:public String(String original)    C:public String(byte[] bytes)    D:public String(char[] value)3)字符串的特点(==和equals的区别)    A:字符串一旦被赋值,就不能改变。(注,不能改变指的是内容,不是引用)    B:注意注意String str = new String("hello");和String str="hello";4)字符串的功能    A:判断功能:equals,equalsIgnoreCalse,contains,startsWith,endsWith,isEmpty    B:获取功能:length,charAt,indexOf,subString,    C:转换功能:getBytes(),toCharArray(),valueOf    D:替换功能: replace(char old,char new)、replace(String old,String new)    E:其他功能:String trim()去空格、compareTo比较 5)面试题:    字符串如果是变量相加,先开空间,在拼接    String s1 = "hello";    String s2 = "world";    String s3 = "helloworld";    System.out.println(s3 == s1 + s2);    System.out.println(s3.equals((s1 + s2)));    System.out.println(s3 == "hello" + "world");    System.out.println(s3.equals("hello" + "world"));

三)StringBuffer

1)用字符串做拼接,耗时耗内存。所以java提供字符串缓冲区2)A:StringBuffer()\(int size)\(String str)3)StringBuffer的常见功能4)StringBuffer<--->相互转换    A:    String---->StringBuffer 构造方法,    StringBuffer---->String  toString方法    B:字符串的拼接    C:判断是否是回文串5)StringBuffer和StringBuilder的区别    StringBuffer同步,数据安全,效率低

四)Integer

0)自动装箱    Integer i = 127; // Integer i = Integer.valueOf(127);   自动拆箱    Integer i = 1234;     Integer i1 = i + 1000;// Integer i1 = i.intValue() + 1000;1)让基本数据类型 可以当做一个对象使用,提供了包装类型    int--->Integer    char--->Character2)Integer的构造方法    A:Integer i = new Integer(100);    B:Integer i = new Integer("100");3)String 和 int的相互转换    A:String--->int   Integer.parseInt("100");    B:int--->String   String.valueOf(100);4)拓展性问题:    -128到127之间的数据缓冲池问题

五)Character – 拓展

1)Character构造方法

  Character ch = new Character('a');

2)方法

        Character.isDigit('1');        Character.isLetter('a');        Character.isLowerCase('a');        Character.isUpperCase('A');        Character.toLowerCase('A');        Character.toUpperCase('b');

3)统计字符串大写,小写,数字个数


六)Math

1)2)方法    
     最大值:static int max(int a, int b)  当然也有min     a值的自然对数(底数是 e):static double log(double a)      a的b次幂  :static double pow(double a, double b)      绝对值 static double abs(double a)     向上取整 static double ceil(double a)      向下取整 static double floor(double a)      四舍五入 static int round(float a)      平方根   public static double sqrt(double a)     π    Math.PI

……
返回角的三角正弦。

随机值 [0.0 ,1.0)的double值:static double random()
可以实现获取任意范围内的随机数。


七)Random —扩展

1)由Math.random()方法的源码 — 看随机怎么实现
2)看情况自己实现获取整形随机值
3)这个是伪随机和随机的区别
A:new Random();是否传参
给定种子,获取随机数相同
使用默认的时间为种子,随机每次不同
B:new Random().nextInt() — 整形范围值
new Random().nextInt(n) — [0,n)


八)BigInteger

浮点存储方式和整形存储方式不同,
浮点运算很少是精确的,超过精度表示范围会产生误差。由此,产生的结果接近但不等于想要的结果。在使用 float 和 double 作精确运算要注意。
可以使用: BigDecimal 或 使用 long 类型来转换。

    BigDecimal add(BigDecimal augend)    subtract(BigDecimal subtrahend)    multiply(BigDecimal multiplicand)    divide(BigDecimal divisor)    divide(BigDecimal divisor,int scale,int roundingMode):商,小数位数,取舍方式
1 0
原创粉丝点击