Skip to content

Commit 17b782c

Browse files
Proto Docs: Clarify the behavior of repeated fields in Java.
PiperOrigin-RevId: 943640700
1 parent 9a9a8e6 commit 17b782c

1 file changed

Lines changed: 25 additions & 21 deletions

File tree

content/reference/java/java-generated.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ enum, or message (including nested types) in the file with the same name,
6666

6767
{{% alert title="Note" color="note" %}}If you are
6868
using the deprecated v1 of the protobuf API, `OuterClass` is added regardless of
69-
any collisions with message
70-
names.{{% /alert %}}
69+
any collisions with message names.
70+
{{% /alert %}}
7171

7272
In addition to any nested classes, the wrapper class itself will have the
7373
following API (assuming the wrapper class is named `Foo` and was generated from
@@ -117,17 +117,18 @@ class files.
117117
## Packages {#package}
118118

119119
The generated class is placed in a Java package based on the `java_package`
120-
option. If the option is omitted, the `package` declaration is used instead.
120+
option. If the option is omitted, the `package` declaration is used
121+
instead.
121122

122123
For example, if the `.proto` file contains:
123124

124125
```proto
125126
package foo.bar;
126127
```
127128

128-
Then the resulting Java class will be placed in Java package
129-
`foo.bar`. However, if the `.proto` file also
130-
contains a `java_package` option, like so:
129+
Then the resulting Java class will be placed in Java
130+
package `foo.bar`. However, if
131+
the `.proto` file also contains a `java_package` option, like so:
131132

132133
```proto
133134
package foo.bar;
@@ -426,33 +427,36 @@ the generated [enum type](#enum).
426427
For this field definition:
427428

428429
```proto
429-
repeated string foos = 1;
430+
repeated int32 foos = 1;
430431
```
431432

432433
The compiler will generate the following accessor methods in both the message
433434
class and its builder:
434435

435-
- `int getFoosCount()`: Returns the number of elements currently in the field.
436-
- `String getFoos(int index)`: Returns the element at the given zero-based
437-
index.
438-
- `ProtocolStringList getFoosList()`: Returns the entire field as a
439-
`ProtocolStringList`. If the field is not set, returns an empty list.
436+
* `int getFoosCount()`: Returns the number of elements currently in the field.
437+
* `int getFoos(int index)`: Returns the element at the given zero-based index.
438+
* `java.util.List<java.lang.Integer> getFoosList()`: Returns the entire field
439+
as a java.util.List. If the field is not set, returns an empty list.
440440

441441
The compiler will generate the following methods only in the message's builder:
442442

443-
- `Builder setFoos(int index, String value)`: Sets the value of the element at
443+
* `Builder setFoos(int index, int value)`: Sets the value of the element at
444444
the given zero-based index.
445-
- `Builder addFoos(String value)`: Appends a new element to the field with the
445+
* `Builder addFoos(int value)`: Appends a new element to the field with the
446446
given value.
447-
- `Builder addAllFoos(Iterable<? extends String> value)`: Appends all elements
448-
in the given `Iterable` to the field.
449-
- `Builder clearFoos()`: Removes all elements from the field. After calling
450-
this, `getFoosCount()` will return zero.
447+
* `Builder addAllFoos(Iterable<? extends Integer> value)`: Appends all
448+
elements in the given Iterable to the field.
449+
* `Builder clearFoos()`: Removes all elements from the field. After calling
450+
this, getFoosCount() will return zero.
451451

452452
For other simple field types, the corresponding Java type is chosen according to
453-
the
454-
[scalar value types table](/programming-guides/proto2#scalar).
455-
For message and enum types, the type is the message or enum class.
453+
the scalar value types table. For message and enum types, the type is the
454+
message or enum class.
455+
456+
For a `repeated string` field, the `getList()` accessor returns a
457+
`ProtocolStringList`, a special interface that extends `java.util.List<String>`
458+
but also allows direct access to the underlying `ByteString` representations.
459+
Other primitive repeated fields return a `java.util.List`.
456460

457461
#### Repeated Embedded Message Fields {#repeated-embedded}
458462

0 commit comments

Comments
 (0)