• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java import statment

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I understand so far, java import statements act a lot like namespace statements in c++. It doesn't actually import anything, it just saves you from typing the long legal name of things.
It also seems like precise imports ( e.g., ) is like using specifics in namespace (e.g., as opposed to )

What I don't get is why the wild card doesn't cover things that look like they are lower in the hierarchy.

If I



Why must I also

?

Why isn't the event picked up by the wild card?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The * only represents one level of hierarchy.
 
Rancher
Posts: 4801
50
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:The * only represents one level of hierarchy.



In fact, I would say there is simply no concept of hierarchy in packages, from the compilers (and JVMs) point of view.

Yes, java.awt.event looks like a lower level of java.awt, but as far as Java is concerned they are completely unrelated. They are just organised like that for the convenience of the developer.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid conflating Java® and C++. They are different languages, even though their syntax looks so similar.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:
In fact, I would say there is simply no concept of hierarchy in packages, from the compilers (and JVMs) point of view.
but as far as Java is concerned they are completely unrelated. They are just organized like that for the convenience of the developer.



Maybe, you are right, but nevertheless, they are a folder structure and hence a hierarchy from a developers point of view.
But, look at it from this point of view, its simple to think of them as folders and simply say.. oh i need a class from folder1 only and not its children.
I have known people who even frown at using "*" wildcards in the imports and who insists in importing only those classes which are used in code (don't ask why )

Putting it in simple words, Its a simple folder structure and the wildcard lets you access only one level deep.


Syntactically comparing 2 languages isn't going to get you anywhere Compare them conceptually.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:. . . don't ask why . . .

I don't need to ask why, because I already know why. Those people know good programming style.

It enables you to see exactly which classes you are using. It reduces the risk of collisionsYou can get other such problems if new classes are added after you wrote the code and you try to compile it again, but I can't remember the full details. Having to write all the imports probably encourages you to shorten your .java files so as to keep all the imports on one page
 
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

salvin francis wrote:Maybe, you are right, but nevertheless, they are a folder structure and hence a hierarchy from a developers point of view.


Yes, but the Java compiler's point of view is what counts. It might look like a hierarchical structure to you, but that's not how the compiler treats packages.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:It might look like a hierarchical structure to you, but that's not how the compiler treats packages.


Honestly, I haven't given it much thought about how a compiler treats it. However, I still see that even after compiling, it still maintains a folder hierarchy with .class files. In fact, compilation too needs you to specify package name and the compiler goes down each folder to compile the respective classes.

Which brings me to a question, why not have a flat structure after compiling as 1.class, 2.class ... and so on ?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the same reason as I mentioned earlier about Timer.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:For the same reason as I mentioned earlier about Timer.



Theoretically you could name the class files com.blah.foo.MyClass.class.

Make the directories a bit crowded, though.
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please avoid conflating Java® and C++. They are different languages, even though their syntax looks so similar.



Its not conflating, the practicalities are extremely similar. You can use ANYTHING you use after import statement without the import statement if you use it long name. Its very much like name spaces in C++. Why get egocentric about languages? It like, "We don't call the pointers in java because they could be anything, but they probably are pointers" This from Head first Java talking about how objects are referenced. Mysticism and programming is not a great match.
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Bear Bibeault wrote:The * only represents one level of hierarchy.



In fact, I would say there is simply no concept of hierarchy in packages, from the compilers (and JVMs) point of view.

Yes, java.awt.event looks like a lower level of java.awt, but as far as Java is concerned they are completely unrelated. They are just organised like that for the convenience of the developer.




In that case they should have used a different symbol than a wild card, because as long as I've been using computers the wild card has been a mask for whatever comes after.. The fact that its eccentric doesn't make it a good thing or intuitive. But theres a reason it doesn't make any sense to a new java programmer. It goes against decades of practice.
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have known people who even frown at using "*" wildcards in the imports and who insists in importing only those classes which are used in code (don't ask why )



Because you can wind up with an error like "MouseEvent is ambiguous" when you are using imports that contain things that are named the same. In which case, you need to use their long name to fix the ambiguity.
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Bear Bibeault wrote:The * only represents one level of hierarchy.



In fact, I would say there is simply no concept of hierarchy in packages, from the compilers (and JVMs) point of view.

Yes, java.awt.event looks like a lower level of java.awt, but as far as Java is concerned they are completely unrelated. They are just organised like that for the convenience of the developer.



In that case, periods were an horrendous choice, because they certainly imply a hierarchy.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shawn Lau wrote:In that case they should have used a different symbol than a wild card, because ...


Not really. In many systems a single * represents a single level, while ** is used to indicate "whatever comes after". In any case, not supported by the import statement and unlikely to change.
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Shawn Lau wrote:In that case they should have used a different symbol than a wild card, because ...


Not really. In many systems a single * represents a single level, while ** is used to indicate "whatever comes after". In any case, not supported by the import statement and unlikely to change.



To be more specific, when it follows a period it matches anything that follows - like load("mypic.*"); That's just the common use of the wild card and to use it differently is eccentric.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then there are a lot of "eccentric" systems out there.
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Then there are a lot of "eccentric" systems out there.



So it seems. Its just hard to accept some of it with a straight face. When references to objects are referred to as remote controls because we don't know if pointers are involved ( really? There's a Geni guiding us to them?), it hard to keep from laughing.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shawn Lau wrote:Its just hard to accept some of it with a straight face.



When I'm learning a new language I find that it's better to accept the language as it is. That goes for both computer languages and human languages. Complaining about language features is unproductive (as they aren't going to be changed) and just gets in the way of the learning process.
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Shawn Lau wrote:Its just hard to accept some of it with a straight face.



When I'm learning a new language I find that it's better to accept the language as it is. That goes for both computer languages and human languages. Complaining about language features is unproductive (as they aren't going to be changed) and just gets in the way of the learning process.



I understand that. There's much I like about it. I find the mystic parts of it amusing. I've never ran into that before in a computer language. I'll be the first to cry "YEA" when they can figure out a way to retrieve data from a computer without using an address register. But I suspect we are chained to addresses. I'm not complaining about language features because I love it. Criticizing some of the design shouldn't make me a heathen. This isn't religion, its science. If someone calls something a function instead of a method, so what? Whats the practical difference between a method and a function? Reference - pointer? Any difference? The difference is not on a science level but a philosophical level. On a register level, they work exactly the same. Is there a technology on the horizon where computers are going to be register free? That's the part I was criticizing, like it somehow looks different than any other language when its spit out into assembly.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shawn Lau wrote:Why get egocentric about languages? It like, "We don't call the pointers in java because they could be anything, but they probably are pointers" This from Head first Java talking about how objects are referenced. Mysticism and programming is not a great match.


I think you're confusing "egocentric" with correct. Java references are NOT pointers and hopefully never will be - indeed, one of the foundation stones of the language was to get rid of pointers.
The fact that they might be implemented as pointers in a realm of the language (the JVM) that most Java programmers never need to worry about is neither here nor there, but they could just as easily be '**'s, or some entirely different thing altogether, because AFAIR the JVM spec doesn't specify how they should work; only what they do.

So, if you feel happier thinking of them as pointers, feel free; but don't try to browbeat us into calling them pointers because they ain't.

Winston
 
Shawn Lau
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"Java references are NOT pointers and hopefully never will be - indeed, one of the foundation stones of the language was to get rid of pointers. "



Well what do you think they are? How do you suppose the CPU finds the data without using an address register?
 
reply
    Bookmark Topic Watch Topic
  • New Topic