About import command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ponvijaya
    New Member
    • May 2007
    • 19

    About import command

    Hi all,

    We are using import command as

    import java.io.*, import java.util.* etc in the program.

    Consider i am having a class A.

    Is there any difference related to Memory consumptions for class A while writing import command as
    import java.util.HashM ap ; rather than
    import java.util.*;
    in the case import java.util.HashM ap i am importing only the class HashMap where as in the case import java.util.* , i am importing all the classes present in util package.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ponvijaya
    Hi all,

    We are using import command as

    import java.io.*, import java.util.* etc in the program.

    Consider i am having a class A.

    Is there any difference related to Memory consumptions for class A while writing import command as
    import java.util.HashM ap ; rather than
    import java.util.*;
    in the case import java.util.HashM ap i am importing only the class HashMap where as in the case import java.util.* , i am importing all the classes present in util package.
    No there's no difference in memory e.t.c.
    Importing a class is different from loading a class. The import is there just to help you type shorter names for classes when refencing them from their packages.

    Comment

    • odefta
      New Member
      • Jul 2007
      • 18

      #3
      There is not a memory problem.

      But importing classes can lead to ambiguities. A class name is ambiguous if it occurs in two packages that are imported by wildcards. For example, suppose a program contains the imports

      import java.awt.*;
      import java.util.*;

      The List class exists in both import statements.
      If use List in your program, which List trully will use?

      Comment

      • odefta
        New Member
        • Jul 2007
        • 18

        #4
        Import only tells the compiler where to look for symbols.

        The search for names is very efficient so there is no effective difference between
        import java.* or import java.Class

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by odefta
          There is not a memory problem.

          But importing classes can lead to ambiguities. A class name is ambiguous if it occurs in two packages that are imported by wildcards. For example, suppose a program contains the imports

          import java.awt.*;
          import java.util.*;

          The List class exists in both import statements.
          If use List in your program, which List trully will use?
          Yep. The compiler will complain if you try to use a non fully qualified List after those imports. You'd need to prefix the full package name if you want to use ambiguous classes.
          Last edited by r035198x; Jul 9 '07, 01:06 PM. Reason: corrected the grammer

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Older versions of javac eagerly imported entire class definitions for every class
            in a package that was merely mentioned in your class. The newer versions
            simply register the fact that every class in that package should be available for
            scrutinizing (loading the class defintion) it by the compiler. In the old days
            avoiding the '*' thingy was a compilation speedup.

            That is not the case anymore; most if not all IDEs can create single class
            import directives for you and it is the recommended way of importing classes
            (it reduces the chance of ambiguity in case of class name clashes).

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              Older versions of javac eagerly imported entire class definitions for every class
              in a package that was merely mentioned in your class. The newer versions
              simply register the fact that every class in that package should be available for
              scrutinizing (loading the class defintion) it by the compiler. In the old days
              avoiding the '*' thingy was a compilation speedup.

              That is not the case anymore; most if not all IDEs can create single class
              import directives for you and it is the recommended way of importing classes
              (it reduces the chance of ambiguity in case of class name clashes).

              kind regards,

              Jos
              One of the joys of being younger

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by r035198x
                One of the joys of being younger
                Statement: I'm not old :-P

                kind regards,

                Jos ;-)

                Comment

                • praveen2gupta
                  New Member
                  • May 2007
                  • 200

                  #9
                  Hi

                  import java.io.*,
                  import java.util.* etc in the program.

                  Consider i am having a class A.
                  Is there any difference related to Memory consumptions for class A while writing import command as
                  import java.util.HashM ap ; rather than
                  import java.util.*;

                  import java.io.* is default import statement , you need not to write it. While
                  in second case There is no difference in Memory consuption. Compiler looks these paths at the time of compilation only.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by praveen2gupta
                    Hi

                    import java.io.*,
                    import java.util.* etc in the program.

                    Consider i am having a class A.
                    Is there any difference related to Memory consumptions for class A while writing import command as
                    import java.util.HashM ap ; rather than
                    import java.util.*;

                    import java.io.* is default import statement , you need not to write it. While
                    in second case There is no difference in Memory consuption. Compiler looks these paths at the time of compilation only.
                    I'm not entirely sure what you're talking about here.
                    What do you mean by " import java.io.* is default import statement , you need not to write it."? If you want to use any classes/interfaces in the io package you have to import the package.

                    Comment

                    Working...