DSA - 2

Modified on Tue, 28 Mar, 2023 at 4:51 PM

Why are we using number/integer as a string? Why can’t we always typecast strings?

Do you understand the error?

Sometimes numbers are stored in a string and not as an int or long, why is this?

Where is the error occurring?

Can we convert a string to an int without any drawbacks? What is the max value of an int and long?

Why do you think you are seeing this error? What might be causing it?

What happens when we try to typecast a string with a number that is greater than the limit of int?

How to Debug this? To debug this issue, I would recommend the following steps:

There is a limit on the size of the number a int/long/etc. can hold, so in cases like this we are using a string to hold it. It is also important to handle this as string and not convert the data type.


If you’re trying to convert a string to a number, first you have to check that it won't exceed the limit, if the string is large when you typecast you will have an overflow and your code will fail.

String s = “91213353453785679348257298463248568247175391283”

If we try to typecast the above string to int it will overflow and give you the wrong value. So in cases like this we just need to handle it as a string. For example we are trying to add two numbers that are strings we need to find a way to handle this.

Num1 = “111”

Num2 = “222”

We can parse the string to int char by char and add them to get the result and then store the value in a string again so we don’t have any overflow issues.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article