Eclipse how to convert automatically?

Let's say I have:

p.size = packetLine[0]; // where packetLine is of type String[] and element at that position is number represented by String


I don't want to always write


 or reverse



Eclipse gives suggestions to fix the error, can I do this to convert the values?

It is currently proposed to change the type. I would like the third sentence to "Convert to Int" or "Convert to String";

This is especially annoying when I am iterating a thousand times, I could just introduce my own method to convert, like toInt or toString2, but in a build solution it would be better.


source to share

3 answers

  • What you ask for does not quit. Java uses the term casting for two operations:

    • Converting a numeric primitive to another numeric primitive that approximates the original value as closely as possible, for example. int

      before double


    • Storing the contents of an object's reference variable into a more specific reference variable if and only where the referenced object can actually be stored.

  • What you are asking for is a conversion from String to primitive type and vice versa. It usually doesn't make sense to provide shortcuts for this. There are several ways to do this, and none is universal. For example. a numeric string can be interpreted as an octal or hexadecimal number, or it float

    can be converted to a String with a different number of floating point digits, depending on the required precision ...


You might be able to make your life easier with repetitive steps by creating custom editor templates in Eclipse . Editor templates are available along with the rest of the content support suggestions when you click Control+Space

. Template creation is not always straightforward, but in some cases it can be quite useful.



Yup, you are stuck with either your class method (toInt () as you said) or a static utility. (Which is good, if you prefer not to have many tries / catch, you can choose 0 for malformed integers. Or something appropriate. I like 0 instead of throwing.)

The only "inline" casting turns anything into a String (using the toString () method) during concatenation. How,

String s = "as a string, it is: " + anything; // and null becomes "null".


Sometimes you see the following:

String s = "" + something; // shorthand


(Numeric types are also implicitly used for you, but basically you should be type safe and all.)



You cannot add implicit casts to java as the language does not support it, outside of primitive types / class hierarchies.



All Articles