All the Classes created in Java imports the classes in java.lang package by default. Hence we wont get the compiler errors while using the Classes of java.lang package. For example - System Class is a class of java.lang package. But we don't get any compiler error to import the java.lang.System; when ever we use System.out.println(" "); statements in our Java programs.
Lets implement and see on Eclipse IDE -
1. Launch Eclipse IDE, create a new Java Project 'Project 53' as shown below -
2. Create a Class 'JavaLangPackage' with main( ) method as shown below -
3. Use System Class of java.lang package by using the System.out.println(" "); statement and observe that no compiler error is displayed to import the java.lang package as shown below -
System Class in System.out.println("Hi"); statement in the above screen has not given any compiler error to import java.lang.System; . Hence all the Java Classes auto imports all the Classes in java.lang package.
Where as when we use the File Class of java.io package in our java programs, the compiler error to import the java.io.File; will be displayed.
Lets implement on Eclipse IDE -
1. In the above program, write the File Class object creation statement as shown below and observe than compiler error with an option to import the java.io.File; is displayed as shown below -
2. Select the 'Import File (java.io)' option from the above error message and observe that java.io.File; is imported in our program and the error got resolved as shown below -
Hence we need to manually import the predefined Classes of Java in other packages like java.io etc. But there is no need to manually import the predefined Classes of Java in java.lang packages, as the Java automatically imports all the Classes of java.lang package in our programs.
Please comment below to feedback or ask questions.