Import statement
==============
=> We can resolve this problem by using fully qualified name "java.util.ArrayList"
l=new java.util.ArrayList();". But problem with using fully qualified name
every time is it increases length of the code and
reduces readability.
=> We can resolve this problem by using import statements.
Types of Import Statements:
There are 2 types of import statements.
1) Explicit class import
2) Implicit class import.
Explicit class import:
Example: Import java.util.ArrayList ;
=> This type of import is highly recommended to use because it improves
readability of the code.
=> Best suitable for developers where readability is important.
Implicit class import:
Example: import java.util.*;
=> It is never recommended to use because it reduces readability of the code.
=> Best suitable for students where typing is important.
=========================================================
"Import statement is totally compile time concept" if more no of imports are there
then more will be the compile time
but there is "no change in execution time".
Difference between C language #include and java language import ?
=======
1. It can be used in C & C++
2. At compile time only compiler copy the code from standard library and placed in
current program.
3. It is static inclusion
4. wastage of memory
Ex : <jsp:@ file="">
import
======
1. It can be used in Java
2. At runtime JVM will execute the corresponding standard library and use it's
result in current program.
3. It is dynamic inclusion
4. No wastage of memory
Ex : <jsp:include >
Note:
In the case of C language #include all the header files will be loaded at the time
of include statement hence it follows static loading.
But in java import statement no ".class" will be loaded at the time of import
statements in the next lines of the code whenever we are
using a particular class then only corresponding ".class" file will be loaded.
Hence it follows "dynamic loading" or "load-on -demand" or "load-on-fly".
JDK 1.5 versions new features :
For-Each
Var-arg
Queue
Generics
Auto boxing and Auto unboxing
Co-varient return types
Annotations
Enum
Static import
String builder
Static import:
This concept introduced in 1.5 versions. According to sun static import improves
readability of the code but according to
worldwide Programming exports (like us) static imports creates confusion and
reduces readability of the code. Hence if there is no
specific requirement never recommended to use a static import.
Usually we can access static members by using class name but whenever we are using
static import it is not require to use class name
we can access directly.
Note:
Two packages contain a class or interface with the same is very rare hence
ambiguity problem is very rare in normal import.
But 2 classes or interfaces can contain a method or variable with the same name is
very common hence ambiguity
problem is also very common in static import.
While resolving static members compiler will give the precedence in the following
order.
1. Current class static member
2. Explicit static import
3. implicit static import
Usage of static import reduces readability and creates confusion hence if there is no specific requirement never recommended to use static import.
What is the difference between general import and static import ?
normal import
============
We can use normal imports to import classes and interfaces of a package.
Whenever we are using normal import we can access class and interfaces directly by their short name it is not require to use fully qualified names.
static import
===========
We can use static import to import static members of a particular class.
Whenever we are using static import it is not require to use class name we can access static members directly.
コメント