• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Struct in Java?

 
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • 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: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in Joshua Bloch's Effective Java pages 75‑76 (first (2001) edition only).
 
Monica Shiralkar
Ranch Foreman
Posts: 2076
12
  • Mark post as helpful
  • send pies
  • 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: 71098
292
  • Mark post as helpful
  • send pies
  • 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: 22801
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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: 65
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 1043
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look up records:
https://dzone.com/articles/a-first-look-at-records-in-java-14
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic