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.
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.
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello world, I am here");
}
}
MyClass.main(null);
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.
return -15.0;
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>;
public class MyClass {
public static void main(String[] args) {
String x = "Hello world!";
x = "GGGE it!";
System.out.println(x);
}
}
MyClass.main(null);
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:
"Hello World"
is of type String
and evaluates to "Hello World"
.+
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
public class MyClass {
// assign "Hello world!" to x and then print it
public static void main() {
String x = "Hello world!";
System.out.println(x);
}
}
MyClass.main();
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.
}
}
MyClass.main();
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.
return (1 + 3) * 10; // some integer operators
return "Hello" + " " + "World!"; // the + operator on Strings is concatenation
int x = 3;
int y = 5;
int z = x * y;
return z;
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 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 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;