|
1 |
| -> **⚠ Experimental** |
| 1 | +> **⚠️ Experimental Feature** |
2 | 2 |
|
3 | 3 | # Optional parameters & named arguments
|
4 | 4 |
|
@@ -164,19 +164,34 @@ Optional parameters and named arguments preserve three essential aspects of bina
|
164 | 164 | - **Binary accessible**
|
165 | 165 | - **Java standard compatible**
|
166 | 166 |
|
167 |
| -### Backward compatible |
168 |
| -Adding new optional parameters to existing methods is binary compatible with code compiled before the new parameters were added. |
| 167 | +### Backward compatible |
169 | 168 |
|
170 |
| -For example, this method: |
| 169 | +You can add new optional parameters to existing methods without breaking compatibility with code compiled before those |
| 170 | +parameters were introduced. |
| 171 | + |
| 172 | +**For example:** |
| 173 | +Suppose you have the following method: |
171 | 174 | ```java
|
172 |
| -public void size(int width) {...} |
173 |
| -``` |
174 |
| -Could be updated with an optional height parameter: |
| 175 | +public void size(int width) { ... } |
| 176 | +``` |
| 177 | +You can update it to include an optional `height` parameter: |
175 | 178 | ```java
|
176 |
| -public void size(int width, int height = width) {...} |
177 |
| -``` |
178 |
| -Code compiled with the previous version still works without requiring recompilation. |
179 |
| - |
| 179 | +public void size(int width, int height = 0) { ... } |
| 180 | +``` |
| 181 | +Code compiled with the earlier version of the method will continue to work without requiring recompilation. |
| 182 | + |
| 183 | +**Changing Default Values:** |
| 184 | +If the default value of an optional parameter changes, the updated value will apply even when older code calls the method. |
| 185 | +```java |
| 186 | +public void size(int width, int height = width) { ... } |
| 187 | +``` |
| 188 | +In this case, older code that doesn’t specify a `height` argument will use the new default (`width`) at runtime without |
| 189 | +needing to recompile. |
| 190 | + |
| 191 | +**⚠️ Caution:** |
| 192 | +Changing default values, while binary-compatible, can still impact the behavior of existing code. Make these changes |
| 193 | +cautiously to prevent unintended side effects. |
| 194 | + |
180 | 195 | ### Binary accessible
|
181 | 196 | Optional parameters and named arguments are fully accessible from compiled .class files, just like source code.
|
182 | 197 | ```java
|
|
0 commit comments