How can I convert a std::string to int?

In C++11 there are some nice new convert functions from std::string to a number type. So instead of atoi( str.c_str() ) you can use std::stoi( str ) where str is your number as std::string. There are version for all flavours of numbers: long stol(string), float stof(string), double stod(string),… see http://en.cppreference.com/w/cpp/string/basic_string/stol

Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?

When you compile a number literal in Java and assign it to a Integer (capital I) the compiler emits: Integer b2 =Integer.valueOf(127) This line of code is also generated when you use autoboxing. valueOf is implemented such that certain numbers are “pooled”, and it returns the same instance for values smaller than 128. From the … Read more

How does Java handle integer underflows and overflows and how would you check for it?

If it overflows, it goes back to the minimum value and continues from there. If it underflows, it goes back to the maximum value and continues from there. You can check that beforehand as follows: public static boolean willAdditionOverflow(int left, int right) { if (right < 0 && right != Integer.MIN_VALUE) { return willSubtractionOverflow(left, -right); … Read more