Java String to Int

Posted in /  

Java String to Int
ramyashankar

Ramya Shankar
Last updated on November 23, 2024

    In Java, a lot of data is stored as objects rather than primitives. A string is an object, whereas int is a primitive data type . The advantage of objects is the ease of manipulation and storage. If we need any calculations to be performed, it is easy to convert String to int using various methods. Java also has an autoboxing feature to convert primitive int into Integer and vice versa.

    Java String to Int conversion

    There are two ways to convert Java String to int (primitive type). We can use the parseInt method of the Integer class or the valueOf method of the same class. We will see both the implementations one by one and then perform some checks to see the difference between the addition between strings and integers. Method 1: Using parseInt()

    int num2 = 0;
    try{
    num2 = Integer.parseInt(“3”);
    }catch(NumberFormatException nfe){
    System.out.println("Not a number..");
    }

    Initially, we create a variable num2 that converts the string “3” into the number 3. We are adding NumberFormatException because we need to also handle scenarios where the text between the double quotes is not an integer. For example, if we type “text” in place of “3”, the program will exit gracefully if we handle the exception. Once we apply the parseInt method, we will get the primitive int value of “3”, which is 3. Method 2: Using valueOf()

    int num3 = Integer.valueOf("3");

    This method is also simple, just like the previous one. Let us now compare both the numbers using the == operator.

    System.out.println(num3 == num2);

    We will get the result as true, as both are numbers and not strings. If we compare both as strings, the equals to (==) operator will not work as expected.

    int number = 3;
    String convnum = String.valueOf(number);
    
    String numstr = "3";
    if(numstr.equals(convnum))
    System.out.println("Strings are Equal");
    
    if(numstr == convnum)
    System.out.println("does == work?");
    else
    System.out.println("'==' does not work for int as strings!");

    Here we have declared a number as int and another string numstr with the same value as the int number. We also have a convnum, which is a reverse conversion from a number to a string value. We do two things here:

    • Apply the .equals() method to compare the strings, which yields the correct result, i.e., both are equal.
    • Apply the equals operator (==), which returns false even though both numbers are 3. This is one of the important reasons for converting Java String to int when needed.

    Now, let's try to add the two strings and then add them after converting both into their respective integer values.

    String seven = "7";
    String three = "3";
    
    int numseven = Integer.valueOf(seven);
    int numthree = Integer.valueOf(three);
    
    System.out.println("Seven + Three (String): " + seven+three);
    System.out.println("Seven + Three (int): " + (numseven+numthree));

    Note that if we don’t add the brackets in the last System.out statement, we will not get ‘10’; instead, the numbers will be appended just like strings!

    Conclusion

    In this article, we have identified ways to convert Java String to int and also explored a few example scenarios and different use cases for the same.

    People are also reading:

    Leave a Comment on this Post

    0 Comments