LEPL1402/LSINC1402
Java basics
What is Java?
• Java is a high level, robust, fast, secured and object oriented programming
language
• Used for:
• Android mobile applications
• Server applications
• Platform-independent desktop applications
Java History
• By James Gosling in 1995 from Sun Microsystems.
• Goals:
• Object Oriented language for general-purpose business applications
and for interactive, web-based internet applications.
• Provide platform independent alternative to C++.
• Easier to use than C++ (e.g., garbage collected)
• Sun was bought in 2009 by Oracle who now owns Java.
But OpenJDK provides a free open-source version https://openjdk.org/
Java is still evolving (syntax and lib)
• Version 1.0: 1996
• Version 1.1: 1997, major update (inner classes, JDBC,...)
• Version 1.2: 1998, major update (Swing, collections,...)
• Version 1.5: 2001, major update (generics, annotations,...)
• Version 1.8: 2013, major update (lambda, streams,...)
• Now called: "Java 8"
• Still popular in companies, some of them never updated to
newer versions!
• ...
• Java 24: many small new features since Java 8
• In this course, we will mostly use the language features of Java 8,
but we will also show you some newer features, especially of
Java 14
The book
• https://pschaus.github.io/book_lepl1402/
• The following slides are about the topics in the book chapter "From
Python to Java" (without the section "Exceptions")
• You must read the book. The slides do not contain all the information
you need to do the exercises!
Python vs Java
Python: Create a file "test.py"
Java: Create a file "Main.java"
Simple Java example
A static method is like a
A method must always be function in Python (= not
inside a class object oriented)
A public static method
called "main" is the starting
point for a Java program
Multiple static methods in a class
Multiple classes in one .java file
You can have only one
public class in a file. The file
must have the same name
as the class.
Types in Python
• In Python, all values have a certain type
• string: "Hello"
• integer: 123
• float: 123.45
• boolean: True / False
• class list: [1,2,3]
• Python is a strongly typed language
Python program using different types
int string
Types in Java
• Java is also a strongly typed language
• But it is also a statically typed language. In a Java program, the types of
the values of a variable are known at the beginning and do not change!
• This example is allowed in Python, but not in Java:
Java program using different types
You (the programmer)
must declare the type of
each variable!
The return type
• Methods that return a result have a return type
This method returns a value of type int
This method does not return anything (void)
The Java compiler and the JVM
• Java is a compiled language
• Java compiler = a tool that takes Java source code and produces class
files that are executed by the Java Virtual Machine (JVM)
Your Class file for Executed
source Compiler JVM
each class program
code
Compile-time errors: Syntax Run-time errors:
errors, type errors,... Division by zero,...
Type checking
• The Java compiler will check the usage of types in your source code
before producing the class files
• Correct code (accepted by the compiler):
• Wrong code (not accepted by the compiler):
What is a variable in Java?
• A variable is a box that can hold a value
• If the variable is a primitive type like int or float, the value is a number
• An assignment means to copy the value from one box to the other box
In the book
• In the book you find more information about primitive types and the
differences between the types int, float, double,...
Local variables and parameters
Local variables Parameter variables get
only exist inside a their value, when the
method method is called
Class variables Class variables exist outside a
method. They belong to the
class and are visible to all
methods in the class.
Arrays (français: tableaux)
• Arrays can store multiple values of the same type
0 0 0 0
Arrays must be This array has space
created for four int values
prints 5
An array variable contains a reference to
the array, not the array itself!
An array is not
a primitive type
prints 5
Creating arrays
• You must create an array before you can use it
• You cannot change the size of an array later!
• If you have an array variable and you want to specify that the variable
currently does not contain a reference to an array, you can use null:
int[] a = null; // no array
a[2] = 5; // run time error
...
a = new int[4]; // create the array
a[2] = 5; // okay
In the book
• In the book you find more information about how to create arrays and
how to work with multi-dimensional arrays
While loop
• While loops in Java are similar to while loops in Python
Single statements vs block statements
• In Java, you must use parentheses { ... } to indicate whether statements
belong together
• Without { ... }, a while loop only has one statement
In the while loop
Not in the while
loop!
For loops with arrays
• For loops can be used to iterate over all elements of an array
An array with
four elements
Flexible for loop
• The for loop in Java (and in C and C++) has another, more flexible form
End
Start After each
condition
iteration
In the book
• In the book you find more information about loops (break and
continue statement)
If/else statement
• If and if/else statements work like in python
In the book
• In the book you find more information about logical operators like !
(not), && (and), || (or),...
Another if/else example
char type = primitive
type for single
characters
Switch/case
• The switch-case construct is like a chain of if/else if/else if/... on the
same variable
Strings
• Strings in Java are similar to strings in Python
String variables
• A string variable contains a references to the string
String object
• Strings are not primitive types like int or float. String values are objects
of the String class.
• Many interesting operations for strings are methods:
In the book
• In the book you‘ll find more information about how to work with
strings
Comparing values
• Primitive-type values (int, float,...) can be compared with ==
• This does not work for arrays or strings! Remember: array and string
variables contain references, not values.
False. Two different
arrays!
True. Same array.
Comparing strings or arrays
• Correct way to compare two arrays a and b:
A method from the Arrays class that
compares the contents of the arrays
• Correct way to compare two strings s1 and s2:
In the book
• In the book you will learn what enumerations are
The last secret...
• What are the parameters of the main method?
• This array (sometimes written as String... args) contains the
command line arguments when you start a Java program from a
console or shell:
java Main Peter Paul Mary
args[0], args[1], args[2]