/usr/share/doc/octave/octave.html/How-to-use-Java-from-within-Octave.html is in octave-doc 4.2.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>How to use Java from within Octave (GNU Octave)</title>
<meta name="description" content="How to use Java from within Octave (GNU Octave)">
<meta name="keywords" content="How to use Java from within Octave (GNU Octave)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Java-Interface.html#Java-Interface" rel="up" title="Java Interface">
<link href="Passing-parameters-to-the-JVM.html#Passing-parameters-to-the-JVM" rel="next" title="Passing parameters to the JVM">
<link href="Making-Java-Classes-Available.html#Making-Java-Classes-Available" rel="prev" title="Making Java Classes Available">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<a name="How-to-use-Java-from-within-Octave"></a>
<div class="header">
<p>
Next: <a href="Passing-parameters-to-the-JVM.html#Passing-parameters-to-the-JVM" accesskey="n" rel="next">Passing parameters to the JVM</a>, Previous: <a href="Making-Java-Classes-Available.html#Making-Java-Classes-Available" accesskey="p" rel="prev">Making Java Classes Available</a>, Up: <a href="Java-Interface.html#Java-Interface" accesskey="u" rel="up">Java Interface</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="How-to-use-Java-from-within-Octave-1"></a>
<h4 class="subsection">A.4.2 How to use Java from within Octave</h4>
<p>The function <a href="Java-Interface-Functions.html#XREFjavaObject">javaObject</a> creates Java objects.
In fact it invokes the public constructor of the class with the given name
and with the given parameters.
</p>
<p>The following example shows how to invoke the constructors
<code>BigDecimal(double)</code> and <code>BigDecimal(String)</code> of the builtin Java
class <code>java.math.BigDecimal</code>.
</p>
<div class="example">
<pre class="example">javaObject ("java.math.BigDecimal", 1.001 );
javaObject ("java.math.BigDecimal", "1.001");
</pre></div>
<p>Note that parameters of the Octave type <code>double</code> are implicitly converted
into the Java type <code>double</code> and the Octave type (array of) <code>char</code> is
converted into the java type <code>String</code>. A Java object created by
<a href="Java-Interface-Functions.html#XREFjavaObject">javaObject</a> is never automatically converted
into an Octave type but remains a Java object. It can be assigned to an
Octave variable.
</p>
<div class="example">
<pre class="example">a = 1.001;
b = javaObject ("java.math.BigDecimal", a);
</pre></div>
<p>Using <a href="Java-Interface-Functions.html#XREFisjava">isjava</a>, it is possible to check whether a
variable is a Java object and its class can be determined as well. In
addition to the previous example:
</p>
<div class="example">
<pre class="example">isjava (a)
⇒ ans = 0
class (a)
⇒ ans = double
isjava (b)
⇒ ans = 1
class (b)
⇒ ans = java.math.BigDecimal
</pre></div>
<p>The example above can be carried out using only Java objects:
</p>
<div class="example">
<pre class="example">a = javaObject ("java.lang.Double", 1.001);
b = javaObject ("java.math.BigDecimal", a);
isjava (a)
⇒ ans = 1
class (a)
⇒ ans = java.lang.Double
isjava (b)
⇒ ans = 1
class (b)
⇒ ans = java.math.BigDecimal
</pre></div>
<p>One can see, that even a <code>java.lang.Double</code> is not converted to an Octave
<code>double</code>, when created by <a href="Java-Interface-Functions.html#XREFjavaObject">javaObject</a>.
But ambiguities might arise, if the Java classes <code>java.lang.Double</code> or
<code>double</code> are parameters of a method (or a constructor). In this case
they can be converted into one another, depending on the context.
</p>
<p>Via <a href="Java-Interface-Functions.html#XREFjavaObject">javaObject</a> one may create all kinds of
Java objects but arrays. The latter are created through
<a href="Java-Interface-Functions.html#XREFjavaArray">javaArray</a>.
</p>
<p>It is possible to invoke public member methods on Java objects in Java syntax:
</p>
<div class="example">
<pre class="example">a.toString
⇒ ans = 1.001
b.toString
⇒ ans = 1.000999999999999889865...
</pre></div>
<p>The second result may be surprising, but simply comes from the fact, that
<code>1.001</code> cannot exactly be represented as <code>double</code>, due to rounding.
Note that unlike in Java, in Octave methods without arguments can be invoked
with and without parentheses <code>()</code>.
</p>
<p>Currently it is not possible to invoke static methods with a Java like syntax
from within Octave. Instead, one has to use the function
<a href="Java-Interface-Functions.html#XREFjavaMethod">javaMethod</a> as in the following example:
</p>
<div class="example">
<pre class="example">java.math.BigDecimal.valueOf(1.001); # does not work
javaMethod ("valueOf", "java.math.BigDecimal", 1.001); # workaround
</pre></div>
<p>As mentioned before, method and constructor parameters are converted
automatically between Octave and Java types, if appropriate. For functions
this is also true with return values, whereas for constructors this is not.
</p>
<p>It is also possible to access public fields of Java objects from within Octave
using Java syntax, with the limitation of static fields:
</p>
<div class="example">
<pre class="example">java.math.BigDecimal.ONE; # does not work
java_get ("java.math.BigDecimal", "ONE"); # workaround
</pre></div>
<p>Accordingly, with <a href="Java-Interface-Functions.html#XREFjava_005fset">java_set</a> the value of a field
can be set. Note that only public Java fields are accessible from within
Octave.
</p>
<p>The following example indicates that in Octave empty brackets <code>[]</code>
represent Java’s <code>null</code> value and how Java exceptions are represented.
</p>
<div class="example">
<pre class="example">javaObject ("java.math.BigDecimal", []);
⇒ error: [java] java.lang.NullPointerException
</pre></div>
<p>It is not recommended to represent Java’s <code>null</code> value by empty brackets
<code>[]</code>, because <code>null</code> has no type whereas <code>[]</code> has type
<code>double</code>.
</p>
<p>In Octave it is possible to provide limited Java reflection by listing the
public fields and methods of a Java object, both static or not.
</p>
<div class="example">
<pre class="example">fieldnames (<Java object>)
methods (<Java object>)
</pre></div>
<p>Finally, an examples is shown how to access the stack trace from within
Octave, where the function <a href="Java-Interface-Functions.html#XREFdebug_005fjava">debug_java</a> is used
to set and to get the current debug state. In debug mode, the Java error and
the stack trace are displayed.
</p>
<div class="example">
<pre class="example">debug_java (true) # use "false" to omit display of stack trace
debug_java ()
⇒ ans = 1
javaObject ("java.math.BigDecimal", "1") ...
.divide (javaObject ("java.math.BigDecimal", "0"))
</pre></div>
<hr>
<div class="header">
<p>
Next: <a href="Passing-parameters-to-the-JVM.html#Passing-parameters-to-the-JVM" accesskey="n" rel="next">Passing parameters to the JVM</a>, Previous: <a href="Making-Java-Classes-Available.html#Making-Java-Classes-Available" accesskey="p" rel="prev">Making Java Classes Available</a>, Up: <a href="Java-Interface.html#Java-Interface" accesskey="u" rel="up">Java Interface</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|