0

When I write the following code:

System.out.println("Hello");

Will the println() method replaced by its definition at any time (during compilation or execution)?

If no, then how does the JVM comes to know what functionality it will do?

Is the information about the println() or any in-built method stored in any form in the class file ?

UPDATE: As @Andreas suggested a link, the answers I read there suggested that it is mostly upto the compiler whether to make it inline or not.

If it is so for those methods which are made inline by compiler, can we run them without having the class files of System and PrintStream classes ?

A_J
  • 977
  • 4
  • 16
  • 44
  • No - All of the relevant class files have to be on the classpath at runtime. – Chris Martin Sep 10 '15 at 13:40
  • @Chris Martin, that means the class file of class System and PrintStream will be present there at runtime. It means it is taking help of class file of class System and PrintStream present in rt.jar ? – A_J Sep 10 '15 at 13:51
  • @Andreas, I referred that link suggested by you. But John has said there that the compiler will decide about it. But in IDEs like Eclipse, the code is compiled as we are typing and sometimes saving. At that time we never see any of the in-built methods converted to its definition. Please clarify. – A_J Sep 10 '15 at 14:07

2 Answers2

0

The compiler will compile your code to byte-code that will perform the desired function when executed by the JVM. Beyond that, the JVM or the compiler can conspire to do whatever they like.

If, for example, the JVM is running in an embedded environment the compiler may even completely remove this line of code and generate no byte-code at all because it knows that there is not such thing as System.out on the embedded system.

In summary - it's impossible to say without specific reference to which compiler is to be used and which JVM is the target.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • Will println() method written in my class be replaced by the code written in PrintStream class for the method println() ? Or that code will just be referenced by JVM i.e. I must have the class files of PrintStream and System classes ? – A_J Sep 10 '15 at 13:58
  • @A_J - Perhaps - but not necessarily. As I said, the compiler can, if it wishes, completely remove the whole line if it knows that there will be no effect on the state of the JVM or it's outputs - e.g. `System.out` has no function. – OldCurmudgeon Sep 10 '15 at 14:28
  • My point is that if I again execute my class file, can I do it without having System.class and PrintStream.class in the classpath ? and what do you mean by saying "System.out has no function". Please clarify. – A_J Sep 10 '15 at 14:36
  • 1
    Yes, you must have the relevant class files; yes, those live in rt.jar. Everything else is just dancing around the point. – Louis Wasserman Sep 10 '15 at 16:03
  • @Louis Wasserman, Thanks for the clarification – A_J Sep 11 '15 at 12:52
0

The method definition is in class relevant class file ( in this case System) class which is always in class path. When you compile , compiler will put instructions and pointers to "invoke" the definition of method. While executing JRE will follow those instructions and invoke the method definition.

vishal
  • 32
  • 2
  • 8
  • But once after compiling, can I run my own class .class file without needing the class files of System and PrintStream ? Or will I need them everytime I execute my code ? – A_J Sep 10 '15 at 14:09