Just a quick question what would be more expensive in Java?
double test = 5;
double test1 = 5;
or
double test = 5;
double test1 = test;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Neither. Java has a very good optimiser that will cause the exact same code to be generated in this example.
The compiler looks at the assignment
double test1 = test;and can work out that at this pointtestis a constant equal to5and completely optimise the assignment away.This is also why you shouldn’t be afraid to expand out numeric values, ie.
That entirely aside, this is very much a case of micro-optimisation that will never return anything worth noting. Worry about that network connection that’s holding up the works for several seconds instead.