Lua For Java Programmers

This is similar to the lua for python programmers, however I unfortunately failed the IQ test, and couldn’t figure out where to edit on the wikipedia.

This first thing you should really know is that** lua is not intented to be an OOP language.**

What is OOP? It means object oriented programming.

Further Explanation:

Originally, Lua was not designed to be used for OOP, but somehow it ended up have the potential to do so, I don’t remember don’t ask me. Still most of the time, Lua is not used for OOP.

------------------------------------------------------------------------------------------------

Lua is a great language, why is that? It is because Lua provides tremendous flexibility in coding and is a lot less tedious then most java work. Here are some examples of why.

Variable Instantiation:

In Java:

int num = 2;

In Lua: [LUA] local num = 2;[/LUA]

You see, in Lua all variables can start off defined without a variable type before it (ie right above…). To get a little more in-depth look at this:

[LUA]–I can define the variable, and then decide what to assign the value to.
Example;
Example = 3;

–OR

local Example;
Example = “Herrow”;

–OR

local Example;
Example = true;
[/LUA]

Scope of the Variable:

Normally in Java you would need to use scope types like public and private especially in classes, however in Lua variables are Global by default. To create a private/local scope, you simply type in ‘local’ in front of the variable.

Functions:

In Java:


public int GetNum()
{
return 3;
}
//Returns an int according to return type in function


public void SetNum(int num) //remember how you always needed the param. datatype?
{
int Num = num
}

Lua does not use braces(curly brackets) to open and close statements like in Java.

In Lua:

[LUA]
function GetNum() --You must have the word function at the start, no return type required
return 3
end --Instead of curly brackets, lua uses end statements.
[/LUA]

[LUA]
function SetNum(num) – You no longer need any parameter datatypes
local Num = num
end
[/LUA]

Tables(Arrays/ArrayLists)

One of the great things about lua tables, are that they are fucking magical, however they can be very confusing and I won’t exactly no how to explain them fully, so I’ll leave a link from the PiL that clarified the situation to me.

The best way I can explain Lua tables, is for me to compare them to ArrayLists. They are still VERY different, but you will see what I mean when we get further into it.

Part 1 of Tables

A simple instantiation of a table in lua looks like this:

[LUA]local Table = {} --There are many different ways to add things to these tables, and that is what makes them so magical.

–Here are a couple ways to add things into tables.

Table.StringOne = “One”; – These have indexes in table of StringOne and StringTwo (Not 1 and 2!!!).
Table.StringTwo = “Two”;

– OR

Table[1] = “One”; – These have numerical indexes
Table[2] = “Two”;

– OR

Table = {“One”, “Two”}; – These also have numerical indexes

– OR

Table = { One = “Two”, Two = “One” } – These have String indexes like the first example.
Table = { [“One”] = “Two”, [“Two”] = “One” } – Another way to assign values in tables.

[/LUA]

Really the best way to keep learning tables is actually to practice tables inside of Garrysmod itself. Here is why it looks like arraylists and not arrays:

[LUA]
local Table = {}
Table[1] = “ZOOM”; --String
Table[2] = 2; --Int
[/LUA]

Java’s arraylists are able to add in infinite types of objects(Unless you specify), so this what the best idea I could give you on how the tables worked.

A huge thing I forgot to mention the first time I posted are the index systems of the Tables and Arrays of Java.

The first item in an arrary in Java is always: [LUA] ray[0] [/LUA]

The first item in a Table for Lua however is: [LUA] ray[1] [/LUA]

To summarize, Java arrays are 0-based index systems, while Lua Tables are 1-based Systems. **This is Important!


** String Methods(I.e string.tolower()) **

In java you normal instantiate a variable and then call the various string methods through that string. However in Lua this is not the case.

In Java:

[LUA]
String Hi = “Hi”;
int length = Hi.length();
[/LUA]

In Lua:

[LUA]
local Hi = “Hi”
local length = string.len(Hi)
[/LUA]

This is much better than java because then you don’t really have to worry about always spelling the variable names right over and over again or anything else that could cause problems, you only have to use string.(Then the method).

See the link at the bottom for more details.


**Loops **

Ahh the mighty loops. First coming into Lua after programming in Java for two years they looked like fucking chinese to me. I will only be covering to kinds of loops today, While and For, mainly because no one ever uses do while loops in java anymore, however they are similar to Lua’s while loop.

In Java the for loop looks like this:

[LUA] for(int x=0;x<5;x++)
{
System.out.println(x);
}
[/LUA]

In Lua the for loop looks like this:

[LUA]
for x=0,5,1 do --You most likely do not need the 1, i am pretty sure the auto increment is 1, but that is just do show you how it is done like in java.
print(x)
end
[/LUA]

Now this may seem pretty simple, but their for loops for things like tables are around the same ballpark as the new “enchanced” java for loops that I could careless about using. (I prefer the old java loops). So today I will be showing you how I can easily just take a for loop with tables from java, and show you how to correctly use it in Lua.

In Java:

[LUA]
ArrayList listy = new ArrayList();
listy.add(1);
listy.add(2);
listy.add(3);
listy.add(4);
for(int x = 0; x<listy.size();x++)
{
System.out.println(listy.get(x));
}
[/LUA]

And this is how it would be done in Lua:

[LUA]
local Table = {}
Table[1] = 1
Table[2] = 2
Table[3] = 3
Table[4] = 4
for k,v in pairs(Table) do
print(v)
end
[/LUA]


Small Syntax

-If, Then, Else, End

[LUA]
if Num == 2 then
–Insert Code
elseif Num == 3 then
–Insert More Code
else
–Insert MOARR Code
end --ends the if statement
[/LUA]

-Comments

Lua:
[LUA]
– Rawr
// Rawr
/*
blah
blah
*/
–[[
blah
blah
]]
[/LUA]

Java:


// Rawr, this is the only method I ever used considering my IDE i could just ctrl + m and it would auto comment all for me I selected.

-Line Breaks

You may have noticed the ';'s I used in both of my coding for showing both of the languages. However, in Lua you do not need these unless you want to just write one long line of code and use it as a nice way of keeping track of what is what.

Further Table Explanation
String Function Explanation

Still WIP…

Updated Later: Added in more String support.
Updated March 19, 2010: Fixed minor syntax errors.
Updated Again 3/19/2010: Edited to use local variables upon request.

Very good and useful dorfy !

Java is a horrible, horrible language.

Just thought I’d put that out there.

Please remove any ; symbol from the lua code snippets, it is entirely useless and serves only to confuse people and start flamewars on facepunch.

One thing that I don’t think is stressed enough is that Lua tables are the center of Lua and the most important part of Lua - they are arrays, arraylists, classes, and more.

well, not COMPLETELY useless


if whatever then a = 53; b() end

Well most computer science classes in high school start with Java. So I decided maybe they should just learn off of this.

(The AP Test is based off of Java) and yes I agree Java is a pretty bad language.

the ‘;’ can be used either way in Lua its not really a big deal.


if whatever then a= 53 b() end

Exactly the same.

And let the flamewar commence. Personally I prefer writing Lua without ;

I wasn’t flaming, I was just saying that the ; isn’t even needed in that situation. I personally write Lua with the semi-colon because I am used to doing it in other languages.

I put it in by habit. Thanks PHP.

I’ve always thought Lua is a mix of BASIC and C-like syntax.

They are just ignored. They are useless, yes. Still if you are coding langues whiche NEED them, you make them automatically, so you better don’t stop in lua or you forget them in the other langues where you need them.
So it’s just a qustion of habits: Since they don’t hurt and all other languages I code use them, I will always add them in lua.

I don’t use them in Lua. My most common PHP error is to forget the semicolon, but I apparently always remember to do it in C/C++. Which is kinda weird.

I dislike the semicolon personally. In a 1024-line lua file (code lines) they’d already add one KiB to the size of the file!

I prefer using the semicolor there, makes it easier to distinguish where the “lines” are supposed to be.

I prefer to put line spaces.

[lua]if whatever then
a = 53;
b();
end[/lua]

I prefer not to use ;'s and put my entire script on one line, just for easy readability.

Also,
[lua]
/
Rawr
/

–[[
Rawr
–]]
[/lua]

You have these a little messed up. It should be:

[lua]
/*
blah
blah
*/
–[[
blah
blah
]][/lua]

That won’t work, you need to do either
[lua]Table = { One = “Two”, Two = “One” }[/lua]
or
[lua]Table = { [“One”] = “Two”, [“Two”] = “One” }[/lua]

Also, don’t forget the proper use of locals instead of having globals everywhere.

I already talked about using locals everywhere etc, however didn’t think it was really necessary to put it in my examples of coding.

@yakahuges Thanks, updated however the way i did the comments with --[[ still works with a --]], I use it that way all the time.

@Rabbish omg I totally blanked on table syntax while writing this so I was kind of in a rush.

People with low or no experience with Lua will use your examples as they are. Stay away from globals unless it’s absoletely necessary to use them.

Alright, I edited it to your liking tell me if there are any parts I missed.