How to convert string to integer in java program

public class StringToIntExample
{
    public static void main(String args[])
    {
        String s = "200";
        int i = Integer.parseInt(s);
        
        System.out.println(s + 100); // 200100 because + is string concatenation operator
        System.out.println(i + 100); // 300 because + is binary plus operator
    }
}

Step-by-Step Explanation

1. Class Declaration:

public class StringToIntExample {

The class StringToIntExample is declared. This is the main class containing the main method where the logic of the program resides.

2. Main Method:

public static void main(String args[]) {

The main() method is the entry point of the Java application. When you run the program, the execution starts from this method.

3. String Declaration:

String s = “200”;

A String variable s is declared and initialized with the value \”200\”. This is a string representation of the number 200.

4. Converting String to Integer:

int i = Integer.parseInt(s);

  • The Integer.parseInt(s) method is used to convert the string s (which contains “200”) into an integer.
  • The value of Integer.parseInt(s) is the integer 200, which is assigned to the variable i.

5. First Output (String Concatenation):

System.out.println(s + 100);  // 200100 because + is string concatenation operator

  • The expression s + 100 is evaluated.
  • Here, s is a string (“200”) and 100 is an integer. When you use the + operator in Java with a string and a number, the number gets automatically changed into a string and then those two strings are concatenated.
  • Therefore, “200” + 100 ends up as a string 200100 printed out on your console.

6. Output Number (Math Addition):

System.out.println(i + 100);  // 300 because + is binary plus operator

  • The expression i + 100 is evaluated.
  • Here, i is the integer value 200 (as it was converted from the string s), and 100 is an integer.
  • The + operator here is a binary addition operator because both i and 100 are integers.
  • The expression 200 + 100 evaluates to 300, which is output to the console.

Output of the Program:

Running the program, the following output appears on the console:

200100

300

What the Output Means?

  • The first output is by string concatenation: here, s is a string (“200”) and 100 will be treated as a string hence concatenated to become “200100”.
  • The second output is by integer addition: i is an integer; 200, and 100 is an integer hence adding them together produces the result 300.

Key Points:

1. on which you apply the + operator between a string and another type, such as an integer, Java converts that other type into a string and then does string concatenation.

2. on which both operands are integers, Java uses the + operator for binary addition, adding their numeric values together.

Example:

String s = “200”;

int i = Integer.parseInt(s);  // Convert string “200” to integer 200

System.out.println(s + 100);  // “200” + 100 = “200100” (string concatenation)

System.out.println(i + 100);  // 200 + 100 = 300 (integer addition)

Leave a Comment

Your email address will not be published. Required fields are marked *