BBj Object Assignment

Description

Assignment of a number or string to an object variable copies the value. For example, after executing these lines of code, subsequent changes to X will not affect X! In addition, subsequent changes to Y$ will not affect Y!:

LET X! = X
LET Y! = Y$

Assignment of any other value to an object variable copies a reference. For example, after executing these lines of code, subsequent changes to A! will be reflected in X!, because they are pointers to the same ArrayList:

LET A! = new java.util.ArrayList()
LET X! = A!

Some objects define a clone() method to create a copy of a complex data structure. See the Java documentation for details. For example:

LET A! = new java.util.ArrayList()
LET X! = A!.clone()

A Java expression that returns a Java array can be assigned directly to a BBj array, as in these examples:

REM ' Locale![] is a BBj array of java.util.Locale objects
LET Locale![] = java.text.NumberFormat.getAvailableLocales()
REM ' IP![] is a BBj array of bytes representing the local IP address
LET IP![] = java.net.InetAddress.getLocalHost().getAddress()
PRINT IP![]                                                
192 168 65 77

In BBj 18.0 and higher, Java arrays can be created with thenewoperator, and can be directly manipulated using syntax similar to that of traditional BBj arrays:

use java.awt.Color
declare Color[] Color!
color! = new Color[2]
color![0] = Color.RED
color![1] = Color.BLUE
print "color![0]=",color![0]
print "color![1]=",color![1]

See Also

BBj Object Variables

BBj Object Error Handling

BBj Object Operators

Working with Java Arrays