编程知识 cdmana.com

Introduction to Java lesson 93 - intvalue and doublevalue methods of number

problem

     stay Java in , abstract class Number yes BigDecimal、BigInteger、Byte、Double、Float、Integer、Long and Short The class of the superclass . This class provides six methods , Pictured -1 Shown .

byte

byteValue()

  With byte Form returns the specified value .

abstract double

doubleValue()

  With double Form returns the specified value .

abstract float

floatValue()

  With float Form returns the specified value . 

abstract int

intValue()

  With int Form returns the specified value . 

abstract long

longValue()

  With long Form returns the specified value

short

shortValue()

  With short Form returns the specified value

     Of the above six methods ,floatValue Methods and doubleValue Method in use , It may involve rounding ; The other four methods may involve rounding or rounding .

     This case requires testing Number Class intValue Methods and doubleValue Method , namely , First , Definition Number Two variables of type :

    Number d=123.45;
    Number n=123;

     Ask for d Objects and n The data type of the object .

     then , take d Objects are converted to int Type variables and double Type variable , And output the result after conversion .

     Last , take n Objects are converted to int Type variables and double Type variable , And output the result after conversion .

programme

     First , New packages and classes , And create a new test method in the class .

     secondly , Use the code d.getClass().getName() And code n.getClass().getName() obtain d Objects and n The data type of the object . Here you just need to know that you can get the data type of the object .

     Third , Use Number Class intValue Methods and doubleValue Method , take d Objects are converted to int Type variables and double Type variable , And output the result after conversion .

     Fourth , Use Number Class intValue Methods and doubleValue Method , take n Objects are converted to int Type variables and double Type variable , And output the result after conversion .

step

     To implement this case, follow the steps below .

Step one : Build packages 、 Class and test method

     First, in the day02 The new name under the package is TestNumber Class , Then create a new test method in this class testIntValueAndDoubleValue, The code is as follows :

    package day02;
    
    import org.junit.Test;
    
    public class TestNumber{
    /**
     * test Number Of intValue Methods and doubleValue Method 
     */
     @Test
     public void testIntValueAndDoubleValue(){
     
     }
    }

Step two : obtain Number The object type of class

     Use the code d.getClass().getName() And code n.getClass().getName() obtain d Objects and n The data type of the object . Here you just need to know that you can get the data type of the object . The code is as follows :

    package day02;
    
    import org.junit.Test;
    
    public class TestNumber{
    /**
     * test Number Of intValue Methods and doubleValue Method 
     */
     @Test
     public void testIntValueAndDoubleValue(){
     
         Number d=123.45;
         Number n=123;
         
         // Output d and n The type of object 
         System.out.println(d.getClass().getName());//java.lang.Double
         System.out.println(n.getClass().getName());//java.lang.Integer
     
     }
    }

Step three : function

     Run... In step 2 testIntValueAndDoubleValue Method , The console output is as follows :

    java.lang.Double
    java.lang.Integer

     As can be seen from the output ,d The type of is Double,n The type of is Integer, This is because ,123.45 The type of direct quantity is double, After automatic packing, it will be converted to Double type ,Double The type is Number Subclass of type , Subclass type conversion to parent type is automatic type conversion , therefore ,d The type of is Double, Empathy ,n The type of is Integer.

Step four : take d Objects are converted to int Type variables and double Type variable , And output the result after conversion , The code is as follows :

    package day02;
    
    import org.junit.Test;
    
    public class TestNumber{
    /**
     * test Number Of intValue Methods and doubleValue Method 
     */
     @Test
     public void testIntValueAndDoubleValue(){
     
         Number d=123.45;
         Number n=123;
         
         // Output d and n The type of object 
         System.out.println(d.getClass().getName());//java.lang.Double
         System.out.println(n.getClass().getName());//java.lang.Integer
     
         int intValue=d.intValue();
         double doubleValue=d.doubleValue();
         System.out.println(intValue+","+doubleValue);//123,123.45
     }
    }

Step five : function

     Operation method testIntValueAndDoubleValue, The output of the last output statement is :

123,123.45

     It can be seen from the output result that , Will be original Double Object of type d, Convert to int After the type , Take it d The integer part of the object . But convert to double No change after type .

Step six : take n Objects are converted to int Type variables and double Type variable

     Use Number Class intValue Methods and doubleValue Method , take n Objects are converted to int Type variables and double Type variable , And output the result after conversion .

    package day02;
    
    import org.junit.Test;
    
    public class TestNumber{
    /**
     * test Number Of intValue Methods and doubleValue Method 
     */
     @Test
     public void testIntValueAndDoubleValue(){
     
         Number d=123.45;
         Number n=123;
         
         // Output d and n The type of object 
         System.out.println(d.getClass().getName());//java.lang.Double
         System.out.println(n.getClass().getName());//java.lang.Integer
     
         int intValue=d.intValue();
         double doubleValue=d.doubleValue();
         System.out.println(intValue+","+doubleValue);//123,123.45
         
         intValue=n.intValue();
         doubleValue=n.doubleValue();
         System.out.println(intValue+","+doubleValue);//123,123.0
     }
    }

Step seven : function

     Run again testIntValueAndDoubleValue Method , The output of the last output statement is :

123,123.0

     It can be seen from the output result that , Will be original Integer Object of type n, Convert to double After the type , Keep one decimal place after the decimal point . But convert to int No change after type .

Official account , Get a learning video

版权声明
本文为[Monkeys learn to program]所创,转载请带上原文链接,感谢

Scroll to Top