Benutzung von Java API Klassen | Using Java API classes:


java.lang.String

= #JxnPortable\docs\programmer_examples\UsingJavaAPI_1_String.jxn

abc = "abc"
 = abc (java.lang.String)
xyz = "xyz"
 = xyz (java.lang.String)
s = abc + "..." + xyz
 = abc...xyz (java.lang.String)
s.substring( 2, s.length() - 2 )
 = c...x (java.lang.String)
s.toUpperCase().replace( ".", "-" )
 = ABC---XYZ (java.lang.String)
!
! unicode characters (4 hex digits):
s = "\u03b1\u03b2\u03b3\u03b4123"
 = αβγδ123 (java.lang.String)
hexString s
 = 03b1 03b2 03b3 03b4 0031 0032 0033 (java.lang.String)
!
! unicode characters (decimal digits followed by ';' or any non digit character):
s = "\#945\#946\#947\#948;123"
 = αβγδ123 (java.lang.String)
hexString s
 = 03b1 03b2 03b3 03b4 0031 0032 0033 (java.lang.String)
s.indexOf "1"
 = 4 (int)

java.lang.System

= #JxnPortable\docs\programmer_examples\UsingJavaAPI_2_System.jxn

start = System.currentTimeMillis()
Thread.sleep( 1000 )
System.currentTimeMillis() - start

start = System.nanoTime()
! generate 10 Mio. gaussian distributed random numbers:
n = 10_000_000
n = KmgLinSkal.floor125( Runtime.getRuntime().maxMemory() / 32 ).intValue()
z = JxnRealArrayAlgebra.noise( n )
0.000_000_001 ( System.nanoTime() - start )

start = System.currentTimeMillis()
y = sort z
0.001 ( System.currentTimeMillis() - start )

plot( y.range( 0, 50 ) ).setPlotFrameTitle( "smallest values of the distribution" )
plot( y.range( n / 2 - 5000, n / 2 + 5000 ) ).setPlotFrameTitle( "values near the median of the distribution" )
! plotting all values takes some time:
! plot( y )

java.util.Vector

= #JxnPortable\docs\programmer_examples\UsingJavaAPI_3_Vector.jxn

v = @Vector()  ! <- @Vector() stands for "new Vector()" in Java
 = [] (java.util.Vector)
v.add( "abc" )
 = true (boolean)
v + @Double(2.)  ! <- abbreviation of v.add( @Double(2.) )
 = true (boolean)
v + { 3., 5., 7. }
 = true (boolean)
v + "xyz"  ! <- maps to v.add( "xyz" ): adds "xyz" to Vector v
 = true (boolean)
v
 = [abc, 2.0, [D@39ab89, xyz] (java.util.Vector)
! but:
">>>" + v + "<<<"  ! adds v.toString() to String ">>>"
 = >>>[abc, 2.0, [D@39ab89, xyz]<<< (java.lang.String)
!
v.size()
 = 4 (int)
v.get(1)
 = 2.0 (java.lang.Double)
v[2]  ! <- abbreviation of v.get(2)
 = { 3.0, 5.0, 7.0 } (double[3])
v[1:]
 = { 2.0, [D@39ab89, xyz } (java.lang.Object[3])
format v
 = { abc (java.lang.String),
 =   2.0 (java.lang.Double),
 =   { 3.0,
 =     5.0,
 =     7.0 } (double[3]),
 =   xyz (java.lang.String) } (java.lang.Object[4]) from java.util.Vector (java.lang.String)

java.util.Hashtable

= #JxnPortable\docs\programmer_examples\UsingJavaAPI_4_Hashtable.jxn

ht = @Hashtable()
 = {} (java.util.Hashtable)
ht.put( "one", @Double(1.) )
 = null
ht.put( "two", @Double(2.) )
 = null
ht.put( "three", @Double(3.) )
 = null
ht.put( "four", @Double(4.) )
 = null
ht
 = {two=2.0, one=1.0, three=3.0, four=4.0} (java.util.Hashtable)
ht.size()
 = 4 (int)
ht["two"]  ! <- abbreviation of ht.get("two")
 = 2.0 (java.lang.Double)

ht2 = @Hashtable()
 = {} (java.util.Hashtable)
key = { "zero", "one", "two", "three", "four" }
 = { zero, one, two, three, four } (java.lang.String[5])
val = { "null", "eins", "zwei", "drei", "vier" }
 = { null, eins, zwei, drei, vier } (java.lang.String[5])
! since put takes parameters of type java.lang.Object JxnUnroll is used to force automatic array loop processing
ht2.put( @JxnUnroll key, @JxnUnroll val )
 = { null, null, null, null, null } (java.lang.Object[5])
ht2
 = {two=zwei, one=eins, three=drei, zero=null, four=vier} (java.util.Hashtable)
ht2.size()
 = 5 (int)
ht2["three"]
 = drei (java.lang.String)

java.math.BigInteger  und | and   #import (package)

= #JxnPortable\docs\programmer_examples\UsingJavaAPI_5_BigInteger.jxn

a = @java.math.BigInteger("3")
 = 3 (java.math.BigInteger)

! #import java.math.BigInteger or
! #import java.math.* or
#import java.math
 = 7. added, View > PackageSearchList shows all (java.lang.String)

! now BigInteger is found without "java.math."
b = @BigInteger(7)  ! @BigInteger(int) works in JXN via auto toString
 = 7 (java.math.BigInteger)

! Automatic mapping to the arithmetic methods (multiply, subtract, ...) of java.math.BigInteger:
-a
 = -3 (java.math.BigInteger)
a b
 = 21 (java.math.BigInteger)
a - b
 = -4 (java.math.BigInteger)
a + b
 = 10 (java.math.BigInteger)
b / a
 = 2 (java.math.BigInteger)
b % a
 = 1 (java.math.BigInteger)
a^150
 = 369988485035126972924700782451696644186473100389722973815184405301748249 (java.math.BigInteger)

! Fibonacci numbers:
n = 1000
 = 1000 (int)
f0 = BigInteger.ZERO; f1 = BigInteger.ONE; JxnLoop.repeat( $this, "", n-1, "f2 = f1 + f0; f0 = f1; f1 = f2;" )
 = 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875 (java.math.BigInteger)
$.doubleValue()
 = 4.3466557686937455E208 (double)

! but (mixed arithmetic with int not applicable for java.math.BigInteger because of missing public BigInteger(int) construtor):
a 2
? term: java.math.BigInteger * int not supported
             see also using wrapper class BigIntegerAlgebra (supports mixed arithmetic)

#import static

= #JxnPortable\docs\programmer_examples\ImportStaticDemo.jxn

a = { 5., 13., 3., 2., 7. }
 = { 5.0, 13.0, 3.0, 2.0, 7.0 } (double[5])
b = Arrays.copyOf( a, length a )
 = { 5.0, 13.0, 3.0, 2.0, 7.0 } (double[5])
! Note: "Arrays.methodName(.)" can be used instead of "java.util.Arrays.methodName(.)"
!       because of an implicit "#import java.util.*" during jxn initialization
set( b, 1, 11. )  ! <= calls JxnUtilities.set( double[], int, double )
 = { 5.0, 11.0, 3.0, 2.0, 7.0 } (double[5])
Arrays.sort b
 = null
Arrays.toString b
 = [2.0, 3.0, 5.0, 7.0, 11.0] (java.lang.String)
!
! the following command adds "java.util.Arrays" to the list of classes automatically searched for static methods:
! ( on initialization class java.lang.Math and class JxnUtilities are added to the static method class list )
#import static java.util.Arrays
 = 1. added, View > StaticMethodClasses shows all (java.lang.String)
! now even the "Arrays." can be omitted
! => static methods of java.util.Arrays (e.g. deepToString(String) ) are found without explicit classname
toString a
 = [5.0, 13.0, 3.0, 2.0, 7.0] (java.lang.String)
!
c = { 1., a, b }
 = { 1.0, [D@16f77cc, [D@aa6d82 } (java.lang.Object[3])
toString c
 = [1.0, [D@16f77cc, [D@aa6d82] (java.lang.String)
deepToString c
 = [1.0, [5.0, 13.0, 3.0, 2.0, 7.0], [2.0, 3.0, 5.0, 7.0, 11.0]] (java.lang.String)