Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import statement byte code significance

Lets say , there are some import statements in a class. When the byte code is generated for that class, what happens to these import statements.

If the import statements are ignored during runtime, how are the dependencies on that classes methods resolved during runtime.

like image 892
java_geek Avatar asked Mar 13 '12 08:03

java_geek


People also ask

What is the significance of byte code?

Bytecodes are non-runnable codes that rely on the availability of an interpreter, this is where JVM comes into play. It is a machine-level language code that runs on the JVM. It adds portability to Java which resonates with the saying, “write once, read anywhere”.

What is the significance of import statement in Java?

Import statement in Java is helpful to take a class or all classes visible for a program specified under a package, with the help of a single statement. It is pretty beneficial as the programmer do not require to write the entire class definition. Hence, it improves the readability of the program.

What is the purpose of the import statement?

An import statement tells Java which class you mean when you use a short name (like List ). It tells Java where to find the definition of that class. You can import just the classes you need from a package as shown below.

What is the advantage of using import statements?

The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any).


2 Answers

The purpose of import statements is just to make life easier for the human readers (and authors) of the code. Thus they are replaced by references to the fully qualified class/method names in the bytecode. And unused import statements are ignored.

like image 145
Péter Török Avatar answered Sep 22 '22 13:09

Péter Török


import in Java is just a shorthand

so that if you import java.util.* you don't have to write java.util.ArrayList in your code but can write ArrayList

like image 42
Hachi Avatar answered Sep 20 '22 13:09

Hachi