Java : Accessing a class within a package, which is the better way?

219,358

Solution 1

There is no performance difference between importing the package or using the fully qualified class name. The import directive is not converted to Java byte code, consequently there is no effect on runtime performance. The only difference is that it saves you time in case you are using the imported class multiple times. This is a good read here

Solution 2

No, it doesn't save you memory.

Also note that you don't have to import Math at all. Everything in java.lang is imported automatically.

A better example would be something like an ArrayList

import java.util.ArrayList;
....
ArrayList<String> i = new ArrayList<String>();

Note I'm importing the ArrayList specifically. I could have done

import java.util.*; 

But you generally want to avoid large wildcard imports to avoid the problem of collisions between packages.

Solution 3

They're equivalent. The access is the same.

The import is just a convention to save you from having to type the fully-resolved class name each time. You can write all your Java without using import, as long as you're a fast touch typer.

But there's no difference in efficiency or class loading.

Solution 4

As already said, on runtime there is no difference (in the class file it is always fully qualified, and after loading and linking the class there are direct pointers to the referred method), and everything in the java.lang package is automatically imported, as is everything in the current package.

The compiler might have to search some microseconds longer, but this should not be a reason - decide for legibility for human readers.

By the way, if you are using lots of static methods (from Math, for example), you could also write

import static java.lang.Math.*;

and then use

sqrt(x)

directly. But only do this if your class is math heavy and it really helps legibility of bigger formulas, since the reader (as the compiler) first would search in the same class and maybe in superclasses, too. (This applies analogously for other static methods and static variables (or constants), too.)

Share:
219,358
Jomia
Author by

Jomia

Android Developer

Updated on March 20, 2020

Comments

  • Jomia
    Jomia about 4 years

    If I access a class within a package using fully qualified name, without importing it, whether it saves any memory?

    Using fully qualified class name :

    java.lang.Math.sqrt(x);
    

    Import package :

    import java.lang.Math;
    
    Math.sqrt(x);
    

    which is the better way : import the package or access using fully qualified name?

    Thanking you..

  • amara
    amara about 13 years
    +1. also personally I like to import java.io.* because its classes have long, uncommon names. similarly, i'd never import java.util.* =)
  • Hillcow
    Hillcow over 5 years
    except ArrayList only works if you import java.util.List too, because java.util.ArrayList inherits from java.util.List. Correct me if Im wrong, Im completely new to Java.