Interfacing to Java
Specifying a call to the Java environment
You can interface to Java by supplying 'java' as the environment string (left argument) for
Setting Java options
You can set various options for the Java virtual machine by using the system function
See the documentation on
Conversion of Java data types to APL data
APLX by default applies the following data conversion rules to data returned from Java:
Anything else is left as an object in the Java environment, and a reference to the object is returned to APL.
There are some special cases to consider. The data might not be convertible at all, or it might lose precision in the conversion. To handle cases like this, APLX provides the
An example which cannot be represented at all is where a Java Double contains a NaN (Not A Number). APL does not handle NaNs, so it cannot be converted to an APL floating-point value. Instead, NaNs are left as Java objects. If you try to use the data in an APL expression, you will get a DOMAIN ERROR, but you can see that it is a NaN and use Java methods on it.
Using the Java interface from multiple APL tasks
Because it is not safe to call the Java virtual machine from multiple threads, you cannot use the Java interface from more than one APL task at a time. If you try to do so, you will get an error message and a DOMAIN error:
'java' ⎕NEW 'java.util.Date' Java Virtual Machine (JVM) is already in use by another APL task DOMAIN ERROR 'java' ⎕NEW 'java.util.Date' ^
The lock will be cleared when the APL task which has been accessing Java executes a )CLEAR, )LOAD, or )OFF.
∇DEMO_TimeZone;date;tzclass;tz;dateFormat;dateList  ⍝ Demonstration of using a TimeZone object in Java  ⍝  ⍝ First create a date  date←'java' ⎕NEW 'java.util.Date'  ⍝  ⍝ What is the date?  'Result of date.toString: ',date.toString  ''  ⍝ To create a TimeZone object we need to call a static  ⍝ method in the TimeZone class  tzclass←'java' ⎕GETCLASS 'java.util.TimeZone'  tz←tzclass.getTimeZone 'America/Los_Angeles'  ⍝  ⍝ Could also call the static method directly...  tz←'JAVA' ⎕CALL 'java.util.TimeZone.getTimeZone' 'America/Los_Angeles'  ⍝  ⍝ Does this time zone use daylight savings time?  'Result of tz.useDaylightTime: ',tz.useDaylightTime  ''  ⍝ What is the time zone called with and without daylight savings time?  'Result of tz.getDisplayName: ',tz.getDisplayName 1(tz.LONG)  'Result of tz.getDisplayName: ',tz.getDisplayName 0(tz.LONG)  ''  ⍝ What is the current date/time in our local time zone?  ⍝ We create a SimpleDateFormat object to format the date  dateFormat←'java' ⎕NEW 'java.text.SimpleDateFormat' 'EEE, d MMM yyyy HH:mm :ss, zzzz'  'Today''s local date/time is: ',dateFormat.format date  ⍝  ⍝ What's the same date/time in Los Angeles?  dateFormat.setTimeZone tz  'In Los Angeles, that''s: ',dateFormat.format date  ''  ⍝  ⍝ Let's make 12 dates with different months  dateList←date.⎕CLONE 12  dateList.setMonth((⍳12)-⎕IO)  ⍝  ⍝ Format each of these dates for Los Angeles time  'Here are some more dates with the month changed:'  12 1⍴dateFormat.format¨dateList  ∇
This produces the following output:
Result of date.toString: Tue Nov 20 14:37:36 GMT 2007 Result of tz.useDaylightTime: 1 Result of tz.getDisplayName: Pacific Daylight Time Result of tz.getDisplayName: Pacific Standard Time Today's local date/time is: Tue, 20 Nov 2007 14:37:36, Greenwich Mean Time In Los Angeles, that's: Tue, 20 Nov 2007 06:37:36, Pacific Standard Time Here are some more dates with the month changed: Sat, 20 Jan 2007 06:37:36, Pacific Standard Time Tue, 20 Feb 2007 06:37:36, Pacific Standard Time Tue, 20 Mar 2007 07:37:36, Pacific Daylight Time Fri, 20 Apr 2007 06:37:36, Pacific Daylight Time Sun, 20 May 2007 06:37:36, Pacific Daylight Time Wed, 20 Jun 2007 06:37:36, Pacific Daylight Time Fri, 20 Jul 2007 06:37:36, Pacific Daylight Time Mon, 20 Aug 2007 06:37:36, Pacific Daylight Time Thu, 20 Sep 2007 06:37:36, Pacific Daylight Time Sat, 20 Oct 2007 06:37:36, Pacific Daylight Time Tue, 20 Nov 2007 06:37:36, Pacific Standard Time Thu, 20 Dec 2007 06:37:36, Pacific Standard Time
Copyright © 1996-2010 MicroAPL Ltd