Main Content

Call Java Method

This example shows how to call a method of the java.util.ArrayList class. The example demonstrates what it means to have Java® objects as references in MATLAB®.

The java.util.ArrayList class is part of the Java standard libraries. Therefore, the class is already on the Java class path. If you call a method in a class that is not in a standard library, then update the Java class path so that MATLAB can find the method. For information, see Java Class Path.

Choose Class Constructor to Create Java Object

Create an ArrayList object by using one of the class constructors. Display the class methods and look for the ArrayList entries in the methods window.

methodsview('java.util.ArrayList')
	ArrayList	(java.util.Collection)	
	ArrayList	( )	
	ArrayList	(int)	

Choose the ArrayList() syntax, which constructs an empty list with an initial capacity of 10.

Shorten Class Name

Use the import function to refer to the ArrayList class without specifying the entire package name java.util.

import java.util.ArrayList

Create Array List

Create an empty ArrayList object.

A = ArrayList;

Pass MATLAB Data to add Method

Add items to the ArrayList object. Look in the methods window at the signatures for the add method.

void     add (int,java.lang.Object)	
boolean  add (java.lang.Object)	

Choose the boolean add(java.lang.Object) syntax. The argument java.lang.Object is a Java type. To find the corresponding MATLAB type, look at the Pass java.lang.Object table. If you pass a double argument, MATLAB converts it to a java.lang.Double type.

Add Elements to ArrayList

To call the add method, use MATLAB syntax.

add(A,5);
A
A =

[5.0]

Alternatively, use Java syntax.

A.add(10);
A
A =

[5.0, 10.0]

Java Objects Are References in MATLAB

To observe the behavior of copying Java objects, assign A to a new variable B.

B = A;

B is a reference to A. Any change to the object referenced by B also changes the object at A. Either MATLAB code or Java code can change the object. For example, add a value to B, and then display A.

add(B,15);
A
A =

[5.0, 10.0, 15.0]

Use ArrayList Object in MATLAB

Suppose that you call a Java method that returns a Java object of type ArrayList. If you invoked the commands in the previous sections, variable A contains the following values:

class(A)
ans =

    'java.util.ArrayList'
A
A =

[5.0, 10.0, 15.0]

To use A in MATLAB, convert the object to either a java.lang.Object type or to a primitive type. Then apply the MATLAB cell and cell2mat functions.

From the ArrayList methods window, find the toArray method that converts an ArrayList to java.lang.Object[].

java.lang.Object[]	toArray	(java.lang.Object[])

Convert A to java.lang.Object.

res = toArray(A)
res =

  java.lang.Object[]:

    [ 5]
    [10]
    [15]

Convert the output to a MATLAB type.

res = cell(res)'
res =

  1×3 cell array

    [5]    [10]    [15]

To convert this value to a matrix, the elements must be the same type. In this example, the values convert to type double.

data = cell2mat(res)
data =

     5    10    15

See Also

|

Related Examples

More About

External Websites