Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Structure vs Class

 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings fellow ranchers.

What is the difference between a structure and a class ? Also specify where and when would you use a structure instead of a class
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, structures are something that don't exist in Java. So one answer to "when would you use a structure" would be "when you're using a language that has them" .

Exactly what it means probably varies a bit from language to language. In C, which is the main language I'd associate with structs, a struct is a type that has fields made up of other types. It's similar to a class in that respect, but it doesn't have methods or the idea of private state.
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif Asif wrote:What is the difference between a structure and a class ? Also specify where and when would you use a structure instead of a class

I wonder if you're confusing "structure" and struct, which is a keyword used in C and C++.

If so, the basic difference is: methods. A Java class binds data, constructors and methods.

However, you will almost never hear the term "structure" used in that context in Java, because there is no such thing as a struct in Java.

Winston
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree , but what about the idea of actually coding a structure implementation in Java via class . Would that be feasible ? As far as the possibility is concerned , I dont think we would we able to get a 100% implementation of a strucuture in Java
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif Asif wrote:I agree , but what about the idea of actually coding a structure implementation in Java via class . Would that be feasible ? As far as the possibility is concerned , I dont think we would we able to get a 100% implementation of a strucuture in Java

And why would you want one? A Java class is more that a struct will ever be, so why would you want an inferior construct simply because it exists in another language?

Winston
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well at the moment, the things coming to my mind are

  • having a light data type that resides in the stack
  • will not be changed after its creation
  • since it resides in stack , then relatively faster CRUD operations on it
  • prevent casting
  •  
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In Java you could only build it out of a class. So you're right, you couldn't get it to reside on the stack. Not changing it after creation is easy - if you want an immutable class just make the state private and don't give it mutators. I'm not sure why preventing casting would be relevant, but I'm probably missing something.
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10492
    64
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Saif Asif wrote:Well at the moment, the things coming to my mind are ...

    Like Matthew, other than (possibly) being stack-based, I don't see what any of the other things buy you (and in fact most of them don't apply to C structs anyway): memory is cheap, mutability is up to you, CRUD wouldn't appear to be an issue since you don't want it to be updatable anyway, and you're not supposed to be worried about memory - that's the JVM's job. Not to mention how some of the Java keywords would apply:
    for example: what would a volatile struct mean?

    Winston
     
    Saif Asif
    Ranch Hand
    Posts: 440
    Eclipse IDE Hibernate Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    what would a volatile struct mean?
    Yes this would definitely make no sense at all

    I see your point . Good discussion fellows . Guess I'll mark this case closed.
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Pie
    Posts: 15436
    41
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Saif Asif wrote:Well at the moment, the things coming to my mind are

  • having a light data type that resides in the stack
  • will not be changed after its creation
  • since it resides in stack , then relatively faster CRUD operations on it
  • prevent casting

  • 1. In Java, objects (instances of classes) are always created on the heap, and not on the stack*. There is no way to create an object-like thing on the stack explicitly.
    2. You can make your classes immutable, and very often it's a good idea to do so.
    3. The JVM is very good at optimizing code. One of the things it does is inlining. Calling a getter or setter on an object is most of the time just as cheap as accessing the field directly.
    4. There's no way you can prevent casting.

    * except in special circumstances, when the JVM does escape analysis - a sophisticated optimization technique.

    It sounds like you have a C++ mindset, where you're worrying about low-level optimization of your code. In Java, you normally don't need to think about these things. The JVM does a lot of sophisticated optimization for you to make your code run fast.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Saif Asif wrote:Well at the moment, the things coming to my mind are

  • having a light data type that resides in the stack


  • In what way would it be 'light"? It would be made out of a class, so it would be at least as heavy as a class.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic