Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Groovy abs()

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

Groovy parseInt()

class Example {
   static void main(String[] args) { 
      int x = Integer.parseInt("9");
      double y = Double.parseDouble("5");
      int z = Integer.parseInt("444",16);
		
      System.out.println(x);
      System.out.println(y);
      System.out.println(z);
   } 
} 

Groovy toString()

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

Groovy valueOf()

class Example {
   static void main(String[] args) {
      int x = 5;
      Double z = 15.56;
		
      Integer xNew = Integer.valueOf(x);
      println(xNew);
		
      Double zNew = Double.valueOf(z);
      println(zNew);
   } 
}

Groovy equals()

class Example { 
   static void main(String[] args) { 
      Integer x = 5; 
      Integer y = 10; 
      Integer z = 5; 
		
      //Comparison against an Integer of different value 
      System.out.println(x.equals(y));
		
      //Comparison against an Integer of same value 
      System.out.println(x.equals(z));  
   } 
}

Groovy xxxValue()

class Example { 
   static void main(String[] args) {  
      Integer x = 5; 
		
      // Converting the number to double primitive type
      println(x.doubleValue()); 
		
      // Converting the number to byte primitive type 
      println(x.byteValue()); 
		
      // Converting the number to float primitive type 
      println(x.floatValue());
		
      // Converting the number to long primitive type 
      println(x.longValue()); 
		
      // Converting the number to short primitive type 
      println(x.shortValue()); 
		
      // Converting the number to int primitive type 
      println(x.intValue());  
   } 
}

Groovy compareTo()

class Example { 
   static void main(String[] args) { 
      Integer x = 5;
		
      //Comparison against a Integer of lower value 
      System.out.println(x.compareTo(3));
		
      //Comparison against a Integer of equal value 
      System.out.println(x.compareTo(5)); 
		
      //Comparison against a Integer of higher value 
      System.out.println(x.compareTo(8)); 
   } 
}

Groovy Optionals

class Example { 
   static void main(String[] args) { 
      // Example of an Integer using def 
      def a = 100; 
      println(a); 
		
      // Example of an float using def 
      def b = 100.10; 
      println(b); 
		
      // Example of an Double using def 
      def c = 100.101; 
      println(c);
		
      // Example of an String using def 
      def d = "HelloWorld"; 
      println(d); 
   } 
} 

Local and External Parameter Names

class Example { 
   static int x = 100; 
	
   public static int getX() { 
      int lx = 200; 
      println(lx); 
      return x; 
   } 
	
   static void main(String[] args) { 
      println(getX()); 
   }  
}

Groovy Nested Switch Statement

class Example { 
   static void main(String[] args) { 
      //Initializing 2 variables i and j 
      int i = 0; 
      int j = 1; 
		
      // First evaluating the value of variable i 
      switch(i) { 
         case 0: 
            // Next evaluating the value of variable j 
            switch(j) { 
               case 0: 
                  println("i is 0, j is 0"); 
                  break; 
               case 1: 
                  println("i is 0, j is 1"); 
                  break; 
               
               // The default condition for the inner switch statement 
               default: 
               println("nested default case!!"); 
            } 
         break; 
			
         // The default condition for the outer switch statement 
         default: 
            println("No matching case found!!"); 
      }
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.