Skip to content
jennahorrall edited this page Feb 10, 2020 · 8 revisions

Less-Java

An Introductory Programming Language

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.

Hello World

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)

Declarations

In Less-Java, you do not need to declare a type:

    name = "Jane Doe"
    age = 10

Basic syntax

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

Writing to standard out

    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

Reading from standard in

    main() {
        i = readInt()
        s = readLine()
    }

For Loops

    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"

Collections

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.

List

  • 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

Set

  • 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

Map

  • 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:

List

  • add
  • push
  • enqueue
  • remove
  • pop
  • dequeue
  • insert
  • removeAt
  • get
  • set
  • size

Set

  • add
  • remove
  • contains
  • size

Map

  • put
  • get
  • remove
  • contains
  • size

Unit Testing

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
Clone this wiki locally