Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - parseInt() method



parseInt() method is used to get the primitive data type of a certain String. parseInt() is a static method and can have one argument or two.

Syntax

static int parseInt(String s)
  
static int parseInt(String s, int radix)

Parameters

  • s − This is a string representation of decimal.

  • radix − This would be used to convert String s into integer.

Return Value

  • parseInt(String s) − This returns an integer (decimal only).

  • parseInt(int i) − This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.

Example - Usage of parseInt() method

Following is an example of the usage of parseInt method −

class Example {
   static void main(String[] args) { 
      int x = Integer.parseInt("444");
		
      System.out.println(x);
   } 
} 

When we run the above program, we will get the following result −

444

Example - Usage of parseInt() method for hexadecimal number

Following is an example of the usage of parseInt method for hexadecimal number −

class Example {
   static void main(String[] args) { 
      int x = Integer.parseInt("444", 16);
		
      System.out.println(x);
   } 
} 

When we run the above program, we will get the following result −

1092

Example - Usage of parseInt() method for octal number

Following is an example of the usage of parseInt method for an octal number −

class Example {
   static void main(String[] args) { 
      int x = Integer.parseInt("444", 8);
		
      System.out.println(x);
   } 
} 

When we run the above program, we will get the following result −

292
groovy_numbers.htm
Advertisements