java

Can i sum two String values without using Wrapper Class in java if yes than how ?


String result = "";
try{
 int value = Integer.parseInt(a)+Integer.parseInt(b);
 result = ""+value;
}catch(NumberFormatException ex){
 //either a or b is not a number
 result = "Invalid input";
}
MyStringSum.show(null,a+b);

I am using Wrapper Class but i want to sum two String values without using
Wrapper Class , can i do this?


Solution

  • You should not worry about the efficiency of your implementation, because it is not using any wrapper objects.

    java.lang.Integer plays several roles:

    1. It holds several important constants, such as MIN_VALUE and MAX_VALUE,
    2. It serves as a helper class for working with primitive ints by providing class methods for parsing and manipulating them, and
    3. Its instances serve as Object wrappers for primitive ints, and providing instance methods for it.

    Because you use only parse(...), which is a class method, your code uses the class in its capacity #2, ignoring the #1 and #3. In other words, you are not using Integer in its "wrapper for int" capacity.