#does `record` supposed to represent `struct` from C/++?

1 messages · Page 1 of 1 (latest)

cinder bison
#

hi

dense condorBOT
#

<@&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>.

#
TJ-Bot
Hi

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.

quaint violet
#

It's similar. Records are classes that just act as simple carriers of data unlike regular classes which often contain more complex behaviours

solemn lark
#

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.

stiff siren
# cinder bison hi

[is] record supposed to represent struct from C?
No, a record is still a class, it's basically just a fancy way of writing a final class where all the attributes are also final, i.e. a class that can not be extended from, and with immutable data.
A struct in C is just a collection of variables.

[is] record supposed to represent struct from C++?
No. struct { ... } in C++ is just another way for writing class { public: ... }, i.e. in C++ a struct is a class where everything is public by default, whereas a class is a class where everything is private by default. Obviously you can extend from a class just as well as from a struct in 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++

cinder bison
stiff siren
#

I don't remember you, but yeah, I do both

cinder bison
#

lol

cinder bison
#

its kinda shorten (fancy of) final class

stiff siren
cinder bison
#

okay

stiff siren
# cinder bison okay

Also when writing a record you can't extend from any other class, but you can implement interfaces

cinder bison
#

a record is a restricted form of a class

#

i see

#

It’s ideal for "plain data carriers,"

stiff siren
#

^ but not 100% sure on that

solemn lark
#

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.