Main Content

Construct and ConcatenateJavaObjects

CreateJavaObject

Many Java®method signatures contain Java object arguments. To create a Java object, call one of the constructors of the class. For an example, seeCall Java Method.

Java objects are not arrays like MATLAB®types. Calling MATLAB functions that expect MATLAB arrays might have unexpected results. Use Java methods to work on Java arrays instead. For an example, seeCall Java Method.

Concatenate Objects of Same Class

To concatenate Java objects, use either thecatfunction or the[]operators.

Concatenating objects of the same Java class results in an array of objects of that class.

value1 = java.lang.Integer(88); value2 = java.lang.Integer(45); cat(1,value1,value2)
ans = java.lang.Integer[]: [88] [45]

Concatenate Objects of Unlike Classes

If you concatenate objects of unlike classes, MATLAB finds one class from which all the input objects inherit. MATLAB selects the lowest common parent in the Java class hierarchy as the output class. For example, concatenating objects ofjava.lang.Byte,java.lang.Integer, andjava.lang.Doublecreates an object of the common parent to the three input classes,java.lang.Number.

byte = java.lang.Byte(127); integer = java.lang.Integer(52); double = java.lang.Double(7.8); [byte integer double]
ans = java.lang.Number[]: [ 127] [ 52] [7.8000]

If there is no common, lower-level parent, then the resultant class isjava.lang.Object.

byte = java.lang.Byte(127); point = java.awt.Point(24,127); [byte point]
ans = java.lang.Object[]: [ 127] [1×1 java.awt.Point]

Related Topics