SlideShare uma empresa Scribd logo
1 de 85
Baixar para ler offline
Introduction to
Presentation by
Hung Nguyen Huy
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Java History
James Gosling, the creator of
Java
Oracle to buy Sun (2009)
Oak Java Coffee (Indonesia)
Java Version History
Java version history timeline with
their code names and feature set.
Java Features
Most important features of Java
language.
Simple
Object
Oriented
Portable
Secured
Distributed
Multi
-threaded
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Classes & Objects
● Object-Oriented Programming is a methodology or
paradigm to design a program using classes and objects.
● Object - any entity that has state and behavior is known as
an object.
● Class - a class is the blueprint from which objects are created.
Classes & Objects
When you design a class, think about the objects that will be created from
that class type.
Think about:
● Things the object knows (instance variables)
● Things the object does (methods)
Class Definition
In general, class declarations can include these
components, in order:
1. Access Modifiers such as public, private,....
2. The Class Name.
3. The name of the class's parent (superclass), if any,
preceded by the keyword extends.
4. A comma-separated list of interfaces implemented
by the class, if any, preceded by the keyword
implements.
5. The class body, surrounded by braces, {}.
Access Modifier Class Name
Instance Variables
Methods
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Access Modifiers
Access modifiers define the scope of variables, methods, constructors or
classes,...
● public: Visible to the world.
● protected: Visible to the package & all subclasses.
● default (no modifiers): Visible to the package
● private: Visible to the class only
Class Members: Instance Variables
● Instance Variables = Class Attributes = Class Variables = Data Members =
Properties
● Instance variables are defined inside the class, but outside of any method,
and are only initialized when the class is instantiated.
● Instance variables are the fields that belong to each unique object.
Class Members: Instance Variables
● You must declare all variables before they can be used.
● Following is the basic form of a variable declaration:
modifiers data-type variable-name [ = value][, variable [ = value] ...] ;
Class Members: Instance Variables
● It's not always necessary to assign a
value when a instance variable is
declared.
● Instance variables that are declared but
not initialized will be set to a reasonable
default by the compiler.
● Generally speaking, this default will be
zero or null, depending on the data
type.
Class Members: Methods
A Java method is a collection of statements that are grouped together to
perform an operation.
Class Members: Methods
Syntax:
modifiers return-type method-name([parameters]) [throws exception-list] {
// Method Body
}
● Modifiers: optional - defines the access type of the method
● Return type: required - a data type or void
● Method name: required - used to call method from somewhere
● () - required, but parameter list is optional
● Exception list - Optional
● { Body } - required for non-abstract method.
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Package
● A package is a namespace that organizes a set of related classes,
interfaces, enums or annotations.
● Conceptually you can think of packages as being similar to different
folders on your computer.
● Packages are used in Java in order to:
○ Provide namespace management (prevent naming conflicts)
○ Provide access protection.
○ Make searching/locating and usage of classes easier.
“import” keyword
● If a class wants to use another class in the same package, the package
name need not be used.
● If a class wants to use another class in another package, it must use one
of 3 techniques:
○ Use fully qualified name of the class it want to use.
○ The package can be imported using “import” keyword and the wild card (*).
○ The class itself can be imported using “import” keyword.
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Constructors
● Constructor is a special type of method that is used to initialize the object.
● Every class, including abstract classes, MUST have a constructor.
● Every time you make a new object, at least one constructor is invoked.
● Use “new” keyword to initialize an object (automatic call a constructor).
Constructors
Declaration rules:
● Constructors must have the same name as the class in which they are
declared.
● Constructors must NOT have a return type.
● Constructors can’t be marked “static”, “abstract” or “final”.
● Constructors could have parameters or not.
Constructors
Declaration rules (cont.):
● If you don't type a constructor into your class code, a default
constructor will be automatically generated by the compiler.
● The default constructor is ALWAYS a no-arg constructor.
● If you declare any constructor(s) into your class, the compiler won’t
provide the default constructor for you.
Constructors Chaining
Horse h = new Horse();
(assume Horse extends Animal and Animal extends Object.)
Question: Which constructor(s) are invoked, in which order?
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Abstract Methods
An abstract method is a method that is declared without an implementation.
access-modifiers abstract return-type method-name([parameters]);
Interfaces
● An interface is a reference type, similar to a class, that can contain only
constants and method signatures (Abstract Methods).
access-modifiers interface interface-name([parameters]) [throws exception-list] {
// Interface body: Constants & Abstract Methods
}
● A class implementing an interface must provide implementation for all of
its method unless it’s an abstract class.
Interfaces: Attributes
[public static final] data-type ATTRIBUTE_NAME = value;
Interfaces: Methods (Abstract)
[public abstract] return-type method-name([parameters]);
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Abstract Class
● An abstract class is a class that is declared abstract—it may or may not
include abstract methods.
● Abstract classes cannot be instantiated, but they can be subclassed.
● If a class includes abstract methods, then the class itself must be declared
abstract.
● If a class is declared abstract, it can NOT be instantiated.
Abstract Class vs Interface
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Encapsulation
Encapsulation is about how to hide data.
Encapsulation
How do you do that?
● Keep instance variables protected (with an access modifier, often private).
● Make public accessor methods, and force calling code to use those methods
(rather than directly accessing the instance variable).
● For the methods, use the JavaBeans naming convention of
set<someProperty> and get<someProperty>.
Encapsulation
Encapsulation
Encapsulation increases the Security
Inheritance
Inheritance is about how to reuse class resources (variables & methods).
Inheritance
● Use 2 keywords: extends and
implements.
● Inherited resources depends on the
access modifiers.
● All Java classes inherit the Object class.
● A class extends an abstract class or
implements an interface, must
override all the abstract methods.
Inheritance
Inheritance increases the Reusability
Polymorphism
Polymorphism is the ability of an object to take on many forms.
Polymorphism
● Any Java object that can pass more than one IS-A test is considered to be
polymorphic.
● The only possible way to access an object is through a reference
variable.
Polymorphism
● A reference variable can be of only one type.
● A reference is a variable, so it can be reassigned to other objects.
● The type of the reference variable would determine the methods that it
can invoke on the object.
● A reference variable can refer to any object of its declared type or any
subtype of its declared type.
● A reference variable can be declared as a class or interface type.
Polymorphism
Polymorphism increases the Flexibility.
Abstraction
Abstraction is about hiding the implementation details from outside code
Abstraction
● Achieved by using abstract classes or interfaces.
● All subclass must implement all abstract methods from abstract classes or
interfaces.
● Abstraction hides the details of objects, details of implementation, details
of how to operate.
● Users just create about the APIs. (the input and output)
Abstraction
Abstraction increases the Extensibility and Maintainability.
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Overriding
Method overriding is a mechanism that allows a subclasses
to provide a specific implementation of a method
that is already provided by its super classes.
Overriding Rules
● The overriding method must have same name as in the super class
● The overriding method must have same parameter list as in the super
class
● It must be IS-A relationship (inheritance)
● The access modifier of the overriding method must be the same or
larger scope than the overridden method in the super class
● private, static, or final methods can NOT be overridden.
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Overloading
● Method overloading is a powerful mechanism that allows us to define
cohesive class APIs.
● Method overloading can be implemented in 2 different ways:
○ Implement two or more methods that have the same name but take different
numbers of arguments
○ Implement two or more methods that have the same name but take arguments of
different types
● It’s NOT possible to have two method implementations that differ only in
their return types.
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Non-Access Modifiers
● abstract
● final
● static
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
Passing variables
Two main techniques for passing data in a programming language:
● (1) passing by value
● (2) passing by reference
Passing by Value
Passing by Value constitutes copying of data, where changes to the copied
value are not reflected in the original value.
Passing by Value (C++ example)
Passing by Reference
Passing by Reference constitutes the aliasing of data, where changes to the
aliased value are reflected in the original value
Passing by Reference (C++ example)
Passing Variables in Java
the Java Language Specification declares that the passing of all data (both
object and primitive), is defined by the following rule:
All data is passed BY VALUE
Passing Primitive Variables
In the case of primitive values, the value is simply the actual data associated
with the primitive (.e.g 1, 20.7, true, etc.) and the value of the data is copied
each time it is passed.
Passing Object Reference Variables
● The value associated with an object is actually a pointer, called a reference, to the
object in memory.
● If you're passing an object reference variable, you're passing a copy of the bits
representing the reference to an object.
● Two identical reference variables refer to the exact same object, if the called
method modifies the object, the caller will see that the object the caller's original
variable refers to has also been changed.
● The called method can NOT reassign the caller's original reference variable and
make it refer to a different object or null.
Passing Object Reference Variables
Passing Object Reference Variables
Java manipulates objects “by reference”,
but it passes object references to methods “by value”.
-- O'Reilly's Java in a Nutshell by David Flanagan
Contents
1. Introduction to Java
2. Classes & Objects
3. Class Members
4. Packages
5. Constructors
6. Interfaces
7. Abstract Classes
8. OOP Characteristics
9. Overriding
10. Overloading
11. Non-Access Modifiers
12. Passing Variables
13. String
14. Summary
String
● String is a sequence of characters placed in the double quote (“ ”).
● The Java platform provides the “String class” to create and manipulates
strings.
Strings are Immutable Objects
● Strings are Objects
○ String could be considered a primitive type in Java, but in fact they are not.
○ String objects are actually made up of an array of char primitives.
● Strings are Immutable
○ Once a String object is created, it can NOT be modified.
○ For mutable string, we can use StringBuffer and StringBuilder classes.
(An object whose state can NOT be changed after it is created is known as an Immutable object. String,
Integer, Float, Double,... and all other wrapper classes’ objects are immutable)
How to create String objects
Two ways to create String objects:
1. By string literal
2. By the new keyword
How to create String objects
1. By string literal
For example:
● String s1 = “welcome”; // creates one String object and one reference variable
● String s2 = “welcome”; // no new object will be created
2. By the new keyword
For example:
● String s3 = new String(“welcome”); // creates two objects, and one reference variable
String Pool
● String Pool is a special area of memory inside the Java Heap Memory,
used to store String literals.
● Each time the JVM encounters a String literal, it checks the pool first to see
if an identical String are already exists.
● If the String already exists in the pool, a reference to the pooled instance
returns.
● If the String doesn’t exist in the pool, a new String object instantiates, then
is placed in the pool.
String Constructors
● No Argument Constructors:
○ No-argument constructor creates an empty String. Rarely used.
○ Example: String empty = new String();
● Copy Constructors:
○ Copy constructor creates a copy of an existing String . Also rarely used.
○ Example:
String word = new String("Java");
String word2 = new String(word);
● Other Constructors:
○ Most other constructors take an array as a parameter to create a String.
○ Example:
char[] letters = {'J', 'a', 'v', 'a'};
String word = new String(letters); //"Java”
Important operations of Strings
● String Concatenation
● String Comparison
● Substring
● Length of String
String Concatenation
Two methods to concatenate 2 or more Strings
● Using concat() method
● Using + operator
String Comparison
String comparison can be done in 3 ways
● Using equals() method
● Using the == operator
● Using compareTo() method
Substring
● A part of string is called substring. In other words, substring is a subset of
another string.
● We can get substring from the given string object by one of the two
methods:
○ public String substring(int startIndex)
○ public String substring(int startIndex, int endIndex)
Length of Strings
The String's length() method finds the length of the string. It returns count of
total number of characters.
Other String methods
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#method_summary
Advantages of Immutability
Use less memory
Disadvantages of Immutability
● Less efficient - you need to create a new string and throw away the old
one even for small changes.
● Example:
String word = “Java";
word = word + “technologies”;
StringBuffer and StringBuilder
● Limitation of String?
○ String is slow and consumes a lot of memory when you concatenate too many strings
because every time it creates new instance.
● What is mutable string?
○ A string that can be modified or changed is known as mutable string.
○ StringBuffer and StringBuilder classes are used for creating mutable string.
● Differences between String and StringBuffer/StringBuilder?
○ String is immutable while StringBuffer/StringBuilder is mutable.
○ String is slower and consumes more memory while StringBuffer/StringBuilder is faster
and consumes less memory.
StringBuffer
● StringBuffer is a synchronized and allows us to mutate the string.
● StringBuffer has many utility methods to manipulate the string.
● This is more useful when using in a multi-threaded environment.
● Always modified in same memory location.
StringBuilder
● StringBuilder is the same as the StringBuffer class.
● However, the StringBuilder class is NOT synchronized and hence in a
single-threaded environment.
● The overhead is less than using a StringBuffer.
SUMMARY
Thank You!

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Core java
Core javaCore java
Core java
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
java tutorial for beginner - Free Download
java tutorial for beginner - Free Downloadjava tutorial for beginner - Free Download
java tutorial for beginner - Free Download
 
Core Java
Core JavaCore Java
Core Java
 
Core java lessons
Core java lessonsCore java lessons
Core java lessons
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Core Java
Core JavaCore Java
Core Java
 
Elements of Java Language
Elements of Java Language Elements of Java Language
Elements of Java Language
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Core Java Certification
Core Java CertificationCore Java Certification
Core Java Certification
 
Top 10 Java Interview Questions and Answers 2014
Top 10 Java Interview Questions and Answers 2014 Top 10 Java Interview Questions and Answers 2014
Top 10 Java Interview Questions and Answers 2014
 
Core java
Core java Core java
Core java
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Hibernate introduction
Hibernate introductionHibernate introduction
Hibernate introduction
 
Java Classes methods and inheritance
Java Classes methods and inheritanceJava Classes methods and inheritance
Java Classes methods and inheritance
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
Java features
Java featuresJava features
Java features
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
 

Semelhante a Core Java Introduction | Basics

INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experienced
yearninginjava
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experienced
Gaurav Maheshwari
 

Semelhante a Core Java Introduction | Basics (20)

Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
 
Suga java training_with_footer
Suga java training_with_footerSuga java training_with_footer
Suga java training_with_footer
 
core_java.ppt
core_java.pptcore_java.ppt
core_java.ppt
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Java notes
Java notesJava notes
Java notes
 
JAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdfJAVA PPT -3 BY ADI.pdf
JAVA PPT -3 BY ADI.pdf
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
 
Core java questions
Core java questionsCore java questions
Core java questions
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experienced
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
Top 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experiencedTop 371 java fa qs useful for freshers and experienced
Top 371 java fa qs useful for freshers and experienced
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
 

Último

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Último (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

Core Java Introduction | Basics

  • 2. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 3. Java History James Gosling, the creator of Java Oracle to buy Sun (2009) Oak Java Coffee (Indonesia)
  • 4. Java Version History Java version history timeline with their code names and feature set.
  • 5. Java Features Most important features of Java language. Simple Object Oriented Portable Secured Distributed Multi -threaded
  • 6. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 7. Classes & Objects ● Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. ● Object - any entity that has state and behavior is known as an object. ● Class - a class is the blueprint from which objects are created.
  • 8. Classes & Objects When you design a class, think about the objects that will be created from that class type. Think about: ● Things the object knows (instance variables) ● Things the object does (methods)
  • 9. Class Definition In general, class declarations can include these components, in order: 1. Access Modifiers such as public, private,.... 2. The Class Name. 3. The name of the class's parent (superclass), if any, preceded by the keyword extends. 4. A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. 5. The class body, surrounded by braces, {}. Access Modifier Class Name Instance Variables Methods
  • 10. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 11. Access Modifiers Access modifiers define the scope of variables, methods, constructors or classes,... ● public: Visible to the world. ● protected: Visible to the package & all subclasses. ● default (no modifiers): Visible to the package ● private: Visible to the class only
  • 12. Class Members: Instance Variables ● Instance Variables = Class Attributes = Class Variables = Data Members = Properties ● Instance variables are defined inside the class, but outside of any method, and are only initialized when the class is instantiated. ● Instance variables are the fields that belong to each unique object.
  • 13. Class Members: Instance Variables ● You must declare all variables before they can be used. ● Following is the basic form of a variable declaration: modifiers data-type variable-name [ = value][, variable [ = value] ...] ;
  • 14. Class Members: Instance Variables ● It's not always necessary to assign a value when a instance variable is declared. ● Instance variables that are declared but not initialized will be set to a reasonable default by the compiler. ● Generally speaking, this default will be zero or null, depending on the data type.
  • 15. Class Members: Methods A Java method is a collection of statements that are grouped together to perform an operation.
  • 16. Class Members: Methods Syntax: modifiers return-type method-name([parameters]) [throws exception-list] { // Method Body } ● Modifiers: optional - defines the access type of the method ● Return type: required - a data type or void ● Method name: required - used to call method from somewhere ● () - required, but parameter list is optional ● Exception list - Optional ● { Body } - required for non-abstract method.
  • 17. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 18. Package ● A package is a namespace that organizes a set of related classes, interfaces, enums or annotations. ● Conceptually you can think of packages as being similar to different folders on your computer. ● Packages are used in Java in order to: ○ Provide namespace management (prevent naming conflicts) ○ Provide access protection. ○ Make searching/locating and usage of classes easier.
  • 19. “import” keyword ● If a class wants to use another class in the same package, the package name need not be used. ● If a class wants to use another class in another package, it must use one of 3 techniques: ○ Use fully qualified name of the class it want to use. ○ The package can be imported using “import” keyword and the wild card (*). ○ The class itself can be imported using “import” keyword.
  • 20. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 21. Constructors ● Constructor is a special type of method that is used to initialize the object. ● Every class, including abstract classes, MUST have a constructor. ● Every time you make a new object, at least one constructor is invoked. ● Use “new” keyword to initialize an object (automatic call a constructor).
  • 22. Constructors Declaration rules: ● Constructors must have the same name as the class in which they are declared. ● Constructors must NOT have a return type. ● Constructors can’t be marked “static”, “abstract” or “final”. ● Constructors could have parameters or not.
  • 23. Constructors Declaration rules (cont.): ● If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler. ● The default constructor is ALWAYS a no-arg constructor. ● If you declare any constructor(s) into your class, the compiler won’t provide the default constructor for you.
  • 24. Constructors Chaining Horse h = new Horse(); (assume Horse extends Animal and Animal extends Object.) Question: Which constructor(s) are invoked, in which order?
  • 25. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 26. Abstract Methods An abstract method is a method that is declared without an implementation. access-modifiers abstract return-type method-name([parameters]);
  • 27. Interfaces ● An interface is a reference type, similar to a class, that can contain only constants and method signatures (Abstract Methods). access-modifiers interface interface-name([parameters]) [throws exception-list] { // Interface body: Constants & Abstract Methods } ● A class implementing an interface must provide implementation for all of its method unless it’s an abstract class.
  • 28. Interfaces: Attributes [public static final] data-type ATTRIBUTE_NAME = value;
  • 29. Interfaces: Methods (Abstract) [public abstract] return-type method-name([parameters]);
  • 30. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 31. Abstract Class ● An abstract class is a class that is declared abstract—it may or may not include abstract methods. ● Abstract classes cannot be instantiated, but they can be subclassed. ● If a class includes abstract methods, then the class itself must be declared abstract. ● If a class is declared abstract, it can NOT be instantiated.
  • 32. Abstract Class vs Interface
  • 33. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 35. Encapsulation How do you do that? ● Keep instance variables protected (with an access modifier, often private). ● Make public accessor methods, and force calling code to use those methods (rather than directly accessing the instance variable). ● For the methods, use the JavaBeans naming convention of set<someProperty> and get<someProperty>.
  • 38. Inheritance Inheritance is about how to reuse class resources (variables & methods).
  • 39. Inheritance ● Use 2 keywords: extends and implements. ● Inherited resources depends on the access modifiers. ● All Java classes inherit the Object class. ● A class extends an abstract class or implements an interface, must override all the abstract methods.
  • 41. Polymorphism Polymorphism is the ability of an object to take on many forms.
  • 42. Polymorphism ● Any Java object that can pass more than one IS-A test is considered to be polymorphic. ● The only possible way to access an object is through a reference variable.
  • 43. Polymorphism ● A reference variable can be of only one type. ● A reference is a variable, so it can be reassigned to other objects. ● The type of the reference variable would determine the methods that it can invoke on the object. ● A reference variable can refer to any object of its declared type or any subtype of its declared type. ● A reference variable can be declared as a class or interface type.
  • 45. Abstraction Abstraction is about hiding the implementation details from outside code
  • 46. Abstraction ● Achieved by using abstract classes or interfaces. ● All subclass must implement all abstract methods from abstract classes or interfaces. ● Abstraction hides the details of objects, details of implementation, details of how to operate. ● Users just create about the APIs. (the input and output)
  • 47. Abstraction Abstraction increases the Extensibility and Maintainability.
  • 48. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 49. Overriding Method overriding is a mechanism that allows a subclasses to provide a specific implementation of a method that is already provided by its super classes.
  • 50. Overriding Rules ● The overriding method must have same name as in the super class ● The overriding method must have same parameter list as in the super class ● It must be IS-A relationship (inheritance) ● The access modifier of the overriding method must be the same or larger scope than the overridden method in the super class ● private, static, or final methods can NOT be overridden.
  • 51. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 52. Overloading ● Method overloading is a powerful mechanism that allows us to define cohesive class APIs. ● Method overloading can be implemented in 2 different ways: ○ Implement two or more methods that have the same name but take different numbers of arguments ○ Implement two or more methods that have the same name but take arguments of different types ● It’s NOT possible to have two method implementations that differ only in their return types.
  • 53. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 55. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 56. Passing variables Two main techniques for passing data in a programming language: ● (1) passing by value ● (2) passing by reference
  • 57. Passing by Value Passing by Value constitutes copying of data, where changes to the copied value are not reflected in the original value.
  • 58. Passing by Value (C++ example)
  • 59. Passing by Reference Passing by Reference constitutes the aliasing of data, where changes to the aliased value are reflected in the original value
  • 60. Passing by Reference (C++ example)
  • 61. Passing Variables in Java the Java Language Specification declares that the passing of all data (both object and primitive), is defined by the following rule: All data is passed BY VALUE
  • 62. Passing Primitive Variables In the case of primitive values, the value is simply the actual data associated with the primitive (.e.g 1, 20.7, true, etc.) and the value of the data is copied each time it is passed.
  • 63. Passing Object Reference Variables ● The value associated with an object is actually a pointer, called a reference, to the object in memory. ● If you're passing an object reference variable, you're passing a copy of the bits representing the reference to an object. ● Two identical reference variables refer to the exact same object, if the called method modifies the object, the caller will see that the object the caller's original variable refers to has also been changed. ● The called method can NOT reassign the caller's original reference variable and make it refer to a different object or null.
  • 65. Passing Object Reference Variables Java manipulates objects “by reference”, but it passes object references to methods “by value”. -- O'Reilly's Java in a Nutshell by David Flanagan
  • 66. Contents 1. Introduction to Java 2. Classes & Objects 3. Class Members 4. Packages 5. Constructors 6. Interfaces 7. Abstract Classes 8. OOP Characteristics 9. Overriding 10. Overloading 11. Non-Access Modifiers 12. Passing Variables 13. String 14. Summary
  • 67. String ● String is a sequence of characters placed in the double quote (“ ”). ● The Java platform provides the “String class” to create and manipulates strings.
  • 68. Strings are Immutable Objects ● Strings are Objects ○ String could be considered a primitive type in Java, but in fact they are not. ○ String objects are actually made up of an array of char primitives. ● Strings are Immutable ○ Once a String object is created, it can NOT be modified. ○ For mutable string, we can use StringBuffer and StringBuilder classes. (An object whose state can NOT be changed after it is created is known as an Immutable object. String, Integer, Float, Double,... and all other wrapper classes’ objects are immutable)
  • 69. How to create String objects Two ways to create String objects: 1. By string literal 2. By the new keyword
  • 70. How to create String objects 1. By string literal For example: ● String s1 = “welcome”; // creates one String object and one reference variable ● String s2 = “welcome”; // no new object will be created 2. By the new keyword For example: ● String s3 = new String(“welcome”); // creates two objects, and one reference variable
  • 71. String Pool ● String Pool is a special area of memory inside the Java Heap Memory, used to store String literals. ● Each time the JVM encounters a String literal, it checks the pool first to see if an identical String are already exists. ● If the String already exists in the pool, a reference to the pooled instance returns. ● If the String doesn’t exist in the pool, a new String object instantiates, then is placed in the pool.
  • 72. String Constructors ● No Argument Constructors: ○ No-argument constructor creates an empty String. Rarely used. ○ Example: String empty = new String(); ● Copy Constructors: ○ Copy constructor creates a copy of an existing String . Also rarely used. ○ Example: String word = new String("Java"); String word2 = new String(word); ● Other Constructors: ○ Most other constructors take an array as a parameter to create a String. ○ Example: char[] letters = {'J', 'a', 'v', 'a'}; String word = new String(letters); //"Java”
  • 73. Important operations of Strings ● String Concatenation ● String Comparison ● Substring ● Length of String
  • 74. String Concatenation Two methods to concatenate 2 or more Strings ● Using concat() method ● Using + operator
  • 75. String Comparison String comparison can be done in 3 ways ● Using equals() method ● Using the == operator ● Using compareTo() method
  • 76. Substring ● A part of string is called substring. In other words, substring is a subset of another string. ● We can get substring from the given string object by one of the two methods: ○ public String substring(int startIndex) ○ public String substring(int startIndex, int endIndex)
  • 77. Length of Strings The String's length() method finds the length of the string. It returns count of total number of characters.
  • 80. Disadvantages of Immutability ● Less efficient - you need to create a new string and throw away the old one even for small changes. ● Example: String word = “Java"; word = word + “technologies”;
  • 81. StringBuffer and StringBuilder ● Limitation of String? ○ String is slow and consumes a lot of memory when you concatenate too many strings because every time it creates new instance. ● What is mutable string? ○ A string that can be modified or changed is known as mutable string. ○ StringBuffer and StringBuilder classes are used for creating mutable string. ● Differences between String and StringBuffer/StringBuilder? ○ String is immutable while StringBuffer/StringBuilder is mutable. ○ String is slower and consumes more memory while StringBuffer/StringBuilder is faster and consumes less memory.
  • 82. StringBuffer ● StringBuffer is a synchronized and allows us to mutate the string. ● StringBuffer has many utility methods to manipulate the string. ● This is more useful when using in a multi-threaded environment. ● Always modified in same memory location.
  • 83. StringBuilder ● StringBuilder is the same as the StringBuffer class. ● However, the StringBuilder class is NOT synchronized and hence in a single-threaded environment. ● The overhead is less than using a StringBuffer.