3. The Basics

In this chapter, we’ll cover the basic syntax of Java (and similar) languages.

3.1 Coding Terms

Source file refers to human-readable code. Binary file refers to computer-readable code (the compiled code).

In Java the source files end with “.java” and binary files end with “.class” (also called “class files”). You compile source files using a compiler which gives you binary files.

In Java the compiler is called javac, in Groovy it is groovyc, and scalac in Scala (see a trend here?).

However, some languages (such as JavaScript) don’t need to be compiled (they’re called interpreted languages).

3.2 Primitives and Reference

Primitive types in Java refer to different types of way to store numbers (which have historical but also practical significance):

  • char: A single character, such as ‘A’ (the letter A).
  • byte: A number from -128 to 127 (8 bits1). Typically a way to store or transmit raw data.
  • short: A 16-bit signed integer. It has a maximum of around 32 thousand.
  • int: A 32-bit signed integer. Its maximum is around 2 to the 31st power.
  • long: A 64-bit signed integer. Maximum of 2 to the 63rd power.
  • float: A 32-bit floating point number. This is a non-precise value that is using for things like simulations.
  • double: Like float but with 64-bits.
  • boolean: Has only two possible values: true and false (much like 1 bit).

See Java Tutorial - Data Types for more information.

Groovy, Scala, and JavaScript

Groovy types are much the same as Java’s. In Scala, everything is an object, so primitives don’t exist. However they are replaced with corresponding value types (Int, Long, etc.). JavaScript has only one type of number, Number, which is similar to Java’s float.

Every other type of variable in Java is a “reference” - it points to some Object in memory. You can think of this as something like an address.

3.3 Strings/Declarations

A String is a list of characters (text). It is a very useful built-in class in Java (and most languages). To define a String you simply surround some text in quotes. For example:

1 String hello = "Hello World!";

Here the variable hello is assigned the String “Hello World!”.

In Java you must put the type of the variable in the declaration. That’s why the first word above is String.

In Groovy and JavaScript, Strings can also be surrounded by single-quotes ('hello'). Also, declaring variables is different in each language. Groovy allows you to use the keyword def, while JavaScript and Scala use var. For example:

1   def hello = "Hello Groovy!" //groovy
2   var hello = "Hello Scala/JS!" //Scala or JS

3.4 Statements

Every statement in Java must end in a semi-colon (;). In many other languages (like Scala, Groovy, and JavaScript) the semi-colon is optional, but in Java it is necessary. Much like how periods at the end of each sentence help you understand the written word, the semi-colon helps the compiler understand the code.

By convention we usually put each statement on its own line, but this is not required as long as semi-colons are used to separate each statement.

3.5 Assignment

Assignment is an extremely important concept to understand but it can be difficult for beginners. However, once you understand it you forget how hard it was to learn.

Let’s start with a metaphor. Imagine you want to hide something valuable, like a gold coin. You put it in a safe place and write the address on a piece of paper. This paper is like a “reference” to the gold. You can pass it around and even make copies of it, but the gold remains in the same place and does not get copied. On the other hand, anyone with the reference to the gold can get to it. This is how a reference variable works.

Let’s look at an example:

1 String gold = "Au";
2 String a = gold;
3 String b = a;
4 b = "Br";

After running the above code, gold and a refer to the String “Au” while b refers to “Br”.

3.6 Class and Object

A class is the basic building block of code (in Object-Oriented languages). A class typically defines state and behavior. The following class is named “SmallClass”:

1 package com.example.mpme;
2 public class SmallClass {
3 }

Class names always start with an uppercase letter in Java. It’s common practice to use “Camel Case” to construct the names. This means instead of spaces (or anything else) to separate words, we write the first letter of each word in uppercase.

The first line is the package of the class. A package is like a directory on the file system. In fact, in Java, the package must actually match the path to the Java source file. So the above class would be located in the path com/example/mpme/ in the source file system.

An object is an instance of a class in memory. Since a class can have multiple values within it, an instance of a class will store those values.

Create a Class

  • Open up your IDE (NetBeans).
  • Right-click on your Java Project and choose “New” ⇒ “Java Class”.

Properties and Methods

Next you might want to add some properties and methods to your class. A property is a value associated with a particular object. A method is a block of code on a class.

1 package com.example.mpme;
2 public class SmallClass {
3     String name;
4     String getName() {return name;}
5     void print() {System.out.println(name);}
6 }

In the above code name is a property and getName and print are methods.

Groovy Classes

Groovy is extremely similar to Java but always defaults to public.

1 package com.example.mpme;
2 class SmallClass {
3     String name //property
4     def print() { println(name) } //method
5 }

Groovy also automatically gives you “getter” and “setter” methods for properties, so writing the getName method would have been redundant.

JavaScript prototypes

Although JavaScript has objects, it doesn’t have a the class keyword. Instead, it uses a concept called the prototype. For example, creating a class looks like the following:

1 function SmallClass() {}
2 SmallClass.prototype.name = "name"
3 SmallClass.prototype.print = function() { print(this.name) }

Here name is a property and print is a method.

Scala Classes

Scala has a very concise syntax which puts the properties of a class in parenthesis. For example:

1 class SmallClass(var name:String) {
2     def print = println(name)
3 }

Creating a New Object

In all four languages, creating a new object uses the new keyword. For example:

1 sc = new SmallClass();

3.7 Comments

As a human, it is sometimes useful for you to leave notes in your source code for other humans and even for yourself later. We call these notes comments. You write comments thusly:

1 String gold = "Au"; // this is a comment
2 String a = gold; // a is now "Au"
3 String b = a; // b is now "Au"
4 b = "Br"; 
5 /* b is now "Br".
6    this is still a comment */

Those last two lines demonstrate multiline comments. So in summary:

  • Two forward slashes denote the start of a single-line comment.
  • Slash-asterisk marks the beginning of a multiple-line comment.
  • Asterisk-slash marks the end of a multiple-line comment.

Comments are the same in all languages covered by this book.

3.8 Summary

In this chapter we learned the basic concepts of programming:

  • Compiling source files into binary files.
  • How objects are instances of classes.
  • Primitive types, references, and Strings.
  • Variable assignment.
  • How Source code comments work.