Java Basics

We revew some java fundamentals to get started with programming without caring to much about fully understanding every piece of syntax we are using (this will come later). Source code in Java is stored in files with the extension .java. We will cover the details of the structure of these files later. For now, we will use a template file structure to explain some basic Java concepts. The content has to be stored in a file named MyClass.java.

public class MyClass { 

    public static void main(String[] args) {
        <some_code>
    }

}

We will replace <some_code> with some basic Java code. See the course materials webpage for instructions on how to install Java and how to compile Java source code and run compiled Java byte code. Ignore the String[] args part for now.

String literals and printing to the console

A String is a sequence of characters. In Java strings are enclosed in double quotes ("). For example "A", and "Hello World!" are literal String values in Java. To print to the standard output you use System.out.println(<string>) in Java. Here <string> is an expression that evaluates to the type String, e.g., a string literals as the ones shown above. println is what is called a method in Java. A callable piece of code that you pass information though its parameters that are passed in parentheses. In the case of println there is a single parameter that is what should be printed on the console.

In [3]:
public class MyClass { 

    public static void main(String[] args) {
        System.out.println("Hello world, I am here");
    }

}
Out[3]:
com.twosigma.beaker.javash.bkrf9bb85f2.MyClass
In [4]:
MyClass.main(null);
Hello world, I am here
Out[4]:
null

Numerical literals

In Java numerical literals are either integers or floating point numbers. For instance, 1 and -5 are integers while 3.0, and -123.345 are floating point numbers.

In [5]:
return -15.0;
Out[5]:
-15.0

Statements, Variables, and Assignment

A statement in Java is ended with a ;. When writing a program you can use variables to store information. Variables are of a certain type that determines what kind of information can be stored in the variable, e.g., numbers of strings. To store some information in a variable you have to assign a value to the variable. In Java assignment is of the form:

<variable_name> = <value> ;

Before using a variable can be used, you have to declare it. A variable declaration if of the form:

<type> <variable_name>;

It is possible to combine assignment with declaration like this:

<type> <variable_name> = <value>;
In [1]:
public class MyClass { 

    public static void main(String[] args) {
        String x = "Hello world!";
        x = "GGGE it!";
        System.out.println(x);
    }

}
Out[1]:
com.twosigma.beaker.javash.bkrde74df4d.MyClass
In [2]:
MyClass.main(null);
GGGE it!
Out[2]:
null

Expressions

Expressions in Java consist of method calls, literals, variable references, and operators (e.g., arithmetic operators like addition +). An expression evaluates to a value of a certain type based on the following rules:

  • literals: A literal evaluates to itself, e.g., the value of the expression "Hello World" is of type String and evaluates to "Hello World".
  • variables: Evaluate to the value that is currently assigned to them
  • method: Evaluate to the value returned by the methods (more on that later)
  • operators: combine their inputs in an operator-specific way. For example, the + operator takes two expressions of a numerical type and returns their sum
(1 + 3) * 4

The above expression combines 3 integer literals using operators + and *. Using elementary school arithmethics you can confirm that it evaluates to 16

In [18]:
public class MyClass { 

    // assign "Hello world!" to x and then print it
    public static void main() {
        String x = "Hello world!";
        System.out.println(x);
    }

}
Out[18]:
com.twosigma.beaker.javash.bkr07309595.MyClass
In [19]:
MyClass.main();
Hello world!
Out[19]:
null
In [10]:
public class MyClass { 

    // assign "Hello world!" to x and then print it
    public static void main() {
        int x = 3;
        int y = 4;
        System.out.println(x + y); // the arguments passed to a method can also be expression, the method is called with the result of the expression.
    }

}
Out[10]:
com.twosigma.beaker.javash.bkrf9bb85f2.MyClass
In [11]:
MyClass.main();
7
Out[11]:
null

Above we simplified a bit. The right-hand side of an assignment can also be an expression. From now on we will omit the template code in MyClass.java and just focus on the code itself.

In [12]:
return (1 + 3) * 10; // some integer operators
Out[12]:
40
In [13]:
return "Hello"  + " " + "World!"; // the + operator on Strings is concatenation
Out[13]:
Hello World!
In [14]:
int x = 3;
int y = 5;
int z = x * y;
return z;
Out[14]:
15

Comments

Comments are used to document code. Comments are ignored by the compiler, i.e., they do not affect the generated byte code.

Single Line Comments

Single line comments in Java start with // and extend towards the end of the line (see above for several examples)

int x = 5; // this is a single line comment
int y = ...

Multi Line Comments

Multi line comments start with /* and extend until the first following */

int x = 5;
/* This
   is a comment
   that spans multiple lines
   and here is where it ends: */
x = 7;