zoukankan      html  css  js  c++  java
  • java 常用类的方法

    包装类:
    基本类型对应的引用类型
    主要用来做类型转换

      基本类型  对应的包装类

    • boolean —> Boolean
    • char —> Character
    • byte—> Byte
    • short—> Short
    • long—> Long
    • int —> Integer
    • float—> Float
    • double—> Double

    public Integer(int value)

            Integer in = new Integer(1);
            Integer in2 = 1;  //自动装箱

    public Integer(String s)

    Integer in = new Integer("1");//只能是数字字符串,否则运行异常 java.lang.NumberFormatException

    public int intValue() 包装类--->基本类型  (有自动拆箱就不用了)

     Integer in = new Integer(1);
     int a = in.intValue();

    public static int parseInt(String s) 最常用String--->int

            String s = "123";
            int b = Integer.parseInt(s);

    public static String toString(int i)  int-->String
    public static Integer valueOf(int i)  int-->integer (有自动装箱就不用了)

    Integer c = Integer.valueOf(6);

    public static Integer valueOf(String s) String-->integer

    Integer c = Integer.valueOf(“6”); //只能是数字字符串

    public static String toBinaryString(int i) 十进制--->二进制

         String s = Integer.toBinaryString(4);
             System.out.println(s);
    //执行结果:100

    public static String toOctalString(int i) 十进制--->八进制
    public static String toHexString(int i) 十进制--->十六进制

    十进制到其他进制 (radix意思是基数,其实就是问你要几进制)
    public static String toString(int i,int radix)

            String s = Integer.toString(4, 2);
            System.out.println(s);
    //        执行结果:100
            

    数字字符串转其他进制
    public static int parseInt(String s,int radix)

    //        让1+1等于3
            String s = "1";
            int m = Integer.parseInt(s+s, 2);
            System.out.println(m);
    //        执行结果:3


    Character类
    public Character(char value)

    1         Character ch = 'a';
    2         Character ch2= new Character('0');
    3         System.out.println(ch);
    4         System.out.println(ch2);
    5         System.out.println(ch+ch2);//97+48
    6 //        执行结果:
    7 //        a
    8 //        0
    9 //        145

    public static boolean isUpperCase(char ch) 判断字符是不是大写
    public static boolean isLowerCase(char ch) 判断字符是不是小写
    public static boolean isDigit(char ch) 判断字符是不是数字
    public static char toUpperCase(char ch) 字符转成大写

    public static char toLowerCase(char ch) 字符转成小写

    Math类:工具类的方法一般都是静态的


    public static int abs(int a)  //取绝对值
    public static double ceil(double a) //向上取整,返回比a还大的最小整数
    public static double floor(double a) //向下取整,返回比a还小的最大整数
    public static int max(int a,int b)     //返回最大的整数
    public static double pow(double a,double b)  //返回a的b次方
    public static double random()   //求取随机数(随机数的值0-1之间(不包括1))

    // 获取随机数公式 (int)(Math.random()*最大值+最小值)
    int a = (int)(Math.random()*10+1);//获取1-10的随机整数

    public static int round(float a) //四舍五入
    public static double sqrt(double a)  //对a开平方根


    Random类
    nextInt()

    nextInt(int)

    1     Random ran = new Random();
    2     int random = ran.nextInt(100);//获取0-100(不包括100的伪随机数)
    3     System.out.println(random);

    System类
    public static void gc() //垃圾回收
    public static void exit(int status) //退出程序,习惯正常退出参数使用0
    public static long currentTimeMillis() //获取当前时间的毫秒值  和日期类的对象名.getTime();相同


    BigInteger:解决大整数运算

    public BigInteger(String val) //将BigInteger的十进制字符串表示形式转换为BigInteger。将BigInteger的十进制字符串表示形式转换为BigInteger。

    BigInteger bi = new BigInteger("123"); //必须是整型字符串

    public BigInteger add(BigInteger val)
    public BigInteger subtract(BigInteger val)
    public BigInteger multiply(BigInteger val)
    public BigInteger divide(BigInteger val)
    public BigInteger[] divideAndRemainder(BigInteger val)   求出商和余数

            BigInteger bi = new BigInteger("12");
            BigInteger bi2 = new BigInteger("4");
            BigInteger[] bis = bi.divideAndRemainder(bi2);
            System.out.println(bis[0]);
            System.out.println(bis[1]);
        //执行结果:
        //   3
        //   0


    BigDecemal:解决小数运算精度的问题(最好是使用字符串形式的数字,以免精度丢失

    public BigDecimal add(BigDecimal augend)
    public BigDecimal subtract(BigDecimal subtrahend)
    public BigDecimal multiply(BigDecimal multiplicand)
    public BigDecimal divide(BigDecimal divisor)


    Date类上一个文章已经总结了时间类 https://www.cnblogs.com/19322li/p/10654328.html)

    java.util.Date类

    public Date() 无参构造方法,表示当前日期时间
    public Date(long date) 1970-1-1 0:0:0 以后多少毫秒的时间
    public long getTime() 获取距离起始计时的时间毫秒
    public void setTime(long time)


    SimpleDateFormat类

    new SimpleDateFormat(String patten)
    public final String format(Date date)
    public Date parse(String source)


    Calendar类

    public static Calendar getInstance()
    public int get(int field) 获取日历中指定部分
    public void add(int field,int amount)
    日期加减运算
    public final void set(int year,int month,int date)
    设置日历位指定的日期

  • 相关阅读:
    设计一个圆柱体类,计算表面积及体积。建立一个半径为3、高为3.5的圆柱体,输出其表面积及体积
    写一个方法完成如下功能,判断从文本框textbox1输入的一个字符,如果是数字则求该数字的阶乘,如果是小写字条,则转换为大写,大写字符不变,结果在文本框textbox2中显示
    写一方法用来计算1+2+3+...n,其中n作为参数输入,返回值可以由方法名返回,也可以由参数返回
    winform控件记录
    写4个同名方法,实现两个整数、两个实数,一个实数一个整数,一个整数一个实数之间的求和。在主调函数中调用这4个方法计算相关的值。(方法的重载)
    写一方法计算实现任意个整数之和.在主调函数中调用该函数,实现任意个数之和。(使用params参数)
    在主函数中提示用户输入用户名和密码。另写一方法来判断用户输入是否正确。该方法分别返回一个bool类型的登录结果和和一个string类型的登录信息。如登录成功,返回true及“登录成功”,若登录失败则返回false及“用户名错误”或“密码错误”(使用out参数)
    Linux下使用Kickstart自动化安装平台架构
    Day10 多线程理论 开启线程
    关闭ipv6的方法
  • 原文地址:https://www.cnblogs.com/19322li/p/10658776.html
Copyright © 2011-2022 走看看