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

Monkeys learn to program 2020-11-13 02:42:13
introduction java lesson intvalue doublevalue


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]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple