Advertisement
Rahmadnet

NUMBERS

Jun 26th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.31 KB | None | 0 0
  1. * Numbers
  2.  
  3. package Numbers;
  4.  
  5. public class AssignmentNumber
  6. {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         System.out.println("(xxxValue() Method)");
  11.        
  12.         /*
  13.          * Syntax :
  14.          * byte byteValue()
  15.          * short shortValue()
  16.          * long longValue()
  17.          * float floatValue()
  18.          * double doubleValue()
  19.          * Parameters :
  20.          * ----
  21.          * Returns :
  22.          * the numeric value represented by this object
  23.          * after conversion to specified type
  24.          */
  25.        
  26.         // creating a Double class object with value "6.9685"
  27.         Double d = new Double("6.9685");
  28.        
  29.         // Converting this Double(Number) object to
  30.         // different primitive data types
  31.         byte b = d.byteValue();
  32.         short s = d.shortValue();
  33.         int i = d.intValue();
  34.         long l = d.longValue();
  35.         float f = d.floatValue();
  36.         double d1 = d.doubleValue();
  37.        
  38.         System.out.println("value of d after converting is to byte = " + b);
  39.         System.out.println("value of d after converting is to short = " + s);
  40.         System.out.println("value of d after converting is to int = " + i);
  41.         System.out.println("value of d after converting is to long = " + l);
  42.         System.out.println("value of d after converting is to float = " + f);
  43.         System.out.println("value of d after converting is to double = " + d1);
  44.        
  45.         System.out.println("\n(CompareTo() Method)");
  46.        
  47.         /*
  48.          * Syntax :
  49.          * public int compareTo( NumberSubClass referenceName )
  50.          * Parameters :
  51.          * referenceName - any NumberSubClass type value
  52.          * Returns :
  53.          * the value 0 if the Number is equals to the argument.
  54.          * the value -1 if the Number is less than the argument.
  55.          * the value 1 if the Number is greater than the argument.
  56.          */
  57.        
  58.         // Creating an Integer class object with value "10"
  59.         @SuppressWarnings("deprecation")
  60.         Integer i1 = new Integer("10");
  61.        
  62.         // comparing value of i
  63.         System.out.println(i1.compareTo(7));
  64.         System.out.println(i1.compareTo(11));
  65.         System.out.println(i1.compareTo(10));
  66.         System.out.println(i1.compareTo(30));
  67.        
  68.         System.out.println("\n(Equals() Method)");
  69.        
  70.         /*
  71.          * Syntax :
  72.          * public boolean equals (Object obj)
  73.          * Parameters :
  74.          * obj - any object
  75.          * Returns :
  76.          * The method returns true if the argument is not null and
  77.          * is an object of the same type and with the same numeric value,
  78.          * otherwise false.
  79.          */
  80.        
  81.         // Creating a short Class object with value "15"
  82.         Short s1 = new Short("15");
  83.        
  84.         // creating a short Class object with value "10"
  85.         Short x = 10;
  86.        
  87.         // Creating an Integer Class object with value "15"
  88.         Integer y = 15;
  89.        
  90.         // Creating another Short Class object with value "15"
  91.         Short z = 15;
  92.        
  93.         // Comparing s with other objects
  94.         System.out.println(s1.equals(x));
  95.         System.out.println(s1.equals(y));
  96.         System.out.println(s1.equals(z));
  97.        
  98.         System.out.println("\n(ParseInt() Method)");
  99.        
  100.         /*
  101.          * Syntax :
  102.          * static int parseInt(String s, int radix)
  103.          * Parameters :
  104.          * s - any String representation of decimal
  105.          * radix - any radix value
  106.          * returns :
  107.          * the integer value represented by the argument in decimal.
  108.          * Throws :
  109.          * NumberFormatException : if the string does not contain a parsable integer.
  110.          */
  111.        
  112.         // Parsing different strings
  113.         int z1 = Integer.parseInt("654",8);
  114.         int a = Integer.parseInt("-FF", 16);
  115.         long l1 = Long.parseLong("2158611234",10);
  116.        
  117.         System.out.println(z1);
  118.         System.out.println(a);
  119.         System.out.println(l1);
  120.        
  121.         // run-time NumberFormatException will occur here
  122.         // "RAHMAD" is not a parsable string
  123.         //int x1 = Integer.parseInt("RAHMAD",8);
  124.        
  125.         // run-time NumberFormatException will occur here
  126.         // (for octal(8),allowed digits are [0-7])
  127.         //int y2 = Integer.parseInt("99",8);
  128.        
  129.        
  130.         System.out.println("\n(toString() Method)");
  131.        
  132.         /*
  133.          * Syntax :
  134.          * String toString()
  135.          * String toString(int i)
  136.          * Parameters :
  137.          * String toString() - no parameter
  138.          * String toString(int i) - i: any integer value
  139.          * Returns :
  140.          * String toString() -
  141.          * returns a String object representing the value of the Number object
  142.          * on which it is invoked.
  143.          * String toString(int i) -
  144.          * returns a decimal String object representing the specified integer(i)
  145.          */
  146.        
  147.         // demonstrating toString() method
  148.         Integer x2 = 12;
  149.        
  150.         System.out.println(x2.toString());
  151.        
  152.         // demonstrating toString(int i) method
  153.         System.out.println(Integer.toString(12));
  154.        
  155.         System.out.println(Integer.toBinaryString(152));
  156.         System.out.println(Integer.toHexString(152));
  157.         System.out.println(Integer.toOctalString(152));
  158.  
  159.  
  160.         System.out.println("\n(valueOf() Method)");
  161.        
  162.         /*
  163.          * Syntax :
  164.          * Integer valueOf(int i)
  165.          * Integer valueOf(String s)
  166.          * Integer valueOf(String s, int radix)
  167.          * Parameters :
  168.          * i - any integer value
  169.          * s - any String representation of decimal
  170.          * radix - any radik value
  171.          * Returns :
  172.          * valueOf(int i) : an Integer object holding the valuepresentend by the int argument.
  173.          * valueOf(String s) : an Integer object holding value represented by the string argument.
  174.          * valueOf(String s, int radix) : an Integer object holding the value
  175.          * represented by the String argument with base radix.
  176.          * Throws :
  177.          * valueOf(String s) -
  178.          * NumberFormatException : if the string does not contain a parsable integer.
  179.          * valueOf(String s, int radix) -
  180.          * NumberFormatException : if the string does not contain a parsable integer.
  181.          */
  182.        
  183.         // demonstrating valueOf(int i) method
  184.         System.out.println("Demonstrating valueOf(String i) method");
  185.         Integer i2 = Integer.valueOf(50);
  186.         Double d2 = Double.valueOf(9.36);
  187.         System.out.println(i2);
  188.         System.out.println(d2);
  189.        
  190.         // demonstrating valueOf(String s) method
  191.         System.out.println("Demonstrating valueOf(String s) method");
  192.         Integer n = Integer.valueOf("333");
  193.         Integer m = Integer.valueOf("-255");
  194.         System.out.println(n);
  195.         System.out.println(m);
  196.        
  197.        
  198.         // demonstrating valueOf(String s, int radix) method
  199.         System.out.println("Demonstrating valueOf(String s) method");
  200.         Integer y1 = Integer.valueOf("333",8);
  201.         Integer x1 = Integer.valueOf("-255",16);
  202.         Long l2 = Long.valueOf("51688245", 16);
  203.         System.out.println(y1);
  204.         System.out.println(x1);
  205.         System.out.println(l2);
  206.        
  207.        
  208.         // run-time NumberFormatException will occur in below cases
  209.         //Integer a1 = Integer.valueOf("RAHMAD");
  210.         //Integer b1 = Integer.valueOf("RAHMAD",16);
  211.        
  212.        
  213.     }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement