-4

Suppose i want to make the object of an Integer(not int) Class,Since the Integer Class is in another package, i should have to import the java.lang package for creating the object of the Integer Class.But I didn't import the package ,yet compiler doesn't give me an error.

meditat
  • 1,197
  • 14
  • 33
  • `java.lang` is automatically imported for you. – Yoshua Nahar Sep 24 '17 at 10:02
  • For convenience, the Java compiler automatically imports three entire packages for each source file: (1) the package with no name, (2) the java.lang package, and (3) the current package (the package for the current file). – Sergey Kalinichenko Sep 24 '17 at 10:02
  • A nearly exact duplicate on software engineering site: [Why we don't import a package while we use String functions?](https://softwareengineering.stackexchange.com/q/148230/44705) – Sergey Kalinichenko Sep 24 '17 at 10:04
  • 2
    Because of [JLS #7 Packages](http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html): 'A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang.' Duplicate. – user207421 Sep 24 '17 at 10:05

1 Answers1

3

This is defined in Chapter 7 of the Java Language Specification (JLS):

A compilation unit [...] automatically imports all of the public types declared in the predefined package java.lang.

Joe C
  • 15,324
  • 8
  • 38
  • 50