Auto-boxing and Unboxing
Auto-boxing
Auto-boxing is the process whereby the JS++ compiler will convert primitive data types to their corresponding object wrapper classes. For example, string
will be converted to System.String
:
1 2 3 4 5 | import System; System.String s1 = "abc" ; // is equivalent to: System.String s2 = new System.String( "abc" ); |
Auto-boxing is important because it enables ease-of-access to Standard Library methods. For instance, JS++ introduces a compare
method, which is not available from JavaScript. Thus, the JS++ compiler will auto-box the string value with the System.String
object wrapper class in order to provide the functionality:
1 2 3 4 | import System; Comparison compare1 = "abc" .compare( "def" ); // Valid due to auto-boxing Comparison compare2 = ( new System.String( "abc" )).compare( "def" ); // Equivalent boxed expression |
Primitive data types are auto-boxed in JS++ when they are assigned to a variable with the type of the corresponding object wrapper class or when they are passed as an argument to a parameter of a function, method, or operation that expects the type of the corresponding object wrapper class.
Unboxing
Unboxing occurs when an instance of an object wrapper class is converted to its corresponding primitive data type. For example, System.Integer32
being converted to int
:
1 2 3 | import System; int x = new System.Integer32(123); |
The above conversion is equivalent to calling the valueOf
method of System.Integer32
:
1 2 3 | import System; int x = ( new System.Integer32(123)).valueOf(); |
Object wrapper classes are unboxed in JS++ when they are assigned to a variable with the type of the corresponding primitive type or when they are passed as an argument to a parameter of a function, method, or operation that expects the type of the corresponding primitive data type (including when they are used as an operand to operations wherein the expected operand's type is of the corresponding primitive data type).
Conversion Table
The following is a conversion table which shows the primitive data types and their corresponding object wrapper classes:
See Also
Share
HTML | BBCode | Direct Link