Slides Java 1
Slides Java 1
Hendrik Speleers
Introduction to Java
●
Additional course material
– “Thinking in JAVA” (4th edition) by Bruce Eckel
– Free download: https://archive.org/details/TIJ4CcR1
●
Java programming
– Java Development Kit (JDK) from Oracle
●
Includes Java Runtime Environment (JRE) to run Java programs
●
Includes tools for Java development
●
Free download: https://www.oracle.com/technetwork/java/javase
– Java Integrated Development Environment (IDE)
●
Eclipse IDE for Java: very powerful and user friendly IDE
●
Free download: https://www.eclipse.org/downloads NMCGJ
2022-2023
Introduction to Java
●
Eclipse IDE for Java
NMCGJ
2022-2023
Introduction to Java
●
Programming
– Part of the software development process
Problem
Problemdefinition
definition Programming
Programming
Design
Design
and
andanalysis
analysis (code
(code/ /test
test/ /debug)
debug)
TARGET
●
Abstraction: problem space vs. solution space
NMCGJ
2022-2023
Introduction to Java
●
Programming languages: level of abstraction
– Low-level programming language Solution space
●
Easy conversion to machine code (machine)
●
Relatively non-portable abstraction:
move away from machine
– High-level programming language simplify problem description
●
Higher abstraction: in need of compiler
●
More readable, more portable Problem space
(what do you want?)
– Structured: Pascal, C, …
– Object-oriented: C++, Java
●
Describe problem in terms of problem elements: OBJECTS
NMCGJ
2022-2023
Introduction to Java
●
Java
– Developed at Sun Microsystems (now Oracle), 1995
●
First intended for programs in small devices
●
Syntax based on C and C++
– Two types of Java programs: applications – applets
– Platform independent: highly portable Java’s logo and mascot (Duke)
●
Java code (*.java) is compiled to byte code (*.class)
●
Byte code is executed in Java Virtual Machine (JVM)
– More user-friendly language than C/C++
●
Memory management: garbage collector
NMCGJ
2022-2023
Introduction to Java
●
Java
– History
●
Java
– History
NMCGJ
2022-2023
Introduction to Java
●
Java
– Java in numbers (according to Oracle)
●
95% of enterprise desktops run Java
●
1 billion Java downloads each year
●
9 million developers worldwide
●
#1 programming language
●
... Java’s logo and mascot (Duke)
NMCGJ
2022-2023
Objects and Classes
●
Alan Kay's 5 rules for Object-Oriented Programming (OOP)
– Everything is an object
●
An object is a fancy variable (storing data) + can perform operations
– A program is a bunch of communicating objects
●
Objects are communicating by sending messages
Alan Kay,
– Each object has its own memory made up of other objects inventor of
●
Hiding complexity behind simplicity of objects (=composition) Smalltalk
●
Programming with objects: the interface
Class diagram
– Class name Light
according to UML standard
switch() (Unified Modeling Language)
brighten()
Interface dim()
Light (1)
●
Programming with objects: the interface
Class diagram
– Class name Light
according to UML standard
Data status (Unified Modeling Language)
Interface switch()
...
Light (1)
●
Reusing the implementation
– Good design: reuse of classes, once created and tested
– Simplest way: creating member objects of a class
– Composing a new class from existing classes
●
Composition is as a “has-a” relationship
Car Engine
Wheel
Class diagram
NMCGJ
2022-2023
Objects and Classes
●
Reusing the interface
CoolingSystem
– Inheritance: derive functionality from a parent class
cool() ●
An “is-a” relationship: override parent class functions
off() ●
An “is-like-a” relationship: override + add new functions
– Polymorphism
●
Code assumes parent class, but not specific child class
(upcasting)
AirConditioner HeatPump
●
Add new child classes without effort
●
Method call determined at run-time
cool() cool()
off() heat()
off()
NMCGJ
2022-2023