Page 69 - CTS - CSA TP - Volume 2
P. 69

COMPUTER SOFTWARE APPLICATION - CITS



           EXERCISE 90 : Use the JAVA numbers class methods



            Objectives

           At the end of this exercise you shall be able to
           •    know more about  the use of number class methods in Java
           •    develop Java programs using number class methods.

           Requirements


           Tools/Materials
           •  PC/Laptop  with Windows OS
           •  JDK Software
           •  Text editor (Visual studio / Sublime / Note pad)


            Procedure

           In Java, the Number class is an abstract class that serves as the superclass for all the numeric wrapper classes.
           The most commonly used numeric wrapper classes are:
              1  Byte
              2  Short
              3  Integer

              4  Long
              5  Float
              6  Double
           All these classes extend the Number class and provide methods to convert the primitive types (byte, short, int,
           long, float, double) into their respective wrapper objects and vice versa. They also inherit methods from the
           Number class.
           1  Byte

              •  Represents an 8-bit signed integer.
              •  Methods include: byteValue(), shortValue(), intValue(), longValue(), floatValue(), doubleValue().
              Task_Byte :  Here’s an example program demonstrating the use of methods inherited from the Number
                 class for the Byte class:
            public class ByteMethodsExample {
               public static void main(String[] args) {
                   // Example using Byte class
                   Byte byteValue = 120;
                  // byteValue()

                   byte byteResult = byteValue.byteValue();
                   System.out.println(“byteValue(): “ + byteResult);
                   // shortValue()
                   short shortResult = byteValue.shortValue();

                   System.out.println(“shortValue(): “ + shortResult);





                                                           54
   64   65   66   67   68   69   70   71   72   73   74