Import statement
/*
class Test{
public static void main(String... args){
ArrayList arr=new ArrayList()
}
}
compile Time Error-:
cannot find symbol : class ArrayList
location : class Test
ArrayList arr=new ArrayList();
^
*/
- we can solve this problem by using a fully qualified name java.util.ArrayList arr=new java.util.ArrayList();
- the problem with using a fully qualified name every time is it increases the length of the code and reduces readability.
- we can solve this problem by using import statements.
- whenever we are writing import statements it is not required to use fully qualified names we can use sort name directly.
Example-:
import java.util.ArrayList;
class Test{
public static void main(String... args){
ArrayList arr=new ArrayList()
}
}
hence import statement as typing shortcut.
case 1-: types of import statements .
there are two type of class import statements Explicit and implicit.
Explicit class Import-:
- it is highly recommended to use explicit class import because it improves the readability of code.
- best suitable for the high-tech city where readability is important.
Implicit class Import-:
- not recommended to use because it reduces the readability of the code.
- best suitable for where typing is important.
case 2-:which of the following import statement is meaningful.
import java.util.ArrayList;
import java.util.ArrayList.*;
import java.util.*;
import java.util;
case 3-: consider the following code class myObject extends java.rmi.UniCastRemoteObject
the code compiles fine even though we are not writing an import statement because we used a fully qualified name .
class myObj extends java.rmi.UniCastRemoteObject{
}
Note-: whenever we are using the fully qualified name it is not required to write an import statement similarly whenever we are writing an import statement it is not required to use the fully qualified name.
case 4-: Ambiguity problem
Note-:even in the case of list also we may get the same ambiguity problem because it is available in both util and awt packages.
import java.util.*;
import java.sql.*;
class Test{
public static void main(String[] args){
Date date=new Date();
}
}
compiler time error : reference to date ambiguity
case 5-: while resolving the class name compiler will always gives precedence in the following order.
- Explicit class import
- class present in the current working directory (default package)
- implicit class import
example-:
import java.util.Date;
import java.sql.*;
class Test{
public static void main(String... args){
Date d=new Date();
System.out.print(d.getClass().getName());
}
}
output-:
java.util.Date;
in the above example, the util package got to consider.
case 6-: whenever importing a java package all classes and interfaces present in that package by default are available but not sub package classes if we want to use subpackage class compulsory we should write import until sub package level.
eg-:
to use pattern class in out program which import statement is required.
import java.*;
import java.util.*;
import java.util.regex.*;(correct);
no import required
- java.lang package
- default package(current working directory)
case 8-: import statement totally compiles time-related concept if more number are import then more will be compiled time but there is no effect on execution time(Run time).
case 9-: difference b/w c language #include and java language import statement.
in the case of c language #include all input/output headers will be loaded at the beginning only at translate time hence it is static include.
but in the case of java import statement no .class file loaded at the beginning whenever we are using a particular class then only the crossponding .class file will be loaded this is like dynamic include are load on demand of load on the fly