#does `record` supposed to represent `struct` from C/++?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Yes, the record keyword in Java is similar to the struct concept in C/C++. A record is a special type of class introduced in Java 14 that is used to store data fields and does not contain any additional methods. It provides a concise way to define classes whose primary purpose is to store data.
Just like a struct in C/C++, a record in Java is immutable by default, meaning that its fields cannot be changed once they are set. This makes it ideal for representing data transfer objects (DTOs) or simple data containers where the focus is on storing and accessing data rather than behavior.
Additionally, records provide several built-in methods such as getters, setters, equals(), hashCode(), and toString(), making it easier to work with them compared to traditional Java classes.
Overall, if you need a lightweight, immutable data structure that only contains fields without behavior, using a record in Java can be an effective and concise way to achieve this.
It's similar. Records are classes that just act as simple carriers of data unlike regular classes which often contain more complex behaviours
Pretty much as the bot says, similar but with different constraints. The key difference to most classes, is that a record shows all of its state - meaning it's a data-container, not a stateful abstraction.
[is]
recordsupposed to representstructfrom C?
No, a record is still a class, it's basically just a fancy way of writing afinalclass where all the attributes are alsofinal, i.e. a class that can not be extended from, and with immutable data.
Astructin C is just a collection of variables.
[is]
recordsupposed to representstructfrom C++?
No.struct { ... }in C++ is just another way for writingclass { public: ... }, i.e. in C++ astructis a class where everything ispublicby default, whereas aclassis a class where everything isprivateby default. Obviously you can extend from aclassjust as well as from astructin C++, because the keywords are practically identical.
So none of these languages imposes any kind of immutability or non-extendibility when using struct, unlike Java when using record.
*A regular Java class would be the "equivalent" to a struct in C and C++
[off-topic] u again? u code C++ & Java?
I don't remember you, but yeah, I do both
i'm phyllmpse incase u forgot
lol
so simply
its kinda shorten (fancy of) final class
final class and all attributes final
okay
Also when writing a record you can't extend from any other class, but you can implement interfaces
a record is a restricted form of a class
i see
It’s ideal for "plain data carriers,"
I think the reason for this is because all records implicitly inherit from some record base class, and obviously in Java you can't inherit from 2 classes
^ but not 100% sure on that
If you could extend other types, they'd need to be records (or the class could violate other record invariants) and the declaration would no longer self-describe the state of the type (since it would have to combine the components - presumably as a prefix to the constructor). Then you get edge cases of shadowing, more complicated definitions of toString, questions about whether or not to make equals cover or exclude subtyping and more.
Once you want that complexity, you should just use a class.