User`s manual
Passing Data to a Java Method
5-51
In each of these calls to menu1.add, an object that is an instance of the
java.awt.MenuItem Java class is passed.
menu1 = java.awt.Menu('File Options');
menu1.add(java.awt.MenuItem('New'));
menu1.add(java.awt.MenuItem('Open'));
menu1.add(java.awt.MenuItem('Save'));
menuBar=java.awt.MenuBar;
menuBar.add(menu1);
frame.setMenuBar(menuBar);
Handling Objects of Class java.lang.Object
A special case exists when the method being called takes an argument of the
java.lang.Object class. Since this class is the root of the Java class hierarchy,
you can pass objects of any class in the argument. The hash table example
shown that follows, passes objects belonging to different classes to a common
method,
put, which expects an argument of java.lang.Object. The method
declaration for
put is
public synchronized Object put(Object key, Object value)
The following MATLAB code passes objects of different types (boolean, float,
and
string) to the put method.
hTable = java.util.Hashtable;
hTable.put(0, java.lang.Boolean('TRUE'));
hTable.put(1, java.lang.Float(41.287));
hTable.put(2, java.lang.String('test string'));
hTable % Verify hash table contents
hTable =
{1.0=41.287, 2.0=test string, 0.0=true}
When passing arguments to a method that takes java.lang.Object, it is not
necessary to specify the class name for objects of a built-in data type. Line
three, in the example above, specifies that
41.287 is an instance of class
java.lang.Float. You can omit this and simply say, 41.287, as shown in the
following example. Thus, MATLAB will create each object for you, choosing the
closest matching Java object representation for each argument.