Core Java
Lesson 1—Introduction to Java
© Simplilearn. All rights reserved.
Learning Objectives
At the end of this lesson, you should be able to:
Discuss Java technology
Discuss object-oriented programming (OOP)
Explain the fundamentals of Java language
Discuss primitive data types and wrapper classes in Java
Set up Java and Eclipse on your machine
Write and run your first Java program
Core Java
Topic 1—Introduction to Java
What Is Java?
A programming language and computing platform for developing
application software that can run on any device
Used in a wide variety of platforms, from game consoles, laptops, and mobile phones
to datacentres, enterprise servers, and scientific supercomputers
Since Java has its own runtime environment—Java Runtime Environment (JRE)
and application programming interface (API), it is considered a platform.
History of Java
The search for a “Java” like language started in 1990 when there
was a need for a portable code which can run on any device.
James Gosling (Sun Microsystems) developed Java in 1995.
Java is now a subsidiary of Oracle Corporation.
Today, Java is used almost everywhere. The reasons are plenty.
Why Java?
Simple and easy to read language
Well-defined libraries created through Purely object-oriented
modifications from the existing C libraries language (OOP)
Used in top mobile OS, for example Distributed, interpreted, robust,
Android uses Java for coding and secure
Provides strong networking infrastructure Portable because you can write it
for server side computing once and run it everywhere
Platform independent
Types of Java Applications
Standalone Application
Web Application
Enterprise Application
Mobile Application
Types of Java Applications
Standalone Application
• An application that needs to be installed
on every machine, such as set-top box,
antivirus, and media player
Types of Java Applications
Web Application
• An application that runs on the server
side and creates dynamic page
Types of Java Applications
Enterprise Application
• A business application that has high level
of security, load balancing, and clustering,
such as banking application
• Developed using Enterprise Java Bean
(EJB)
Types of Java Applications
Mobile Application
• An application for mobile devices
• Currently, developed using Android and
Java Micro Edition (Java ME)
Java Editions
Java Standard Java Enterprise Java Micro
Edition (SE) Edition (EE) Edition (ME)
Java Editions
Java SE • A standard edition core Java programming platform
• Contains all Java libraries
• Allows development and deployment of Java applications
on desktops, servers, and embedded environments
• Used to build standalone applications
Desktop/servers and
embedded environment • Latest version—Java SE 8u 31
Java Editions
• An enterprise edition that lets you develop large-scale, multi-
tiered, scalable, reliable, and secure network applications
Java EE
• Allows Application Programming Interface (API) to develop
distributed applications (apps) that follows client-server model
Distributed apps are applications that can run on multiple
computers within a network.
• Portable—allows an application hosted at one place to be
accessed by users across the world over the Internet
Web and enterprise
applications
• Latest version—Java EE 7
Java Editions
Java ME
• A micro edition which lets you develop programs for
mobile application devices
• Consists of programming specifications and a special
virtual machine that allows a Java ME encoded program to
run on mobile devices
• Latest version—Java ME 8.3
Mobile applications
Differences between Editions
SE API Packages EE API Packages ME API Packages
• java.applet • javax.annotation • Device Access API
• java.awt • javax.ejb • System Configuration API
• java.lang • javax.jms • OAuth2 API
• java.rmi • javax.persistence • JSON API
• java.sql • javax.servlet.http • HTTP Client API
• java.util • javax.servlet.jsp • Security API
Latest Version—Java 8
• Java 8 is the latest version of Java, which was released in March, 2014.
• It allows developers to develop applications across a wide range of scenarios, from the
smallest embedded Internet of Things (IoT) devices to enterprise servers in the cloud.
• It includes upgrades to Java programming, Java virtual machine (JVM), tools, and libraries.
Latest Version—Java 8 (Contd.)
• The features of Java 8 are the following:
o Java 8 Date/Time API o Optional class
o Lambda Expressions o Nashorn JavaScript
o Method References o Parallel Array Sorting
o Functional Interfaces o Type Inference
o Stream API o Method Parameter Reflection
o Base64 Encode Decode o Type annotations and repeating
o Default Methods annotations
o forEach method o Java Database Connectivity (JDBC)
o Collectors improvements
o StringJoiner
Core Java
Topic 2—Object-Oriented Programming (OOP)
Object-Oriented Programming
• OOP is a programming language model, organized around “objects” rather than “actions” and
“data” rather than “logic.”
• It simplifies software development and maintenance by providing the following important
concepts:
o Class
o Object
o Inheritance
o Encapsulation
o Polymorphism
o Abstraction
Object-Oriented Programming
A class is a blueprint or prototype for creating objects, providing initial values for variable
Class
state, and implementing behavior.
access_specifier class ClassName
Object {
/*
class consists of
a)class level or global variables
Inheritance b)objects
c) static and non static methods
d) constructors
e) inner classes
*/
Encapsulation
}
Polymorphism
Abstraction
Object-Oriented Programming (Contd.)
An object is a software bundle of related state and behavior. Software objects are often
Class
used to model the real-world objects.
Object An object is an entity that has “variable” and “method.”
An object has three characteristics:
Inheritance • State: Data (value) of an object
• Behavior: The behavior (functionality) of an object, such as deposit, withdraw
Encapsulation • Identity (ID): Typically implemented via a unique ID
Polymorphism The value of the ID is not visible to the external user. But, it is used internally
by the JVM to identify each object uniquely.
Abstraction
Object-Oriented Programming (Contd.)
Inheritance provides a powerful and natural mechanism for organizing and structuring
Object
classes.
Class In Inheritance, subclasses:
• Inherit attributes and services from their superclass
• May add new attributes and services
Inheritance • Reuse the code in the superclass
• Provide specialized behaviors (overriding and dynamic binding)
Encapsulation • Partially define and implement common behaviors (abstract)
Class A Class A Class A
Polymorphism
Class B Class B Class B Class C
1. Single type 3. Hierarchical type
Abstraction Class C
2. Multilevel type
Object-Oriented Programming (Contd.)
Example of inheritance:
Object
Shape
Class draw () class Subclass-name extends Superclass-name
{
//methods and fields
}
Inheritance Line
draw ()
Encapsulation
The keyword “extends” implies that you are making a new class that descends
Polymorphism
from an existing class.
Abstraction
Object-Oriented Programming (Contd.)
Encapsulation is a mechanism for wrapping the variables and methods together as a
Object
single unit.
Class In Encapsulation, variables from a class will be hidden from other outer classes and can
only be accessed by the method of same class.
Inheritance Using Encapsulation:
• A field in a class can be made read-only or read and write only via the methods and
Encapsulation not directly
• A class can control the elements in it
Polymorphism
Abstraction
Object-Oriented Programming (Contd.)
Polymorphism is the ability of an object to take on many forms.
Object
Shape
draw ()
Class
Line Triangle Rectangle Circle
draw () draw () draw () draw ()
Inheritance
There are two types of Polymorphism:
Encapsulation • Compile time Polymorphism
• Runtime Polymorphism
Polymorphism It can be performed in two ways:
• Overloading
• Overriding
Abstraction
Object-Oriented Programming (Contd.)
Hiding internal details and showing functionality is known as abstraction. For example:
Object
Employee of a company with hidden mobile number, salary, and address.
Class In Java, we use abstract class and interface to achieve abstraction.
Inheritance Abstraction
Employee
Encapsulation
Mobile
Polymorphism number
Address
Salary
Abstraction
Core Java
Topic 3—Fundamentals of Java Language
Fundamentals of Java Language
Core Elements of Java
Language Class has two components:
• Class declaration—It contains the class name and the access modifier. The access
Classes and Objects modifier defines the accessibility of the class.
• Class body—It comes after declaration, and it contains functions and variables.
Methods
Object is an instance of a class. When a program is run, objects are created to do the
task.
Variables
Fundamentals of Java Language
Core Elements of Java A method is a subprogram that contains the logic of the application. It acts on
Language data and often returns a value.
Classes and Objects It is referred to by name and can be called by its name.
Each method contains the following:
Methods
• Declaration—includes the method name, return type, parameter, and
accessibility
Variables • Body—contains information that determines the behavior of the object
Method1
public static void main (String[] args) ________
{ ________
statement; ________
method1();
statement;
method2();
statement; Method2
________
} ________
________
__
Fundamentals of Java Language
Core Elements of Java Variable is a reserved memory that temporarily stores a data value and thus has a data
Language type. It can be either declared or inferred. It adheres to type constraints.
Classes and Objects There are four types of variables in Java:
• Instance/non-static variables—a variable that belongs to an object
• Static variables—a variable that belongs to a class
Methods
• Local variables—a variable declared inside methods, constructors, or blocks
• Parameters—a variable that is passed to a method when the method is called
Variables Example of non-static variable
public class MyClass
{
public static void main(String[] args) {
int number = 5;
number = number + 10;
}
}
Fundamentals of Java Language
Components of Java JDK contains JRE and development tools necessary to compile, document, and package
Java programs.
Java Development Kit
(JDK) Set of Libraries
Other Files
Example: rt.jar
Java Runtime
Development Tools
Environment (JRE) Java Virtual Machine (JVM)
Example: Javac and java
JRE
Java Virtual Machine
(JVM) JDK
Fundamentals of Java Language
Components of Java JRE is a runtime environment in which Java bytecode is executed. It contains all the class
libraries and other files that JVM uses at runtime.
Java Development Kit
(JDK) Set of Libraries
Other Files
Example: rt.jar
Java Runtime
Environment (JRE) Java Virtual
Machine (JVM)
Java Virtual Machine JRE
(JVM)
Fundamentals of Java Language
Components of Java JVM is an abstract machine. It is a specification that provides runtime environment in
which Java bytecode can be executed.
Java Development Kit
(JDK) Unlike other languages, Java “executables” are executed on a CPU that does not exist.
Java Runtime
Environment (JRE)
Java Virtual
Machine (JVM)
Java Virtual Machine
(JVM)
Java Architecture
Class Loader
Java Class
Java Source Byte Code
Libraries
Code (.java) Verifier
File Class Loader Class Loader
System/ Byte Code Byte Code
Java Compiler
Network Verifier Verifier
(javac) Java Virtual
Machine (JVM)
Java Byte Code is Runtime System
moved locally or
through network
Java Byte Code Operating System (OS)
(.class)
Hardware
Compile Time Run Time
Core Java
Topic 4—Primitive Data Types and Wrapper Classes in Java
Primitive Data types
Primitive data types are basic data types in Java language.
There are 8 primitive data types:
• int 4 bytes
• short 2 bytes
• long 8 bytes Behavior is same as
• byte 1 byte in C++
• float 4 bytes
• double 8 bytes
• char Unicode encoding (2 bytes)
• boolean {true, false}
Primitive type always begins with a lowercase letter.
Wrapper Classes in Java
Wrapper class in Java provides the mechanism to
convert primitive into object and object into primitive.
Data Type
Primitive Non-
The eight classes of java.lang package are known as primitive
String
wrapper classes in Java.
Boolean
Array
Numeric
• Boolean etc.
• Character
Character Integral
• Byte
• Short Integer Floating-point
• Integer
• Long
• Float
boolean char byte short int long float double
• Double
Core Java
Topic 4—How to Set Up Java and Execute Java Program
Setting Up Eclipse
• To write your Java programs, you need Integrated Development Environment (IDE), such as Eclipse.
• There are many more IDEs available in the market. For now, let us consider Eclipse IDE.
• Eclipse is a Java IDE developed by Eclipse open-source community.
Setting Up Eclipse (Contd.)
To set up Eclipse IDE in Windows machine:
1. Download and install the Eclipse IDE.
2. Configure the Eclipse Plugin to start under JDK 1.8.
3. Set up the Java environment.
4. To set up the Java environment in Eclipse:
a. Right click My Computer
b. Select Properties>Advanced system settings>Environmental Variables
c. Under User Variables, click New. A popup window will appear
d. In the popup window, define the variable name and its value
Value is the complete path to where you have saved your JDK. For example: C:\Program
Files\Java\jdk1.8.0_16\bin;.; path
Setting Up Eclipse (Contd.)
5. Test the variable by opening the command prompt window and type “javac” to launch the compiler
with the command.
The command prompt window should show the list of things supported by Java.
6. Restart your machine.
7. Start Eclipse and do the following:
a. Update the Installed JREs
b. Install the Maven Eclipse Plugin
c. Configure the Maven Plugin
d. Set up a DOS Shell in Eclipse
Setting Up Java and Executing Java Program
To set up JDK on Windows machine:
1. Download and install JDK and JRE by running the downloaded installer.
2. Include JDK’s “bin” Directory in the PATH.
3. Verify the JDK Installation.
4. Write a “Hello-World Java” Program
class Hello
{
public static void main(String[] args){
System.out.println(“Hello World !!!”);
}
}
C:\javac Hello.java ( compilation creates Hello.class )
C:\java Hello (Execution on the local JVM)
5. Compile and Run the Hello World Java Program.
Key Takeaways
Java is both a programming language and computing platform for developing
application software that can run on any device.
Java is platform independent—Write Once and Run Anywhere (WORA).
Java programming can be used to develop standalone, web, enterprise, and
mobile applications.
Java is an Object-Oriented Programming language.
Classes and objects, methods, and variables are some of the core elements of
Java.
JVM, JRE, and JDK are components of Java.
Primitive data types are basic data types in Java language.
Wrapper class in Java provides the mechanism to convert primitive into object
and object into primitive.
Quiz
QUIZ
Which of these is needed for Java developer to develop the Java app?
1
a. jdk
b. apk
c. jar
d. jre
QUIZ
Which of these is needed for Java developer to develop the Java app?
1
a. jdk
b. apk
c. jar
d. jre
The correct answer is a and d.
A programmer needs both jdk and jre. Jdk is used to develop applications in java and jre is used to
run and test applications.
QUIZ
Java is currently part of:
2
a. Sun Microsystems
b. Microsoft
c. Oracle
d. Sun Organization
QUIZ
Java is currently part of:
2
a. Sun Microsystems
b. Microsoft
c. Oracle
d. Sun Organization
The correct answer is c. Oracle
Since 2010, Java (Sun Microsystems) is under Oracle Corporation.
Thank You