I was wondering if there is a difference in efficiency or performance when working with temporary variables inside constructors or methods.
Here is an example
// Get bufferedImage
BufferedImage trayIconImage = ImageIO.read(getClass().getResource("/images/D.gif"));
// Calculate width
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
// Create TrayIcon
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));
So I have a temporary variable "trayIconWidth"
But I also could do this like so:
//Get bufferedImage
BufferedImage trayIconImage = ImageIO.read(getClass().getResource("/images/D.gif"));
// Create TrayIcon
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(new TrayIcon(trayIconImage).getSize().width, -1, Image.SCALE_SMOOTH));
So basically I am skipping the step of getting the int value for the width.
There are a lot of examples where you can skip multiple temporary variables and I know that it has to do with readability and stuff. But I would like to know if there is any difference in speed, performance, efficiency, or ram usage.
Does the (e.g. java) garbage collector handle this kind of temporary action?
EDIT 1:
I compared the bytecode of two basic snippets. They are different.
second

So this means the pc has to do one or more instructions to get the execution done - am I right?
regards Nur1