• 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

Struct in Java?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was asked in an interview what are structs in Java.I had never heard so replied saying sorry I don't know. Later when I checked I did not find any struts in Java but it is there in C++. I am thinking that interviewer might have meant equivalent of structs in Java. Does Java have it or its equivalent ?
Thanks
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look in Joshua Bloch's Effective Java pages 75‑76 (first (2001) edition only).
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I will check further.May have been there in old version of Java and removed.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . . May have been there in old version of Java and removed.

How many things have been removed from Java®? That section has been removed from the book in later editions.
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The logical equivalent of a struct in Java is a Java Class. There is no physical equivalent.

In C, a struct defines not only a data association, but also memory mapping. That is, knowing the characteristics of a particular compiler for a particular machine and OS, I can compute to the byte how much memory a struct will occupy and where, within that struct, each of its data elements are. You cannot do that with a Java class.

C++, incidentally gets a little funny. Unless someone's tightened definitions, the only difference between a C++ struct and a C++ class is the initial scope of its member declaraions. A struct starts out with public scope,  a class starts out with private scope. But C++ classes (including structs) can define virtual resources, and if they do, the location and composition of those resources may make it harder to predict storage sizes and offsets.
 
Ranch Hand
Posts: 82
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Java definitively has no structs But what you describe here looks like a JavaBean kind of class
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to look up records:
https://dzone.com/articles/a-first-look-at-records-in-java-14
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic