Lua for Java Programmers — Unfamiliar Territory

Every language needs something to set itself apart from the crowd, something it does better or even differently from the rest to be noticed. This post will cover what really sets Lua apart from the norm and covers the major hurdles a Java programmer will need to overcome to effectively use the Lua language. The topics are: Chunk Statement Structure, Tables, Functions, and Coroutines.

Chunk Statement Structure

Even a beginner to the Lua language likely knows that you do not need semicolons to end a statement. But what may surprise some programmers is that Lua statements are not terminated with line breaks either. Instead, Lua will automatically detect when a statement is “complete” based on syntax. When using the Lua standalone interpreter, attempting to submit an incomplete statement will result in the program prompting you for more information. This is shown by having a double angle brace (>>) at the start of the line. When you have provided a complete statement, the interpreter will execute your code, or as it is known in Lua, your “chunk.” In fact, a chunk can be much more than a single statement. Chunks of executable code can come in the form of entire files or even a long string of valid statements written on the command line. This allows you to execute as much or as little code as you need to.


An uninformed Java programmer will likely take a look at Tables and think of Arrays. This is not wrong as tables can be used for arrays, but the real power of tables is that they can be used for so much more. Tables are in fact the ONLY data structure in Lua and can be used for just about anything you would need a data structure for. This includes Arrays, Lists, Queues, Key-Value Mapping and even custom Packages. This is because a table is not strictly indexed numerically, although it is automatically done so for arrays. Tables can be indexed with any datatype available in Lua exempting nil. Tables also do not have a fixed size, so elements can be added and removed from a table freely. The Table Standard Library provides even more functionality to these powerful objects, so a good programmer will have all the tools they need to structure data.


In Java you’d call these things methods, and they are used for many of the same things. However, there are a couple big things to note. First, functions are one of the core datatypes in Lua, and are therefore assigned to variables like numbers or strings are. Thankfully for ease of use, a function can be defined in a traditional syntax as well. Both of the following are identical definitions.

function cube(num)
return num*num*num
cube = function(num)
return num*num*num

Secondly, functions are allowed to return more than one value. This has many practical uses in both user defined functions and in the standard libraries. To accept multiple return values, you can either use multiple assignment like this:

x,y,z = thisReturnsThree()

Or you can capture the results in a table, which can be useful for functions that return an indeterminate number of results:

x = {thisReturnsMany()}


Coroutines are Lua’s take on threading. When a new coroutine is created, you have effectively spawned a little side process that will have its own stack, variables, and program counter. However, do not be fooled by the thread-like functionality because coroutines are NOT run simultaneously like a Java thread is. Switching control of a coroutine is done by manually calling the yield function and then resuming another coroutine. This limits their usefulness, but there are still some useful applications for them.


With this information in mind, you should have a better time using Lua to its full power. Now that you have an understanding of what you can expect from Lua’s Statement Structure, Tables, Functions, and Coroutines; you can move forward towards making great things in Lua.

Like what you read? Give David Black a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.