Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

import statement takes resources (memory)?

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've been wondering about this:

Let say I only need java.net.URL and java.net.URLConnection.

Is it going to make a difference (resource wise) for these two options:

1st option:

2nd option:


Will the 2nd option import everything into memory? How does import statement work?
 
Marshal
Posts: 74059
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the compiler sees "URLConnection" it recognises it as a type (because of its context in the code) but doesn't know where to find it. So the import statement tells it where to look. There is no difference in memory requirements between the two styles, but the 1st option is preferred because
  • There is less risk of collisions (eg Timer; there are two Timer classes available)
  • It is easier to review the code and see what imports have been used.
  •  
    Marshal
    Posts: 22458
    121
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Import statements are a source-only mechanism. The compiler uses them to translate non-fully qualified class names like URLConnection to fully qualified class names like java.net.URLConnection. The generated byte code therefore no longer has any import statements.
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You could compile your source file with the first way of specifying the imports, save the resulting class file, and then with the second way of specifying the imports. Compare the two class files and you will see that they are exactly the same.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic