Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

import vs include  RSS feed

Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the impression that the import or reserved word is similar to the include statement used in C or is include also used in java?
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Kevin,
As you stated, Java's 'import' looks the same as C/C++'s #include (Java does not use include). But there are a few (some might say major, some might say minor) differences.
First of all, Java's import statement doesn't include the file, as #include in C/C++ does, it just tells the compiler where to look for its class references. Therefore the import statement isn't used to import/include C-style header files in which you declare all the used functions, as the #include statement in C/C++ is used most of the time. Note that this isn't needed as, unlike C/C++, Java doesn't make a distinguishment between declaring a function and defining the function, as Java class files simply acts as both the declaration and the implementation of the object.
The second difference between Java's import statement and C/C++'s #include is that the latter statement is parsed by a preprocessor, transforming the statement into the equivalent C expressions/statements, thus actually adding the source code of the included file into the including file before compilation. Java does not have this kind of preprocessing because, in fact, you doesn't really need it.
Hope this helped,
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in Java 1.4 and lesser releases, the import statement just tells the compiler where to find other classes. It looks like in Java 1.5, import statements will also be used to tell the compiler where to find static methods, if I've understood the brief readings I've done on the subject.
Tim B.,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!