Avatar ·

How to convert a String to an int in Java?

Hello everyone! I just started learning Java and ran into a problem. I have a string that contains a number, for example, "123". I need to perform arithmetic operations with this number, but the compiler gives an error. Here's my code: ```java String numberStr = "123"; int result = numberStr + 10; // Error! System.out.println(result); ``` The compiler gives an error: "incompatible types: String cannot be converted to int". I understand that a string and a number are different data types, but how do I get a number from a string? In other languages, like Python, this is done simply with the int() function. Is there something similar in Java? I tried googling, but the answers are too complicated for a beginner. Please explain in simple terms how to safely convert a string to int, and what to do if the string contains something unexpected.