javase personal rubbish review notes 03Number & Math class commonly used methods, Character method

Some commonly used methods of Number & Math class:


1 xxxValue()
converts the Number object into a value of xxx data type and returns. ****

public class Test{
    
     
 
   public static void main(String args[]){
    
    
      Integer x = 5;
      // 返回 byte 原生数据类型
      System.out.println( x.byteValue() );
 
      // 返回 double 原生数据类型
      System.out.println(x.doubleValue());
 
      // 返回 long 原生数据类型
      System.out.println( x.longValue() );      
   }
}

2 compareTo()
compares the number object with the parameter.
If the specified number is equal to the parameter, return 0.
If the specified number is less than the parameter, -1 is returned.
If the specified number is greater than the parameter return 1.

3 equals()
determines whether the number object is equal to the parameter.
If the Number object is not Null, and the method's parameter type and value are equal, return True, otherwise return False.

public class Test{
    
    
    public static void main(String args[]){
    
    
        Integer x = 5;
        Integer y = 10;
        Integer z =5;
        Short a = 5;

        System.out.println(x.equals(y));  
        System.out.println(x.equals(z)); 
        System.out.println(x.equals(a));
    }
}
/*编译以上程序,输出结果为:

false
true
false*/

4 valueOf()
returns a built-in data type
Integer specified by a Number object valueOf(int i): returns an Integer instance representing the specified int value.

Integer valueOf(String s): Returns the Integer object that holds the value of the specified String.

Integer valueOf(String s, int radix): Returns an Integer object, which holds the value extracted from the specified String when parsing with the radix provided by the second parameter.

public class Test{
    
    
public static void main(String args[]){
    
    
                Integer x =Integer.valueOf(9);
                Double c = Double.valueOf(5);
                Float a = Float.valueOf("80");              

                Integer b = Integer.valueOf("444",16);   // 使用 16 进制

                System.out.println(x);
                System.out.println(c);
                System.out.println(a);
                System.out.println(b);
        }
}

5 toString()
returns the value as a string.
toString(): Returns the String object representing the Integer value.

toString(int i): Returns the String object representing the specified int.

public class Test{
    
    
    public static void main(String args[]){
    
    
        Integer x = 5;

        System.out.println(x.toString());  
        System.out.println(Integer.toString(12)); 
    }
}
/*编译以上程序,输出结果为:

5
12*/

6 parseInt()
parses the string into int type.
parseInt(String s): Returns the integer value represented by the decimal parameter.

parseInt(int i): The integer represented by the string parameter using the specified radix (the radix can be 10, 2, 8, or hexadecimal numbers).

public class Test{
    
    
    public static void main(String args[]){
    
    
        int x =Integer.parseInt("9");
        double c = Double.parseDouble("5");
        int b = Integer.parseInt("444",16);

        System.out.println(x);
        System.out.println(c);
        System.out.println(b);
    }
}
/*编译以上程序,输出结果为:

9
5.0
1092

7 abs()
returns the absolute value of the parameter.

8 ceil()
returns the smallest integer greater than or equal to (>=) the given parameter, and the type is double-precision floating point.

9 floor()
returns the largest integer less than or equal to (<=) the given parameter.

public class Test{
    
    
    public static void main(String args[]){
    
    
        double d = 100.675;
        float f = -90;    
 
        System.out.println(Math.ceil(d));
        System.out.println(Math.ceil(f)); 
                     
        System.out.println(Math.floor(d));
        System.out.println(Math.floor(f)); 
    }
}
编译以上程序,输出结果为:

101.0
-90.0
100.0
-90.0

10 rint()
returns the integer closest to the argument. The return type is double.

public class Test{
    
    
    public static void main(String args[]){
    
    
        double d = 100.675;
        double e = 100.500;
        double f = 100.200;
 
        System.out.println(Math.rint(d));
        System.out.println(Math.rint(e)); 
        System.out.println(Math.rint(f)); 
    }
}
/*编译以上程序,输出结果为:

101.0
100.0
100.0

11 round()
It means rounding. The algorithm is Math.floor(x+0.5), which means adding 0.5 to the original number and then rounding down. Therefore, the result of Math.round(11.5) is 12, and Math.round( -11.5) The result is -11.
By default it returns an integer
12 min()
returns the minimum of the two parameters.

13 max()
returns the maximum of the two parameters.

14 exp()
returns the natural number base e to the power of the argument.

public class Test{
    
     
    public static void main(String args[]){
    
    
        double x = 11.635;
        double y = 2.76;

        System.out.printf("e 的值为 %.4f%n", Math.E);
        System.out.printf("exp(%.3f) 为 %.3f%n", x, Math.exp(x));  
    }
}
编译以上程序,输出结果为:

e 的值为 2.7183
exp(11.635)112983.831

15 log()
returns the logarithm of the natural base of the parameter. The value of ln

16 pow()
returns the first parameter to the power of the second parameter.

public class Test{
    
    
    public static void main(String args[]){
    
    
        double x = 11.635;
        double y = 2.76;

        System.out.printf("e 的值为 %.4f%n", Math.E);
        System.out.printf("pow(%.3f, %.3f) 为 %.3f%n", x, y, Math.pow(x, y));
    }
}
/*编译以上程序,输出结果为:

e 的值为 2.7183
pow(11.635, 2.760) 为 874.008

17 sqrt()
Find the arithmetic square root of the parameter.

18 sin()
finds the sine value of the specified double type parameter.

19 cos()
finds the cosine value of the specified double type parameter.

20 tan()
finds the tangent of the specified double type parameter.

21 asin()
finds the arc sine value of the specified double type parameter.

22 acos()
finds the arc cosine value of the specified double type parameter.

23 atan()
finds the arc tangent of the specified double type parameter.

24 atan2()
converts Cartesian coordinates to polar coordinates and returns the angle value of polar coordinates.

25 toDegrees()
converts parameters into angles.

26 toRadians()
converts the angle to radians.

27 random()
returns a random number.
Character method
1 isLetter()
is a letter
If the character is a letter, it returns true; otherwise, it returns false.
2 Is isDigit()
a numeric character
If the character is a number, it returns true; otherwise, it returns false.
3 Is isWhitespace()
a whitespace character?
If the character is a whitespace character, it returns true; otherwise, it returns false.
4 Whether isUpperCase()
is an uppercase letter
If the character is uppercase, it returns true; otherwise, it returns false.
5 Whether isLowerCase()
is a lowercase letter
If the character is lowercase, it returns true; otherwise, it returns false.
6 toUpperCase()
specifies the uppercase form of the letter.
Returns the uppercase form of the converted character, if any; otherwise, returns the character itself.
7 toLowerCase()
specifies the lowercase form of the letter.
Returns the lowercase form of the converted character, if any; otherwise, returns the character itself.
8 toString()
returns the character string form, the length of the string is only 1

public class Test {
    
    

    public static void main(String args[]) {
    
    
        System.out.println(Character.toString('a'));
        System.out.println(Character.toString('A'));
    }
}
以上程序执行结果为:

a
A

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108507257