Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

JavaProgrammingForBeginners-Presentation-16-30

The document provides an overview of Object Oriented Programming (OOP) concepts using a 'Planet' class example, illustrating the distinction between classes and objects, as well as their states and behaviors. It also discusses Java's evolution, including versioning, new features, and modularization introduced in Java 9, alongside practical applications using the Spring Framework. Key features such as local variable type inference, switch expressions, text blocks, and records are highlighted, emphasizing improvements in Java's syntax and structure.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JavaProgrammingForBeginners-Presentation-16-30

The document provides an overview of Object Oriented Programming (OOP) concepts using a 'Planet' class example, illustrating the distinction between classes and objects, as well as their states and behaviors. It also discusses Java's evolution, including versioning, new features, and modularization introduced in Java 9, alongside practical applications using the Spring Framework. Key features such as local variable type inference, switch expressions, text blocks, and records are highlighted, emphasizing improvements in Java's syntax and structure.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Object Oriented Programming (OOP)

class Planet
name, location, distanceFromSun // data / state / fields
rotate(), revolve() // actions / behavior / methods

earth : new Planet


venus : new Planet

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

earth : new Planet


venus : new Planet

Each Planet has its own state:


name: "Earth", "Venus"
location : Each has its own orbit
distanceFromSun : They are at unique, different distances from the sun
Each Planet has its own unique behavior:
rotate() : They rotate at different rates (and in fact, different directions!)
revolve() : They revolve round the sun in different orbits, at different speeds

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

JDK 1.0 January 1996

J2SE 5.0 September 2004 5 Releases in 8 years

Java SE 8 (LTS) March 2014 Most important Java Release

Java SE 9 September 2017 4 Releases in 13 years

Java SE 10 March 2018 Time-Based Release Versioning

Java SE 11 (LTS) September 2018 Long Term Support Version (Every 3 years)

Java SE 12 March 2019

...

Java SE 16 March 2021

Java SE 17 (LTS) September 2021

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

Java SE 9 Sep 2017 Modularization (Java Platform Module System)

Java SE 10 Mar 2018 Local Variable Type Inference

Java SE 14 Mar 2020 Switch Expressions (Preview in 12 and 13)

Java SE 15 Sep 2020 Text Blocks (Preview in 13)

Java SE 16 Mar 2021 Record Classes (Preview in 14 and 15)

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);

Java compiler infers the type of the variable at compile time


Introduced in Java 10
You can add final if you want
var can also be used in loops
Remember:
You cannot assign null
var is NOT a keyword
Best Practices:
Good variable names
Minimize Scope
Improve readability for chained expressions

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";
};

Create expressions using switch statement


Released in JDK 14
Preview - JDK 12 and 13
Remember:
No fallthrough
Use yield or -> to return value

24
Text Blocks
System.out.println("\"First Line\"\nSecond Line\nThird Line");
System.out.println("""
"First Line"
Second Line
Third Line"""
);

Simplify Complex Text Strings


Released in JDK 15
Preview - JDK 13 and 14
Remember:
First Line : """ Followed by line terminator
"""abc or """abc""" in First Line are NOT valid
Automatic Alignment is done
Trailing white space is stripped
You can use text blocks where ever you can use a String

25
Records
record Person(String name, String email, String phoneNumber) { }

Eliminate verbosity in creating Java Beans


Public accessor methods, constructor, equals, hashcode and toString are automatically
created
You can create custom implementations if you would want
Released in JDK 16
Preview - JDK 14 and 15
Remember:
Compact Constructors are only allowed in Records
You can add static fields, static initializers, and static methods
BUT you CANNOT add instance variables or instance initializers
HOWEVER you CAN add instance methods

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

Iteration 3: Loose Coupling - Spring


Spring framework will manage all our objects!
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

You might also like