Main Content

Construct and Concatenate Java Objects

Create Java Object

Many Java® method signatures contain Java object arguments. To create a Java object, call one of the constructors of the class. For an example, see Call 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, see Call Java Method.

Concatenate Objects of Same Class

To concatenate Java objects, use either the cat function 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 of java.lang.Byte, java.lang.Integer, and java.lang.Double creates 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 is java.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