I had a doubt,
Any expression assignment operation is a compile time or runti开发者_StackOverflowme phenomenon ?
e.g Foo f=new Bar();
Thanks.
If you compile the following:
int x = 34 + 45;
then the expression that you are assigning will be evaluated at compile time because it involves only constant basic types. In most other cases, including your example, the expression will be evaluated at runtime.
I don't know what exactly is your doubt, but I think that during compilation textual representation of a program is translated into machine-understandable form (in Java case it'd be Java byte code). The same happens with operations like assignments. In runtime, such operations are executed. So to precisely answer your question, the assignment operation is both compile and runtime phenomenon (certain actions are taken in both cases - in a case of compilation they're done by Java compiler and in runtime the're done by JVM).
For instance,
class Foo { }
class Bar { }
Foo f = new Bar();
This code will be rejected by compiler. It will yell at you that you are trying to assign incompatible types.
Its runtime
Foo f
would be referring to Object
created runtime of Bar
at runtime.
There are compile time check for it. Bar
must be a Foo
for this to compile successfully
Its both.
Compile time - an assignment is analyzed at compile time for type compatibility, for the injection of code to do conversion/boxing/unboxing, etc. Even some flow analysis can affect an assignment, e.g. variable and its assignment can be optimized away completely.
Run time - the actual assignment of a value, i.e. changing the bits in the variable's memory location, of course, happens at run-time.
精彩评论