JavaProgrammingForBeginners-Presentation-16-30
JavaProgrammingForBeginners-Presentation-16-30
class Planet
name, location, distanceFromSun // data / state / fields
rotate(), revolve() // actions / behavior / methods
A class is a template.
In above example, Planet is a class
An object is an instance of a class.
earth and venus are objects.
name, location and distanceFromSun compose object state.
rotate() and revolve() define object's behavior.
Fields are the elements that make up the object state. Object behavior is
implemented through Methods.
16
Object Oriented Programming (OOP) - 2
class Planet
name, location, distanceFromSun // data / state / fields
rotate(), revolve() // actions / behavior / methods
17
Next Few Sections
Java keeps improving:
Java 10, Java 11, Java 12, ..., Java 17, Java 18 ...
Developing Java Applications is Evolving as well:
Spring
Spring Boot
REST API
How about building a Real World Java Project?
REST API with Spring and Spring Boot
Let's get started!
18
Java Versioning
Version Release Data Notes
Java SE 11 (LTS) September 2018 Long Term Support Version (Every 3 years)
...
19
Java New Features
Version Release Data Important New Features
J2SE 5.0 Sep 2004 Enhanced For Loop, Generics, Enums, Autoboxing
Java SE 8 (LTS) Mar 2014 Functional Programming - Lambdas & Streams, Static methods in interface
All Java Versions - API Improvements, Performance and Garbage Collection Improvements
20
Java Modularization - Overview
Introduced in Java 9
Goals:
Modularize JDK (IMPORTANT)
rt.jar grew to 60+ MB by Java 8
Modularize applications
Modularizing JDK:
java --list-modules
java.base
java.logging
java.sql
java.xml
jdk.compiler
jdk.jartool
jdk.jshell
java -d java sql
21
Java Modularization - Remember
Module Descriptor - module-info.java: Defines metadata about the module:
requires module.a; - I need module.a to do my work!
requires transitive module.a; - I need module.a to do my work
AND my users also need access to module.a
exports - Export package for use by other modules
opens package.b to module.a - Before Java 9, reflection can be used to find details about
types (private, public and protected). From Java 9, you can decide which packages to expose:
Above statement allows module.a access to perform reflection on public types in package.b
Advantages
Compile Time Checks
For availability of modules
Better Encapsulation
Make only a subset of classes from a module available to other modules
Smaller Java Runtime
Use only the modules of Java that you need!
22
Local Variable Type Inference
// List<String> numbers = new ArrayList<>(list);
var numbers = new ArrayList<>(list);
23
Switch Expression
String monthName = switch (monthNumber) {
case 1 -> {
System.out.println("January");
// yield statement is used in a Switch Expression
// break,continue statements are used in a Switch Statement
yield "January"; // yield mandatory!
}
case 2 -> "February";
case 3 -> "March";
case 4 -> "April";
default -> "Invalid Month";
};
24
Text Blocks
System.out.println("\"First Line\"\nSecond Line\nThird Line");
System.out.println("""
"First Line"
Second Line
Third Line"""
);
25
Records
record Person(String name, String email, String phoneNumber) { }
26
Getting Started with Spring Framework - Goals
Build a Loose Coupled Hello World Gaming App
with Modern Spring Approach
Get Hands-on with Spring and understand:
Why Spring?
Terminology
Tight Coupling and Loose Coupling
IOC Container
Application Context
Component Scan
Dependency Injection
Spring Beans
Auto Wiring
27
Loose Coupling with Spring Framework
Design Game Runner to run games:
Mario, Super Contra, PacMan etc
Iteration 1: Tightly Coupled
GameRunner class
Game classes: Mario, Super Contra, PacMan etc
Iteration 2: Loose Coupling - Interfaces
GameRunner class
GamingConsole interface
Game classes: Mario, Super Contra, PacMan etc
28
Spring Framework - Questions
Question 1: What's happening in the background?
Let's debug!
Question 2: What about the terminology? How does it relate
to what we are doing?
Dependency, Dependency Injection, IOC Container, Application Context,
Component Scan, Spring Beans, Auto Wiring etc!
Question 3: Does the Spring Framework really add value?
We are replacing 3 simple lines with 3 complex lines!
Question 4: What if I want to run Super Contra game?
Question 5: How is Spring JAR downloaded?
Magic of Maven!
29
Question 1: What's happening in the background?
Let's Debug:
Identified candidate component class: file [GameRunner.class]
Identified candidate component class: file [MarioGame.class]
Creating shared instance of singleton bean 'gameRunner'
Creating shared instance of singleton bean 'marioGame'
Autowiring by type from bean name 'gameRunner' via constructor to bean named
'marioGame'
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
with name 'gameRunner' defined in file [GameRunner.class]
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is:org.springframework.beans.factory.NoUniqueBeanDefinitionException
No qualifying bean of type 'com.in28minutes.learnspringframework.game.GamingConsole' available
expected single matching bean but found 3: marioGame,pacManGame,superContraGame
30