Can anyone explain, what exactly happens in the following line?
java.lang.System.out.print("string" + i);
It will print “string” followed immediately by value of integer i, but what is happening here?
What is the significance of every part of this line?
java.langis the package, which isn’t needed with the correctimportstatement.Systemis a class in thejava.langpackage.outis a static public field (requires no bound instance, like a global variable) in theSystemclass which has typePrintStream.This
PrintStreamis connected behind the scenes to anOutputStreamwhich can either be connected to IDE output or console output (via Java Native Interface).The arguments to the
printmethod"string" + iare concatenated using the+operator into a big string, which can be directly fed to the print method.iis a primitiveinttype so is temporarily converted in memory toStringduring concatenation. As for string concatenation optimisation behind the scenes: