Andreas Rozek      

LuaJava_05 - the natural Lua Equivalents of Java Types

LuaJava_05 is the counterpart of LuaJava_04 and determines the Lua type to which a given Java value (or object) is converted upon return from a Java method.

Please, also consider my "Hints for Reading" and the "List of Recent Changes"!
(Problems displaying this page? Ugly graphics? Please, click here)

LuaJava_05

The script itself is extremely simple and should not require any further explanation. The accompanying Java source may be compiled using
 

  javac LuaJava_05.java

and does not even need the luajava package. The resulting class file should be copied to a place where it can be found by the Java class loader (e.g., into a directory which is automatically scanned by the Java extension mechanism).

After an invocation of the form
 

  java luna.LuaJava LuaJava_05.lua

the script produces the following output

LuaJava_05 - what are the "natural" Lua equivalents of Java types?

  Java2Lua: null     -> nil (nil)
  Java2Lua: boolean  -> boolean (true)
  Java2Lua: byte     -> number (123)
  Java2Lua: short    -> number (12345)
  Java2Lua: int      -> number (1234567)
  Java2Lua: long     -> number (123456789)
  Java2Lua: float    -> number (123.45600128174)
  Java2Lua: double   -> number (1.23456e+080)
  Java2Lua: char     -> userdata (userdata: 006DCAD0)
  Java2Lua: String   -> string (just a test)
  Java2Lua: Class    -> userdata (userdata: 006DAB50)
  Java2Lua: Instance -> userdata (userdata: 006DFE20)
  Java2Lua: Array    -> userdata (userdata: 006DF950)

which looks as one might expect:

  • null pointers are converted to "nil";
  • boolean values are converted to "boolean";
  • any kind of numeric value is converted to "number";
  • characters are not supported (as shown by LuaJava_03 already);
  • strings are converted to "string";
  • any other kind of object (including array types) is treated as "userdata".

An important outcome of this script is that Java arrays can not be inspected (nor modified) from within Lua directly - it will always be necessary to write an appropriate Java method and invoke this with the Java array as an argument.

Source Code

The source code of this script and its accompanying Java class is publically available:

Disclaimer

Please, also consider the author's Disclaimer!


http://www.Andreas-Rozek.de/LuaJava/Acquainting/LuaJava_05_en.html   (last Modification: 25.11.2004)