X-Git-Url: http://gitweb.scilab.org/?p=scilab.git;a=blobdiff_plain;f=scilab%2Fmodules%2Fexternal_objects_java%2Fhelp%2Fen_US%2F01-getting-started.xml;h=bc15212df993fc465567b360c65f415d8f1373d8;hp=8b9d64dbdf2796b99929d3442073f7b8e704c7ec;hb=807fe176da3bcb88ce29ad8401cd97aa22cc1c1f;hpb=025bb6738d47ccbd9093b90bc22b9710c41227c0 diff --git a/scilab/modules/external_objects_java/help/en_US/01-getting-started.xml b/scilab/modules/external_objects_java/help/en_US/01-getting-started.xml index 8b9d64d..bc15212 100644 --- a/scilab/modules/external_objects_java/help/en_US/01-getting-started.xml +++ b/scilab/modules/external_objects_java/help/en_US/01-getting-started.xml @@ -5,18 +5,21 @@ * Copyright (C) 2013 - S/E - Sylvestre Ledru * Copyright (C) 2012 - CNES - Simon Billemont * - * Copyright (C) 2012 - 2016 - Scilab Enterprises - * - * This file is hereby licensed under the terms of the GNU GPL v2.0, - * pursuant to article 5.3.4 of the CeCILL v.2.1. - * This file was originally licensed under the terms of the CeCILL v2.1, - * and continues to be available under such terms. - * For more information, see the COPYING file which you should have received - * along with this program. +* Copyright (C) 2012 - 2016 - Scilab Enterprises +* +* This file is hereby licensed under the terms of the GNU GPL v2.0, +* pursuant to article 5.3.4 of the CeCILL v.2.1. +* This file was originally licensed under the terms of the CeCILL v2.1, +* and continues to be available under such terms. +* For more information, see the COPYING file which you should have received +* along with this program. * * --> - + Getting started - Beginning How to use the Java Scilab binding? @@ -28,7 +31,9 @@ Basic - Before starting, it is convenient to know the most important functions and what they do. These frequently used functions are the following Scilab functions: + + Before starting, it is convenient to know the most important functions and what they do. + These frequently used functions are the following Scilab functions: jimport: Import a Java class @@ -39,17 +44,33 @@ - jimport is the function that mirrors the functionality of the java statement 'import', and loads the specified class definition/template of a given class in memory. When loaded, this definition is used to accesses static methods/members, and create new objects. + jimport is the function that mirrors the functionality of the java statement 'import', + and loads the specified class definition/template of a given class in memory. + When loaded, this definition is used to accesses static methods/members, + and create new objects. - jinvoke is a function that calls (invokes) a specified method on a java class or object. This invoke has an optional set of parameters that must coincide with the actual member signature. This means you must pass the same number of arguments, and these arguments must have the correct type. + jinvoke is a function that calls (invokes) a specified method on a java class or object. + This invoke has an optional set of parameters that must coincide with the actual member + signature. This means you must pass the same number of arguments, and these arguments + must have the correct type. Example 1: Creating a basic class and calling a simple method - In this first example, the three basic pillars of working with Java are treated. The first is to load a class, then an instance is constructed and finally the invocation or calling of one of this methods or members. - Consider a basic class as presented in the example HelloWorld. It has a default constructor generating a message upon construction and one public method which also shows a message when it has been called. This class should now be compiled into java byte-code. When developing your own code, then this section is usually handled by your IDE (integrated development environment). If you plan to use external libraries, these will available in precompiled form (packed in a JAR). + In this first example, the three basic pillars of working with Java are treated. + The first is to load a class, then an instance is constructed and finally the invocation + or calling of one of this methods or members. + + + Consider a basic class as presented in the example HelloWorld. + It has a default constructor generating a message upon construction and one + public method which also shows a message when it has been called. + This class should now be compiled into java byte-code. When developing your own code, + then this section is usually handled by your IDE (integrated development environment). + If you plan to use external libraries, these will available in precompiled form + (packed in a JAR). - Once the compiled version of this Java class exists, we can start Scilab and try to get Scilab to show us the various messages. - Now the HelloWorld class can be imported into our workspace. This is done using the already mentioned jimport: + Once the compiled version of this Java class exists, we can start Scilab and try + to get Scilab to show us the various messages. + Now the HelloWorld class can be imported into our workspace. This is done using the + already mentioned jimport: - -->jimport com.foo.HelloWorld +--> jimport com.foo.HelloWorld - -->HelloWorld - HelloWorld = +--> HelloWorld +HelloWorld = +class com.foo.HelloWorld - class com.foo.HelloWorld - - -->whos -name HelloWorld - Name Type Size Bytes - - HelloWorld _EClass ? 168 - +--> whos -name HelloWorld +Name Type Size Bytes +HelloWorld _EClass ? 168 + - Upon competition, an additional variable named HelloWorld has been created. This is equivalent to a Class object in java. From this class object, new objects of the HelloWorld type can be created. + Upon competition, an additional variable named HelloWorld has been created. + This is equivalent to a Class object in java. From this class object, + new objects of the HelloWorld type can be created. - Creating such an object instance can be done by invoking new on the class definition. The arguments to this function are the parameters that are delegated to the Java constructor. The result of this operation is a new Java object reference which can be stored in a variable for later use. + Creating such an object instance can be done by invoking new + on the class definition. The arguments to this function are the parameters that are + delegated to the Java constructor. + The result of this operation is a new Java object reference which can be stored + in a variable for later use. - -->object = HelloWorld.new(); - HelloWorld constructed! - - -->object - object = - - com.foo.HelloWorld@49aacd5f +--> object = HelloWorld.new(); +HelloWorld constructed! - -->whos -name object - Name Type Size Bytes +--> object +object = +com.foo.HelloWorld@49aacd5f - object _EObj ? 160 - +--> whos -name object +Name Type Size Bytes +object _EObj ? 160 + - What one sees when the new operator is called on the JClass, it transparently invokes the Java constructor, and our "HelloWorld constructed!" message appears. The resulting HelloWorld object is stored in the "object" variable. When the variable name is reentered in the command line, the details of its reference are shown. This message can be customized by overriding the toString method in the HelloWorld class. + What one sees when the new operator is called on the + JClass, it transparently invokes the Java constructor, and our + "HelloWorld constructed!" message appears. The resulting HelloWorld object is + stored in the "object" variable. When the variable name is reentered in the + command line, the details of its reference are shown. This message can be + customized by overriding the toString method in the HelloWorld class. - Now that a specific HelloWorld object has been created, one can try to call the public method that has been declared; HelloWorld\#message(). The same natural technique as with new can then be applied to invoke the method: + Now that a specific HelloWorld object has been created, one can try to call + the public method that has been declared; HelloWorld\#message(). + The same natural technique as with new can then be + applied to invoke the method: - -->object.message(); - Hello world! - +--> object.message(); +Hello world! + - The dot operator (dot between object and message) is actually a handy shortcut and expands the following snippet of Scilab code. The use of this shortcut makes it simpler and cleaner to invoke methods/get member variables. + The dot operator (dot between object and message) is actually a handy shortcut + and expands the following snippet of Scilab code. The use of this shortcut + makes it simpler and cleaner to invoke methods/get member variables. - -->jinvoke(object, 'message'); - Hello world! - +--> jinvoke(object, 'message'); +Hello world! + Example 2: Exchanging Scilab and Java primitives - This example treats the way you can exchange primitive data types and strings between Scilab and Java. We will be passing various types of objects between these two languages. + This example treats the way you can exchange primitive data types and strings between + Scilab and Java. We will be passing various types of objects between these two languages. - For this an example class (see Class Inspector) has been defined that takes and returns objects. - There are two methods defined. The first takes a double does some arithmetic and returns a result: Inspector#eval(double). The other methods takes any object, shows some basic information and returns it: Inspector#inspect(Object). + For this an example class (see Class Inspector) has been defined that takes and + returns objects. + There are two methods defined. The first takes a double does some arithmetic and + returns a result: Inspector#eval(double). The other methods takes any object, + shows some basic information and returns it: Inspector#inspect(Object). - As in the previous example, this code must be compiled to Java byte-code before it can be used directly. + As in the previous example, this code must be compiled to Java byte-code before + it can be used directly. To start, import the Inspector class and create an Inspector object: - -->jimport('com.foo.Inspector'); - - -->myInspector = Inspector.new() - myInspector = - - com.foo.Inspector@2a788315 - - - Now information between the two systems can be passing along. When passing along any of the Scilab data types into Java, it is automatically wrapped (see jwrap) to its Java equivalent. First, an example using the most used data type in Scilab; reals (constant) is given. When passing along a real, this type gets automatically mapped to the Scilab type double. Let's try; +--> jimport('com.foo.Inspector'); + +--> myInspector = Inspector.new() +myInspector = +com.foo.Inspector@2a788315 + + Now information between the two systems can be passing along. + When passing along any of the Scilab data types into Java, it is automatically wrapped + (see jwrap) to its Java equivalent. + First, an example using the most used data type in Scilab; reals (constant) is given. + When passing along a real, this type gets automatically mapped to the Scilab type double. + Let's try; - -->result = myInspector.eval(12.5) - result = - - 6.25 - - -->result * 2 - ans = - - 12.5 - - -->whos -name result - Name Type Size Bytes - - result constant 1 by 1 24 - - The automatic convert is controlled by the jautoUnwrap function. Without this function, all the conversions have to be done explicitly. +--> result = myInspector.eval(12.5) +result = +6.25 + +--> result * 2 +ans = +12.5 + +--> whos -name result +Name Type Size Bytes +result constant 1 by 1 24 + + + The automatic convert is controlled by the jautoUnwrap function. + Without this function, all the conversions have to be done explicitly. - -->result = myInspector.eval(12.5) - result = - - 6.25 - - -->result * 2 - ans = - - null +--> result = myInspector.eval(12.5) +result = +6.25 - -->whos -name result - Name Type Size Bytes +--> result * 2 +ans = +null - result _EObj ? 160 +--> whos -name result +Name Type Size Bytes +result _EObj ? 160 - The result that is returned seems to be correct at first glance ($12.5/2=6.25$). However upon closer inspection, notice that what is returned from our function call is not a number. What we received is another Java object (Double in this case). To be able to use the given data in again in Scilab, the already mentioned junwrap functionality can be used if the jautoUnwrap is not set to true. - This transforms Java types back to there equivalent Scilab form. From then onward we have a normal number again: + + The result that is returned seems to be correct at first glance ($12.5/2=6.25$). + However upon closer inspection, notice that what is returned from our function call + is not a number. What we received is another Java object (Double in this case). + To be able to use the given data in again in Scilab, the already mentioned + junwrap functionality can be used if the jautoUnwrap + is not set to true. + This transforms Java types back to there equivalent Scilab form. From then onward we + have a normal number again: - -->result = junwrap(result) - result = - - 6.25 - - -->whos -name result - Name Type Size Bytes - - result constant 1 by 1 24 - - -->result * 2 - ans = - - 12.5 - - - From this example is clear how doubles get automatically transformed into a Double, which is used by the Java VM and returned back. When calling junwrap on the returned variable, it is transformed back into a native Scilab type. But how do other types work? Let's inspect several of the other primitive types; +--> result = junwrap(result) +result = +6.25 + +--> whos -name result +Name Type Size Bytes +result constant 1 by 1 24 + +--> result * 2 +ans = +12.5 + + + From this example is clear how doubles get automatically transformed into a Double, + which is used by the Java VM and returned back. + When calling junwrap on the returned variable, it is + transformed back into a native Scilab type. But how do other types work? Let's inspect + several of the other primitive types; - -->jautoUnwrap(%f) // Make sure we disable the auto Unwrap - - -->result = myInspector.inspect("Hello world!"); - Inspecting: 'Hello world!' - Class: String - - -->whos -name result - Name Type Size Bytes - - result _EObj ? 160 - - -->result = junwrap(result) - result = - - Hello world! - - -->whos -name result - Name Type Size Bytes - - result string 1 by 1 72 - - // An Integer - -->result = myInspector.inspect(int32(150)); - Inspecting: '150' - Class: Integer - - -->result = junwrap(result) - result = - - 150 - - -->whos -name result - Name Type Size Bytes - - result int32 1 by 1 40 - - // A boolean - -->result = myInspector.inspect(%t); - Inspecting: 'true' - Class: Boolean - - -->result = junwrap(result) - result = - - T - - -->whos -name result - Name Type Size Bytes - - result boolean 1 by 1 16 - - - - As can be seen, all relevant data types are can be transformed transparently between Scilab and Java type. However this also extends without any additional effort to matrices; +--> jautoUnwrap(%f) // Make sure we disable the auto Unwrap + +--> result = myInspector.inspect("Hello world!"); +Inspecting: 'Hello world!' +Class: String + +--> whos -name result +Name Type Size Bytes +result _EObj ? 160 + +--> result = junwrap(result) +result = +Hello world! + +--> whos -name result +Name Type Size Bytes +result string 1 by 1 72 + +// An Integer +--> result = myInspector.inspect(int32(150)); +Inspecting: '150' +Class: Integer + +--> result = junwrap(result) +result = +150 + +--> whos -name result +Name Type Size Bytes +result int32 1 by 1 40 + +// A boolean +--> result = myInspector.inspect(%t); +Inspecting: 'true' +Class: Boolean + +--> result = junwrap(result) +result = +T + +--> whos -name result +Name Type Size Bytes +result boolean 1 by 1 16 + + + As can be seen, all relevant data types are can be transformed transparently between + Scilab and Java type. However this also extends without any additional effort to matrices; - --> jautoUnwrap(%t) // Make sure we come back in the default mode where Scilab auto unwrap all calls - - -->result = myInspector.inspect(1:5) - Inspecting: '[D@b05236' - Class: double[] - result = - - 1. 2. 3. 4. 5. - - -->whos -name result - Name Type Size Bytes - - result constant 1 by 5 56 - - -->result = myInspector.inspect(testmatrix('magi',3)) - Inspecting: '[[D@11d13272' - Class: double[][] - result = - - 8. 1. 6. - 3. 5. 7. - 4. 9. 2. - - -->whos -name result - Name Type Size Bytes - - result constant 3 by 3 88 - - When looking at the class of these wrapped matrices, it is clear that Java stores them as arrays of the appropriate size. When working with 2D matrices, the data in these equivalent Java arrays can be stored in are column-major (default) or row-major mode. In column-major mode, the first array contains a pointer to each of the columns. Whereas in row-major mode, the first array contains the pointers to each row of data. For more information see jautoTranspose. +--> jautoUnwrap(%t) // Make sure we come back in the default mode where Scilab auto unwrap all calls + +--> result = myInspector.inspect(1:5) +Inspecting: '[D@b05236' +Class: double[] +result = +1. 2. 3. 4. 5. + +--> whos -name result +Name Type Size Bytes +result constant 1 by 5 56 + +--> result = myInspector.inspect(testmatrix('magi',3)) +Inspecting: '[[D@11d13272' +Class: double[][] +result = +8. 1. 6. +3. 5. 7. +4. 9. 2. + +--> whos -name result +Name Type Size Bytes +result constant 3 by 3 88 + + + When looking at the class of these wrapped matrices, it is clear that Java stores them + as arrays of the appropriate size. When working with 2D matrices, the data in these + equivalent Java arrays can be stored in are column-major (default) or row-major mode. + In column-major mode, the first array contains a pointer to each of the columns. + Whereas in row-major mode, the first array contains the pointers to each row of data. + For more information see jautoTranspose. History @@ -311,7 +353,9 @@ jcompile("com.foo.Inspector",javacode); 5.5.0 - Function introduced. Based on the 'JIMS' module. The main difference in the behavior compared to the JIMS module is that jautoUnwrap is enabled by default. + Function introduced. Based on the 'JIMS' module. The main difference in the + behavior compared to the JIMS module is that + jautoUnwrap is enabled by default.