• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

behaviour of import statement

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends
I have simple question for those who knows C/C++. I know most of the people who read and post messages in this forum are with this background only.
Anyways my question is...
in C/C++ you use #include directives to refer the class definitions and function declarations.
in Java we use import <packagename> to use refer the libraries.
What is the actual behaviour of this import statement?
When u say import this package what does it meann exactly?
Siva Prasad
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'import java.util;' means that you can declare say Hashtable without the package name - that's all. If you don't use the import statement, you can still declare the same using java.util.Hashtable. 'import' is not like #include in C/C++ in that it doesn't actually reference anything external. All that matters is that a class is referenced in the classpath with the JRE starts. 'import' does not load classes.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which means that java import does not have the overhead associated with c++ include. There is no penalty for importing unneeded stuff.
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy,
I'm guessing that there is a penalty when you compile the java code because everywhere that the compiler finds Hashtable it needs to replace it with java.util.Hashtable and I don't see any other way of doing that except trying each of the packages specified in the import statements.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark is right,
but the overhead is only for the first time the class is referenced.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Matts Smith:
Mark is right,
but the overhead is only for the first time the class is referenced.


hi matts,
cud u clarify on ur quote. does the include in c/c++ have a overhead evertime the program is run in c/c++ and import is completely different in java. thanx. bye.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there is compile time overhead for both java and C++. There is no Runtime penalty in Java, only the classes used in the application are involved.
It is my understanding that in C++ the included classes are all loaded whether used or not.
 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
Yes, there is compile time overhead for both java and C++. There is no Runtime penalty in Java, only the classes used in the application are involved.
It is my understanding that in C++ the included classes are all loaded whether used or not.


There is a very clear overhead when we talk about #include directive in C++ in the form of unnecessary inclusion of code at the time of compilation. And also we get an .Obj file too which will be used by the linker inorder to generate the .exe file. Even then C++ program runs faster than Java program. When there is no runtime penalty in Java why its so slow?
Any thoughts?
As per my understanding about import statement, its just the way to tell Java to look for those specific classes in the specific directories/packages. Am I rite?
Siva
 
Matts Smith
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well well well.
both #include and import are directive to the compiler.
import is just to tell the compiler where to look for classes. Vector for example is 'replaced' by java.util.Vector.
#include pastes the content of the included file at the place the directive is. usually it's only prototypes so it's just used to make your call to functions defined in another source file or later in the same source file to be 'accepted' (i.e. link might still fail but the compiler will shut up)

hope it helps.
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other key point of #include is to include the definition
of inline functions so they can be inlined in the object.
 
It means our mission is in jeapordy! Quick, read this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic