简体   繁体   中英

Java: multiple conversions in a single line

Hello StackOverflow community.

Im making my first steps in Java and OOP. I would like to know ¿Is there a way to convert and argument in to another type of variable, then convert it in to an object in a single line of operation?.

Thank you for your time and help.

This is the code that isnt mine, it is from a book im reading:

public class Changer {
    public static void main(String[] arguments) {
        if (arguments.length > 0) {
            System.out.println("The original value: "
                + arguments[0]);
            Float num1 = new Float(arguments[0]);
            float num2 = num1.floatValue();
            int num3 = (int)num2;
            System.out.println("The final value: " + num3);
        }
    }
}

Boann Answer: float is faster than Float. However, since this particular code executes only once, it will take no time either way. You should only worry about performance of those lines of code that run millions, billions or trillions of times in your program, or when you've benchmarked and determined that there is a real bottleneck. Anything else is wasting your time as a programmer, while saving no noticeable time in the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM