0
votes

I have a message Message defined in my .proto file. It has two fields, a required field and an optional field.

message Message
{
    required int32 requiredField = 1;
    optional int32 optionalField = 2;
}

Java code generated has its associated class Message and I am having two objects of it oMessage1 & oMessage2. oMessage1 is holding some value in its optionalField but oMessage2 is not holding any value in it (as its optional).

My Question is:

When I do oMessage1 = oMessage2, what happens to the value of optionalField of oMessage1?

  1. Does it vanishes? (Because oMessage2 doesn't have it) OR
  2. Does it remain same? (Because oMessage1 have it and therefore assignment operator has taken care to not to overwrite it with non-existing fields)

I couldn't find its answer in documentation. And generated code was too big for me to analyse as I am relatively new to Java :( Ideally I am expecting #2 is an answer. But I like to verify with Protobuf experts so that I can take care about it in my code.

Many thanks in advance!

1

1 Answers

1
votes

For object variables, the assignment operator in Java just assigns the reference. oMessage1 will just point to the object denoted by oMessage2, too. Accessing oMessage1 will be identical to accessing oMessage2 after the assignment.

The protocol buffer referenced by oMessage1 may vanish altogether (= be collected by garabge collection) if nothing else holds a reference to it.