Pages

Number Class

1. xxxxValue Method Example

public class MethodOne {
      public static void main(String args[]){
            Integer x = 6;
            System.out.println( x.byteValue() );
            System.out.println(x.doubleValue());
            System.out.println( x.longValue() );
      }
}

Output :

6       6.0       6.

2. compareTo Method Example

public class MethodTwo {
      public static void main(String args[]) {
            Integer x = 6;
            System.out.println(x.compareTo(3));
            System.out.println(x.compareTo(6));
            System.out.println(x.compareTo(8));
      } }

Output :

1       0       -1.

3. equals Method Example

public class MethodThree{
      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));
      }
}

Output :

false       true       false.

4. valueOf Method Example

public class MethodFour{
      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);
            System.out.println(x);
            System.out.println(c);
            System.out.println(a);
            System.out.println(b);
      }
}

Output :

9       5.0       80.0       1092.

5. toString Method Example

public class MethodFive {
      public static void main(String args [] ) {
            Integer x = 5;
            System.out.println(x.toString());
            System.out.println(Integer.toString(12));
      }
}

Output :

5       12.

6. parseXXX Method Example

public class MethodSix {
      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);
      }
}

Output :

9       5.0       1092.

7. absolute Method Example

public class MethodSeven {
      public static void main(String args [] ) {
            Integer a = -8;
            double d = -100;
            float f = -90;
            System.out.println(Math.abs(a));
            System.out.println(Math.abs(d));
            System.out.println(Math.abs(f));
      }
}

Output :

8       100.0      90.0.

8. ceil Method Example

public class MethodEight {
      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));
      }
}

Output :

-100.0       -90.0       -101.0       -90.0.

9. floor Method Example

public class MethodNine {
      public static void main(String args [] ) {
            double d = -100.675;
            float f = -90;
            System.out.println(Math.floor(d));
            System.out.println(Math.floor(f));
            System.out.println(Math.ceil(d));
            System.out.println(Math.ceil(f));
      }
}

Output :

-101.0       -90.0       -100.0       -90.0.

10. rint Method Example

public class MethodTen {
      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));
      }
}

Output :

101.0       100.0       100.0.

11. round Method Example

public class MethodEleven {
      public static void main(String args [] ) {
            double d = 100.675;
            double e = 100.500;
            float f = 100;
            float g = 90f;
            System.out.println(Math.round(d));
            System.out.println(Math.round(e));
            System.out.println(Math.round(f));
            System.out.println(Math.round(g));
      }
}

Output :

101       101       100       90.

12. min Method Example

public class MethodTwelve {
      public static void main(String args [] ) {
            System.out.println(Math.min(12.123, 12.456));
            System.out.println(Math.min(23.12, 23.0));
      }
}

Output :

12.123       23.0.

13. max Method Example

public class MethodThirteen {
      public static void main(String args [] ) {
            System.out.println(Math.max(12.123, 12.456));
            System.out.println(Math.max(23.12, 23.0));
      }
}

Output :

12.456       23.12.

14. log Method Example

public class LogExample {
      public static void main(String args [] ) {
            double x = 11.635;
            double y = 2.76;
            System.out.printf("The value of e is %.4f%n", Math.E);
            System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x));
      }
}

Output :

The value of e is 2.7183
log(11.635) is 2.454

15. exp Method Example

public class ExpExample {
      public static void main(String args [] ) {
            double x = 11.635;
            double y = 2.76;
            System.out.printf("The value of e is %.4f%n", Math.E);
            System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x));
      }
}

Output :

The value of e is 2.7183
exp(11.635) is 112983.83

16. Power Method Example

public class PowerExample {
      public static void main(String args [] ) {
            double x = 11.635;
            double y = 2.76;
            System.out.printf("The value of e is %.4f%n", Math.E);
            System.out.printf("pow(%.3f, %.3f) is %.3f%n", x, y, Math.pow(x, y));
      }
}

Output :

The value of e is 2.7183
pow(11.635, 2.760) is 874.008

17. Square Root Method Example

public class SquareRootExample {
      public static void main(String args [] ) {
            double x = 11.635;
            double y = 2.76;
            System.out.printf("The value of e is %.4f%n", Math.E);
            System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x));
      }
}

Output :

The value of e is 2.7183
sqrt(11.635) is 3.411

18. Sin Method Example

public class SinExample {
      public static void main(String args [] ) {
            double degrees = 45.0;
            double radians = Math.toRadians(degrees);
            System.out.format("The value of pi is %.4f%n", Math.PI);
            System.out.format("The sine of %.1f degrees is %.4f%n", degrees, Math.sin(radians));
      }
}

Output :

The value of pi is 3.1416
The sine of 45.0 degrees is 0.7071

19. Cos Method Example

public class CosExample {
      public static void main(String args [] ) {
            double degrees = 45.0;
            double radians = Math.toRadians(degrees);
            System.out.format("The value of pi is %.4f%n", Math.PI);
            System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians));
      }
}

Output :

The value of pi is 3.1416
The cosine of 45.0 degrees is 0.7071

20. Tan Method Example

public class TanExample {
      public static void main(String args [] ) {
            double degrees = 45.0;
            double radians = Math.toRadians(degrees);
            System.out.format("The value of pi is %.4f%n", Math.PI);
            System.out.format("The tangent of %.1f degrees is %.4f%n", degrees, Math.tan(radians));
      }
}

Output :

The value of pi is 3.1416
The tangent of 45.0 degrees is 1.0000

21. ToDegree Method Example

public class ToDegree {
    public static void main(String args [] ) {
        double x = 45.0;
        double y = 30.0;
        System.out.println( Math.toDegrees(x) );
        System.out.println( Math.toDegrees(y) );
    }
}

Output :

2578.3100780887044
1718.8733853924698

22. ToRadians Method Example

public class ToRadians {
    public static void main(String args [] ) {
        double x = 45.0;
        double y = 30.0;
        System.out.println( Math.toRadians(x) );
        System.out.println( Math.toRadians(y) );
    }
}

Output :

0.7853981633974483
0.5235987755982988

23. Random Method Example

public class RandomExample {
    public static void main(String args [] ) {
        System.out.println( Math.random() );
        System.out.println( Math.random() );
    }
}

Output :

0.16763945061451657
0.400551253762343