Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

imports and performance

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will having lots of classes to import slow down performance?


------------------
What's this H2SO4 doing in my fridge?? ( thud )
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, no. It might slow down compilation a little, but once a class has been compiled and loaded it just references the other classes it needs directly, regardless of how many import statements were in teh source file.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++ when you do an include, all the source is pulled into your code. That is not the case with java. Only the classes that you actually USE are pulled into the code. An import is more like a search path than a copy.
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cindi is correct the import statement just acts like a search path to reach to the class required.
If you try to decompile a file you will notice that * in the import statement is replaced by the exact class names and the irrelevant import statements are deleted.
test.java
import java.util.*;
import java.applet.*;
class test
{
public static void main(String []args)
{
Date d = new Date();
System.out.println(d.getTime());

Vector v = new Vector();
System.out.println(v.capacity());
}
}
decompiled file test.class
import java.io.PrintStream;
import java.util.Date;
import java.util.Vector;
class test
{
test()
{
}

public static void main(String []args)
{
Date date = new Date();
System.out.println(date.getTime());

Vector vector = new Vector();
System.out.println(vector.capacity());
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic