Reputation: 33
i want to create an struct in java , like c++ :
struct MyStruct {
int x;
};
#include <iostream>
int main() {
MyStruct Struct;
Struct.x = 0;
std::cout << Struct.x;
return 0;
}
can anyone help me ?
Upvotes: 1
Views: 29214
Reputation: 338446
struct MyStruct {
int x;
};
… becomes:
record MyRec ( int x ) {}
And this:
#include <iostream>
int main() {
MyStruct Struct;
Struct.x = 0;
std::cout << Struct.x;
return 0;
}
… becomes:
public class RecordDemo
{
public static void main ( String[] args )
{
record MyRec( int x ) { }
MyRec rec = new MyRec ( 0 );
System.out.println ( rec.x );
}
}
In the future, we may be able to write simply this:
void main ( String[] args )
{
record MyRec( int x ) { }
MyRec rec = new MyRec ( 0 );
System.out.println ( rec.x );
}
A more realistic example:
record Person( String name , LocalDate dateOfBirth ) { }
SequencedCollection < Person > persons =
List.of (
new Person ( "Alice" , LocalDate.of ( 1960 , Month.JANUARY , 23 ) ) ,
new Person ( "Bob" , LocalDate.of ( 1970 , Month.FEBRUARY , 24 ) ) ,
new Person ( "Carol" , LocalDate.of ( 1980 , Month.MARCH , 25 ) )
);
LocalDate today = LocalDate.now ( ZoneId.of ( "America/Edmonton" ) );
double averageAge = persons.stream ( ).mapToInt ( person -> Period.between ( person.dateOfBirth , today ).getYears ( ) ).average ( ).getAsDouble ( );
System.out.println ( "averageAge = " + averageAge );
averageAge = 54.0
record
Java 16 and later offers the Java record
feature.
As a special kind of class, a record is appropriate where you want to transparently communicate shallowly-immutable data. For a complete definition, see JEP 395: Records.
Formally speaking, a record is Java’s nominal tuple. Tuple means a sequenced set of fields. Nominal means the fields have a name.
👉🏽 Practically speaking, you can think of a Java record as a C-style struct
on steroids.
Let's rewrite your example code:
struct MyStruct {
int x;
};
… as a Java record:
record MyRec ( int x ) {}
The fields are defined within the parentheses, with only a single one here. The braces would contain code for behavior, but here we have no additional behavior. By default a record implicitly provides these behaviors:
java.lang.Object
class:
Let's rewrite the rest of your example code:
#include <iostream>
int main() {
MyStruct Struct;
Struct.x = 0;
std::cout << Struct.x;
return 0;
}
… in Java, showing just the key lines:
record MyRec( int x ) { }
MyRec rec = new MyRec ( 0 );
System.out.println ( rec.x );
… and showing a complete example app.
package work.basil.example.recs;
public class RecordDemo
{
public static void main ( String[] args )
{
record MyRec( int x ) { }
MyRec rec = new MyRec ( 0 );
System.out.println ( rec.x );
}
}
When run:
0
You can conveniently both compile and execute that last code example by:
package
line.RecordDemo.java
.java RecordDemo.java
.For details, see JEP 330: Launch Single-File Source-Code Programs.
Notice that we have the option of declaring the record
locally. This is handy when organizing some locally-generated data that will not be shared with other parts of your app such as generating a report. Or, of course, you may declare the record
separately as a separate class for when your are sharing the data as objects.
A Java record is read-only. A record has getter methods, but no setter methods.
You can easily write a Builder manually, or via a generative AI.
Write the code for a Builder for this Java record: record MyRec ( int x ) {}
The AI generates this source code:
public record MyRec(int x) {
public static class Builder {
private int x;
public Builder setX(int x) {
this.x = x;
return this;
}
public MyRec build() {
return new MyRec(x);
}
}
public static Builder builder() {
return new Builder();
}
}
… along with a usage example:
public class Main {
public static void main(String[] args) {
MyRec myRec = MyRec.builder()
.setX(10)
.build();
System.out.println(myRec);
}
}
Wither methods are methods that take an object and generate a new object using the original’s field values as the defaults while allowing some values to be specified as different. The methods typically involve the word “with…”, hence the name withers.
Let's include withers in our AI request.
Write the code for a Builder that includes withers for this Java record: record MyRec ( int x ) {}
public record MyRec(int x) {
public static class Builder {
private int x;
public Builder setX(int x) {
this.x = x;
return this;
}
public MyRec build() {
return new MyRec(x);
}
// Wither for the x property
public Builder withX(int x) {
this.x = x;
return this;
}
}
public static Builder builder() {
return new Builder();
}
// Wither method for creating a modified copy
public MyRec withXValue(int newX) {
return new MyRec(newX);
}
}
And the usage example generated by the AI:
public class Main {
public static void main(String[] args) {
// Creating an instance using the builder
MyRec myRec = MyRec.builder()
.setX(10)
.build();
System.out.println(myRec); // Output: MyRec[x=10]
// Using the wither method to create a modified copy
MyRec modifiedRec = myRec.withXValue(20);
System.out.println(modifiedRec); // Output: MyRec[x=20]
}
}
Work is underway to enable derived creation for records. If successful, this effort will eliminate the need to provide explicit wither methods. See JEP 468: Derived Record Creation (Preview).
Upvotes: 3
Reputation: 17454
Java doesn't have struct
s like C or C++, but you can use Java classes and treat them like a struct
. On top of that, you can of course declare all its members as public. (to work exactly like a struct
)
class MyClass
{
public int num;
}
MyClass m = new MyClass();
m.num = 5;
System.out.println(n.num);
One of the differences between a struct
and a class
is that a struct do not have methods. If you create a class without methods, it will work like a struct
.
However, you can always put in methods (getters and setters) and set the variables as private like this (it doesn't hurt).
class MyClass
{
private int num;
public void setNum(int num){
this.num = num
}
public int getNum(){
return num
}
}
MyClass m = new MyClass();
m.setNum(5);
System.out.println(n.getNum());
Java doesn't have struct
s, but a Class can do exactly the same things a struct
does.
Upvotes: -1
Reputation:
public class ircodes {
public ircodes(String msg_id, String node_id, String frequency, String data) {
this.hdr = new msg_hdr(4 + data.length(), Integer.parseInt(msg_id), Integer.parseInt(node_id));
this.frequency = Integer.parseInt(frequency);
this.data = data;
}
public class msg_hdr {
int msg_len;
int msg_id;
int node_id;
public msg_hdr(int msg_len, int msg_id, int node_id) {
this.msg_len = 12 + msg_len;
this.msg_id = msg_id;
this.node_id = node_id;
}
}
msg_hdr hdr;
int frequency;
String data;
public ByteBuffer serialize() {
ByteBuffer buf = ByteBuffer.allocate(hdr.msg_len);
buf.putInt(hdr.msg_len);
buf.putInt(hdr.msg_id);
buf.putInt(hdr.node_id);
buf.putInt(frequency);
buf.put(data.getBytes());
return buf;
}
}
Upvotes: 0
Reputation: 1403
You can use a class, which functions similarly to a struct
in C++.
For example, a C++ point
struct may look like
typedef struct __point {
int x, y;
} point;
A Java point
class has the form
final class Point {
private final double x; // x-coordinate
private final double y; // y-coordinate
// point initialized from parameters
public Point(double x, double y) {
this.x = x;
this.y = y;
}
// accessor methods
public double x() { return x; }
public double y() { return y; }
// return a string representation of this point
public String toString() {
return "(" + x + ", " + y + ")";
}
}
We may then call the following:
Point q = new Point(0.5, 0.5);
System.out.println("q = " + q);
System.out.println("x = " + q.x());
Upvotes: 0