Sie sind auf Seite 1von 57

Java

MOHIT PATEL
Java Version History - 1
Java Version History - 2
Java Editions
Java Runtime Architecture
What is Java?
• Object Oriented Programming Language
• Program around objects rather than actions

• Platform Independent
• Write Once, Run Anywhere

• Secured
• Java Programs run inside a virtual machine sandbox

• Dynamic
• It supports dynamic loading of classes. It means classes are loaded on demand. It also supports
functions from its native languages, i.e., C and C++
What is Java?
• Multi-threaded
• Distributed
• Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used
for creating distributed applications. This feature of Java makes us able to access files by calling the methods
from any machine on the internet.
• High-performance
• Portable
• Java is portable because it facilitates you to carry the Java bytecode to any platform
• Architecture-neutral
• the size of primitive types is fixed
• Robust (Strong)
• Garbage Collector, Exception Handling, Memory Management and type checking mechanism
4 Fundamental things to keep in mind
while designing class
• Abstraction (abstract)
• Include what is important (common among the objects) and relevant
• Exclude what is not important and irrelevant

• Encapsulation
• Wrap Instance Variables & methods in a bundle
• Hide everything (Data Hiding) unless what is absolutely necessary

• Inheritance
• subclass inherits from superclass
• Overriding & Overloading of methods can be done

• Polymorphism
• Many forms (Just like if we use + sign, it knows whether to add or concatenate)
Encapsulation
Encapsulation
• Encapsulation is the process of wrapping data and methods into a single unit (class)
• A way to make programs more secure
• Prevents unauthorized members from accessing certain variables and methods
• Encapsulating in Java can be known as data hiding
• You cannot directly access the private properties of a class unless you are writing code inside
the class itself
• It helps developers better group and organize data
• Developers can easily change code without affecting other parts of the program
Access Modifiers
Access Modifiers
Private Default Protected Public
Class NA In same package NA Everywhere
Nested Class
Constructor Public constructor In same package In same package + Everywhere
+ Static method of subclass in
same class different package
Method Only in the same In same package In same package + Everywhere
class subclass in
different package
Field Only in the same In same package In same package + Everywhere
class subclass in
different package
Public Private Protected Default Static Abstract final
Class Yes No No Yes No Yes Yes
Instance Yes Yes Yes Yes Yes No Yes
Variable
Methods Yes Yes Yes Yes Yes Yes Yes
Constructor Yes Yes Yes Yes No No No
Enum
Inner Class
Static Block
Interface
Inheritance
• Inheritance is when one class acquires the methods and fields of another
• The class which inherits the properties of the other is known as subclass (or derived class,
parent class)
• The class which is inherited is called superclass
• Every object in Java inherits from the Object class implicitly
• Uses of a class Extend to inherit the properties of another class
• Keyword extends
• Advantage: Minimize duplicate of code
• Disadvantage: Superclass and subclass can be tightly coupled
Inheritance in Java
Inheritance Hierarchy
Inheritance
• A subclass inherits all the members (fields, methods, and nested classes) from its superclass
• Constructors are not members, so they are not inherited by subclasses, but the constructor of
the superclass can be invoked from the subclass using the keyword super
• A subclass does not inherit the private members of its parent class
Rules for Overriding
• The access modifier for an overriding method can allow more, but not less, access than the
overridden method. For example, a protected instance method in the super-class can be made public,
but not private, in the subclass. Doing so, will generate compile-time error
• Final methods cannot be overridden
• Static methods can not be overridden (Method Overriding vs Method Hiding)
• Private methods can not be overridden
• The overriding method must have same return type (or subtype)
• Invoking overridden method from sub-class (use super)
• The presence of synchronized/strictfp modifier with method have no effect on the rules of overriding
• Abstract methods in an interface or abstract class are meant to be overridden in derived concrete
classes otherwise compile-time error will be thrown.
Rules for Overriding
• If the super-class overridden method does not throws an exception, subclass overriding method
can only throws the unchecked exception, throwing checked exception will lead to compile-time
error.
• If the super-class overridden method does throws an exception, subclass overriding method can
only throw same, subclass exception. Also there is no issue if subclass overridden method is not
throwing any exception.
Polymorphism
Polymorphism
• Superclass <object reference> = new Subclass();
• Constructor of the subclass will be called
• It can refer to methods which is defined in superclass and overridden in subclass. The
overridden method in the subclass will be called
• In case of static method, superclass method will be called
• Interface can be used instead of superclass
Memory Management
Garbage Collector
Java Coding Conventions
Abstract Class & method in Java
• If any method is abstract then the class has to
be abstract too
• Any abstract method has to be defined in first
concrete class in inheritance tree
Comparing Values
Logical Operators
JDK
• Java Development Kit
• It is software development kit for java
• Contains Java Runtime Environment (JRE) which in turn contains Java Virtual Machine (JVM)
• Includes compilation and packaging tools
• java : runtime
• javac: compiler
• javadoc: docs builder
• jar: archive builder
What is an IDE?
• Integrated Development Environment
• Software to write code
• Contains code editor and debugger
• Example: IntelliJ IDEA, Eclipse
What is JShell?
• Introduced newly in Java 9
• Lives in the terminal or similar shell environment
• Immediate results from a line of command
• Great for testing code
How we know if it is an object
• Check if it is noun.
• e.g. bank account, cat, dog etc.

• Add “the” before the name and check if it suits


• e.g. the bank account, the event, the cat, the dog etc.
Things to know
• Objects are immutable
• Instead of modifying or changing an object, you create a new object which looks like the old
object, except the change
• Strings are immutable
• Array is a container that stores a sequence of values of the same type
• Parameters are variables in a method definition
• Argument is the actual value that is passed to the function
• Java uses call by value method
• Unified Modelling Language (UML) helps developers visualize the flow of a program
Java Libraries
• Java.lang
• Fundamental to the core Java language (e.g. math, Boolean, byte)

• Java.util
• Generic utilities (scanning, formatting strings, data manipulations)

• Java.net
• Infrastructure for networking
Collection Framework
Interfaces
• Interfaces are a way to enforce certain fields or methods on a class
• Interfaces do not enforce exactly how these methods should be implemented – just that you
must have them
• Interfaces are definition of a behavior
• When used, they force classes and objects to have certain properties without forcing their
implementation
Functional Programming
• Functional programming focuses on computing results from functions rather than performing
actions on objects
• Lambda functions are anonymous functions that you can create in Java without the usual code
overhead
• A great tool if you need a quick function for a calculation in your code
• They usually have a single purpose and do not affect other objects in the code
Java Essential Training
Code Clinic series to see code in action
Check polymorphism
Key Terminologies
• Abstraction

• Polymorphism

• Inheritance

• Encapsulation

• Composition

• Association

• Aggregation

• Constructors

• Destructors

• Cardinality

• Singleton

• Class-Responsibility-Collaboration

• Chain-of-Responsibility
Covering Topics
• JVM architecture
• Java versions in Java history
• Garbage Collector (4 types)
• Nested classes and its access modifiers + inheritance
• Use of enum
• Cover all access modifiers
Components of Java
• Java Comments
• Object  State, Behavior
• Class  Instance Variables, Methods
• Primitive Data Types
• Arrays
• Operators
• Control Flow
• Interface
• Package
A car represented in Code
 Attributes (Instance Variables): Describing the car class
• License plate number
• Average miles per gallon
• Paint color

 Behavior (Methods): Interacting with the car class


• Check the tail lights
• Change the paint job

This is not a actual car but it describes the blueprint of a car.


Actual car is created by creating an instance of a class
Using this
Access Modifier
Inner Class
• An inner class is associated with an instance of its enclosing class and has direct access to that
object's methods and fields.
• It cannot define any static members itself
Interface

All constant values defined in an interface are implicitly public, static, and final

All abstract, default, and static methods in an interface are implicitly public

Your class can implement more than one interface, so the implements keyword is followed by a comma-
separated list of the interfaces implemented by the class
Interface

We can use interface name as method return type


Packages
• If you put multiple types in a single source file, only one can be public, and it must be same
name as the source file

Special Characters not allowed


Reserved keywords not allowed
Should not start with numbers
Packages

Jawa.awt and jawa.awt.color are two different


packages.
Java.awt.color is not inside Java.awt package

Imports Inner Class Also Imports all static members of the class
Imports Inner Class Only
Packages

Class Path. JVM will know that all classes are in these path
Number - java.lang.Number
• Subclasses: Byte, Short, Integer, Long, Float, Double, BigDecimal, BigInteger, AtomicInteger,
AtomicLong
• Uses:
• Converts the value to other primitive types
• byteValue()
• intValue()
• Compares this object to the argument
• compareTo(Byte anotherByte)
• compareTo(Integer anotherInteger)
• Determines whether the number object is equal to the argument
• boolean equals(Object obj)
• For converting to and from strings
• For converting between number systems
• Autoboxing & AutoUnboxing
• MIN_VALUE & MAX_VALUE
Number Formatter
• out object in System class is of type PrintStream
• Formatting String can be found in java.util.Formatter
• format and printf can both be used
Decimal Formatter –
java.text.DecimalFormat
• DecimalFormat inherits from NumberFormat
Math - java.lang.Math
• Math.E
• Math.PI
• abs(), ceil(), floor(), rint(), round(), min(), max(), exp(), log(), pow(), sqrt()
• java.util.Random is used if we want to create a series of random numbers
String
String
• CharSequence is an interface implemented by String class

• Methods of String Class:


• substring()
• split()
• subSequence()
• trim()
• toLowerCase()
• toUpperCase()
• indexOf()
• lastIndexOf()
• contains()
• replace()
• replaceAll()
• replaceFirst()
StringBuilder
CharSequence is an interface implemented by StringBuilder class

StringBuilder()  Empty string with a capacity of 16


StringBuilder(String)  String, plus extra capacity of 16
setLength(int)  Trims/adds null to the remaining part
append()
delete()
deleteCharAt()
insert()
replace()
setCharAt()
reverse()
toString()

Das könnte Ihnen auch gefallen