Converter String

Alguém sabe uma forma de converter uma string para um int?

Integer.parseInt(str);

java.lang.Integer.parseInt(“01”);???

Quando o Anders Hejslberg foi projetar o C# ele pegou uma listinha de todas as coisas não-intuitivas que ele tinha achado no Java (lembrem-se que ele é que projetou o J++, a versão Microsoft do Java).

Uma delas é essa - para converter uma String contendo um valor decimal para um inteiro, você tem de chamar Integer.parseInt (não tem nada a ver - você não acha isso facilmente na documentação, tem de perguntar para alguém).

Aí ele viu que essa parte de conversão é toda espalhada entre várias classes, e muitas vezes não há a conversão direta de um tipo para outro no Java.

Ou seja, isso fez com que ele inventasse a classe System.Convert no C#. Sabendo que existe essa classe, é fácil converter uma coisa em outra.

This class returns a type whose value is equivalent to the value of a specified type. The supported base types are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime and String.

A conversion method exists to convert every base type to every other base type. However, the actual conversion operation performed falls into three categories:

A conversion from a type to itself simply returns that type. No conversion is actually performed.

A conversion which cannot yield a meaningful result throws InvalidCastException. No conversion is actually performed. An exception is thrown for conversions from Char to Boolean, Single, Double, Decimal or DateTime, and from those types to Char. An exception is thrown for conversions from DateTime to any type except String, and from any type, except String, to DateTime.

Any base type, other than those described above, can be converted to and from any other base type.

An exception will not be thrown if the conversion of a numeric type results in a loss of precision (that is, the loss of some least significant digits). However, an exception will be thrown if the result is larger than can be represented by the particular conversion method’s return value type.

For example, when a Double is converted to a Single, a loss of precision might occur but no exception is thrown. However, if the magnitude of the Double is too large to be represented by a Single, an overflow exception is thrown.

A set of methods support converting an array of bytes to and from a String or array of Unicode characters consisting of base 64 digit characters. Data expressed as base 64 digits can be easily conveyed over data channels that can only transmit 7-bit characters.

Some of the methods in this class take a parameter object that implements the IFormatProvider interface. This parameter can supply culture-specific formatting information to assist the conversion process. The base value types ignore this parameter, but any user-defined type that implements IConvertible can honor it.

É, no java não é tão intuitivo,
deve-se usar:
Integer.parseInt(01) --> para inteiros
Double.parseDouble(0.3) -->para ponto flutuante …
e assim vai