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 While Statement

class Example {
   static void main(String[] args) {
      int count = 0;
		
      while(count<5) {
         println(count);
         count++;
      }
   }
}

Groovy Assignment Operators

class Example {
   static void main(String[] args) {
      int x = 5;
		
      println(x+=3);
      println(x-=3);
      println(x*=3);
      println(x/=3);
      println(x%=3);   
   }
} 

Groovy Bitwise Operators

class Example { 
   static void main(String[] args) { 
      int a = 00111100; 
      int b = 00001101; 
      int x;
		
      println(Integer.toBinaryString(a&b)); 
      println(Integer.toBinaryString(a|b)); 
      println(Integer.toBinaryString(a^b)); 
		
      a=~a; 
      println(Integer.toBinaryString(a)); 
   } 
}

Groovy Logical Operators

class Example { 
   static void main(String[] args) { 
      boolean x = true; 
      boolean y = false; 
      boolean z = true; 
		
      println(x&&y); 
      println(x&&z); 
		
      println(x||z); 
      println(x||y); 
      println(!x); 
   } 
}

Groovy Relational Operators

class Example { 
   static void main(String[] args) { 
      def x = 5;
      def y = 10;
      def z = 8;
		
      if(x == y) { 
         println("x is equal to y"); 
      } else 
         println("x is not equal to y"); 
			
      if(z != y) { 
         println("z is not equal to y"); 
      } else 
         println("z is equal to y"); 
				
      if(z != y) { 
         println("z is not equal to y"); 
      } else 
         println("z is equal to y"); 
					
      if(z<y) { 
         println("z is less than y"); 
      } else 
         println("z is greater than y"); 
						
      if(x<=y) { 
         println("x is less than y"); 
      } else 
         println("x is greater than y"); 
			
      if(x>y) { 
         println("x is greater than y"); 
      } else 
         println("x is less than y"); 
			
      if(x>=y) { 
         println("x is greater or equal to y"); 
      } else 
         println("x is less than y"); 
   } 
} 

Groovy Arithmetic Operators

class Example { 
   static void main(String[] args) { 
      // Initializing 3 variables 
      def x = 5; 
      def y = 10; 
      def z = 8; 
		
      //Performing addition of 2 operands 
      println(x+y); 
		
      //Subtracts second operand from the first 
      println(x-y); 
		
      //Multiplication of both operands 
      println(x*y);
		
      //Division of numerator by denominator 
      println(z/x); 
		
      //Modulus Operator and remainder of after an integer/float division 
      println(z%x); 
		
      //Incremental operator 
      println(x++); 
		
      //Decrementing operator 
      println(x--);  
   } 
} 

Range Operators in Groovy

class Example { 
   static void main(String[] args) { 
      def range = 5..10; 
      println(range); 
      println(range.get(2)); 
   } 
}

Printing Variables

class Example { 
   static void main(String[] args) { 
      //Initializing 2 variables 
      int x = 5; 
      int X = 6; 
	  
      //Printing the value of the variables to the console 
      println("The value of x is " + x + "The value of X is " + X);  
   }
}

Naming Variables

class Example { 
   static void main(String[] args) { 
      // Defining a variable in lowercase  
      int x = 5;
	  
      // Defining a variable in uppercase  
      int X = 6; 
	  
      // Defining a variable with the underscore in it's name 
      def _Name = "Joe"; 
		
      println(x); 
      println(X); 
      println(_Name); 
   } 
}

Variable Declarations

class Example { 
   static void main(String[] args) { 
      // x is defined as a variable 
      String x = "Hello";
		
      // The value of the variable is printed to the console 
      println(x);
   }
}

Advertisements
Loading...

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