java中导入包是干嘛的,Java中的类导入和包导入之间有什么区别吗?

In Java we can either import single classes as well as the whole set of classes (a package).

As an example

import java.util.*

includes

import java.util.ArrayList;

import java.util.Date;

import java.util.Enumeration;

import java.util.HashSet;

import java.util.Hashtable;

import java.util.List;

import java.util.Iterator;

import java.util.Map;

Other than the length of the code, are there any specific advantages of using the each approach in any manner? Memory allocation? Performance?

解决方案

There is no performance or memory allocation advantage to either -- they both will compile to the same bytecode.

The import statement is to tell the compiler where to find the classes that the source code is referring to.

However, there is an advantage to importing only by classes. If there is a class with the exact same name in two packages, there is going to be a conflict as to which class is being referred to.

One such example is the java.awt.List class and the java.util.List class.

Let's say that we want to use a java.awt.Panel and a java.util.List. If the source imports the packages as follows:

import java.awt.*;

import java.util.*;

Then, referring to the List class is going to be ambigious:

List list; // Which "List" is this from? java.util? java.awt?

However, if one imports explicitly, then the result will be:

import java.awt.Panel;

import java.util.List;

List list; // No ambiguity here -- it refers to java.util.List.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值