0% found this document useful (0 votes)
2 views10 pages

Import Statement in Java

The document explains the use of the import statement in Java, which allows programmers to use predefined classes and packages without specifying their full names. It details the syntax for importing single classes and entire packages, along with examples demonstrating their usage. Additionally, it covers commonly used import statements, automatic imports, and answers frequently asked questions about imports in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Import Statement in Java

The document explains the use of the import statement in Java, which allows programmers to use predefined classes and packages without specifying their full names. It details the syntax for importing single classes and entire packages, along with examples demonstrating their usage. Additionally, it covers commonly used import statements, automatic imports, and answers frequently asked questions about imports in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Import Statement in Java

What is import in Java?

The import keyword is used to bring predefined classes and packages into your Java program
so that you can use them without writing their full package names.

Syntax

import package_name.ClassName;

or

import package_name.*;

Why do we use import?

Java has thousands of built-in classes organized into packages.

Instead of writing the complete package name every time, we use the import statement.

Without Import

[Link] sc = new [Link]([Link]);

With Import

import [Link];

class Test

public static void main(String args[])

Scanner sc = new Scanner([Link]);

Using import makes the program:

• Easier to read
• Shorter

• More professional

Types of Import

1. Import a Single Class

Imports only one specific class.

Syntax

import [Link];

Example

import [Link];

class InputDemo

public static void main(String args[])

Scanner sc = new Scanner([Link]);

[Link]("Enter your name: ");

String name = [Link]();

[Link]("Hello " + name);

Explanation

• [Link] → Package name

• Scanner → Class name


• Scanner is used for taking keyboard input.

2. Import All Classes of a Package

Syntax

import [Link].*;

The * means all classes of that package.

Example

import [Link].*;

class Demo

public static void main(String args[])

Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");

int n = [Link]();

[Link]("Number = " + n);

Commonly Used Import Statements

Import Statement Purpose

import [Link]; User input


Import Statement Purpose

import [Link]; Generate random numbers

import [Link]; Array operations

import [Link]; Mathematical functions

import [Link].*; File handling

import [Link]; Date and time

Example 1: Using Scanner

import [Link];

class Student

public static void main(String args[])

Scanner sc = new Scanner([Link]);

[Link]("Enter Age: ");

int age = [Link]();

[Link]("Age = " + age);

}
Example 2: Using Random Class

import [Link];

class RandomDemo

public static void main(String args[])

Random r = new Random();

int num = [Link](100);

[Link]("Random Number = " + num);

Output (Example)

Random Number = 57

Example 3: Using Math Class

class MathDemo

public static void main(String args[])

[Link]([Link](25));

[Link]([Link](2,5));

}
Output

5.0

32.0

Note: Math belongs to the [Link] package, which is imported automatically.

Example 4: Using Arrays Class

import [Link];

class SortDemo

public static void main(String args[])

int a[] = {5,2,8,1,4};

[Link](a);

for(int i=0;i<[Link];i++)

[Link](a[i]+" ");

Output

12458

Packages That Are Automatically Imported


Java automatically imports

import [Link].*;

So you can directly use

• String

• System

• Math

• Integer

• Double

• Character

without writing an import statement.

Example

class Demo

public static void main(String args[])

String s = "ICSE";

[Link]([Link]());

[Link]([Link](64));

Difference Between import [Link] and import package.*


import [Link]; import package.*;

Imports only one class Imports all available classes in the package

Faster to understand Convenient when using many classes

Preferred in professional coding Common in beginner programs

Example

import [Link];

Only Scanner is imported.

import [Link].*;

Classes like Scanner, Random, ArrayList, Date, etc., can all be used.

Important Points to Remember

1. import is written before the class declaration.

2. It allows access to predefined Java classes.

3. [Link].* is imported automatically.

4. Scanner belongs to [Link].

5. Math does not require an explicit import because it is in [Link].

6. * imports all classes from a package, not subpackages.

Frequently Asked Questions (ICSE/CBSE)

Q1. What is an import statement in Java?

Answer: An import statement allows a Java program to use classes from another package
without writing the full package name.

Q2. Which package is imported automatically?

Answer: [Link].*

Q3. Which package contains the Scanner class?

Answer: [Link]
Q4. Which keyword is used to import a package?

Answer: import

Q5. Where should the import statement be written?

Answer: At the beginning of the program, after the optional package statement (if any) and
before the class declaration.

Practice Questions

A. Fill in the Blanks

1. The keyword used to include a package is __________.

2. Scanner belongs to the __________ package.

3. The package imported automatically is __________.

4. Random class belongs to __________ package.

5. Math belongs to __________ package.

Answers:

1. import

2. [Link]

3. [Link]

4. [Link]

5. [Link]

B. Write Output

1.

import [Link];

class Demo

{
public static void main(String args[])

Random r = new Random();

[Link]([Link](10));

Answer: Prints a random integer from 0 to 9 (output varies each run).

2.

class Demo

public static void main(String args[])

[Link]([Link](3,2));

Output

9.0

You might also like