-
Notifications
You must be signed in to change notification settings - Fork 7
Tutorial
Less-Java is very similar to Java, with some simplified changes to syntax and function in order to promote understanding of concepts in a more simplified manner.
Here is a basic hello world program:
main() {
println("Hello, World")
}
Notice two things that differ from Java:
- Instead of public static void main(String[] args), the main method is recognized as main()
- Instead of System.out.println(“Hello, World!”), it is simply println(“Hello, World)
In Less-Java, you do not need to declare a type:
name = "Jane Doe"
age = 10
Usage of brackets are similar to that of Java. Here is a basic Car class example:
Car {
private make = ""
private model = ""
private speed = 0
Car(make, model) {
this.make = make
this.model = model
}
public getSpeed() {
return this.speed
}
}
Notice a few things in this code snippet that differ from Java:
- Semicolons are omitted
- No return type required in method declarations - this is due to internal type inferencing
printf(%s says %s, this.name, this.otherName)
println("newline")
print("no newline")
- Notice that printing to standard out is basically the same as Java, except the "System.out" is omitted in all cases for simplification
There are four native data types in LessJava.
- integer
- double
- boolean
- string
main() {
i = readInt()
s = readLine()
}
index() {
for (i: ["A", "B", "C"]) {
println(i)
}
}
- No conditional statement in the for loop
- No initialization or declaration of variable "i"
- Internal iterator; no need to increment "i"
In Less-Java, there are three basic collections: Lists, Sets, and Maps. They can be constructed with an explicit call to a constructor, or just by using initialization operators - such as brackets or lists.
- The List collection supports many functions such as add, push, remove, insert, get, etc.
- Initialization example:
list = List() - default constructor
list = [1, 2, 3, 4, 5] - explicit constructor
- The Set collection is similar to the List, but it supports very basic operations including add, remove, contains, and size.
- Initialization example:
set = Set{} - default constructor
set = {"A", "B", "C", "D"} - explicit constructor
- The Map collection is similar to the Java Map collection, but again supports very basic and widely used operations including put, get, remove, contains, and size.
- Initialization example:
map = Map() - default constructor
map = <"x", 10> \\ "x" is mapped to the value 10
Below is a list of all operations included in the List, Set, and Map collections:
- add
- push
- enqueue
- remove
- pop
- dequeue
- insert
- removeAt
- get
- set
- size
- add
- remove
- contains
- size
- put
- get
- remove
- contains
- size
In Less-Java, unit testing is very easy. Instead of creating a new test file/driver, one can just test their code following any method or class. Here is an example:
add(a, b) {
return a + b
}
test add(1, 2) == 3