1695206978-4.1 Essentials of Java Programming Language
1695206978-4.1 Essentials of Java Programming Language
Disclaimer
The content is curated from online/offline resources and used for educational purpose only
Programming and Coding Skills
Learning Objectives
• Core JAVA (J2SE) Programming and Coding Skills
Programming and Coding Skills
Scenario
Source: https://www.freepngimg.com/thumb/android/58538-development-android-software-free-hd-image.png
https://1000marken.net/wp-content/uploads/2021/01/Minecraft-Symbol.png
th (397×265) (bing.com)
Programming and Coding Skills
Contents
Source: https://www.freepik.com/premium-vector/content-marketing-landing-page_6324893.htm#
Programming and Coding Skills
Introduction
• After 27 years of existence, Java is still doing well.
• Programmers who know it are still in high demand.
• They will continue to be sought after for a long time
to come as over 90% of the Fortune 500
companies still rely on Java for their development
projects.
Source: https://dev.to/tech_sam/let-s-revisit-java-in-2019-3o8c
Programming and Coding Skills
JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java program.
Source: https://www.programiz.com/java-programming/jvm-jre-jdk
Programming and Coding Skills
• It provides Java class libraries, Java Virtual Machine (JVM), and other components that are required to run
Java applications.
Source: https://www.programiz.com/java-programming/jvm-jre-jdk
Programming and Coding Skills
• JDK (Java Development Kit) is a software development kit required to develop applications in Java.
Source: https://www.programiz.com/java-programming/jvm-jre-jdk
Programming and Coding Skills
Java Variables
• A variable is a location in memory (storage area) to
hold data.
• Boolean
• Byte
• Short
• Int
• Long
• Double
• Float
• Char
Programming and Coding Skills
Java Operators
Here are some commonly used Java operators along with examples:
• Arithmetic Operators
• Assignment Operators
• Comparison Operators
• Logical Operators
• Unary Operators
• Increment and Decrement Operators
• Conditional (Ternary) Operator
Programming and Coding Skills
Java Input: Java provides different ways to get input from the user. One of them is by using
object of Scanner class.
• Declaration Statements: In Java, declaration statements are used for declaring variables.
• Java Blocks: A block is a group of statements (zero or more) that is enclosed in curly braces { }.
A block may not have any statements.
Programming and Coding Skills
Java Comments
• Comments are a portion of the program that are completely ignored by Java compilers. They are mainly used
to help programmers to understand the code.
1. Single-line Comment: A single-line comment starts and ends in the same line. To write a single-line
comment, we can use the // symbol.
2. Multi-line Comment: When we want to write comments in multiple lines, we can use the multi-line
comment. To write multi-line comments, we can use the /*....*/ symbol.
Programming and Coding Skills
• The if statement executes a certain section of code if the How the if...else statement works?
test expression is evaluated to true.
Source: https://www.programiz.com/java-programming/if-else-statement
Programming and Coding Skills
Source: https://www.programiz.com/java-programming/switch-statement
Programming and Coding Skills
Source: https://www.programiz.com/java-programming/for-loop
Programming and Coding Skills
Source: https://www.programiz.com/java-programming/do-while-loop
Programming and Coding Skills
Source: https://www.programiz.com/java-programming/break-statement
Programming and Coding Skills
Source: https://www.programiz.com/java-programming/continue-statement
Programming and Coding Skills
Java Arrays
• An array is a collection of similar types of data.
• Array indices always start from 0. That is, the first Java Arrays initialization
element of an array is at index 0.
Source: https://www.programiz.com/java-programming/arrays
Programming and Coding Skills
Source: https://www.programiz.com/java-programming/multidimensional-array
Programming and Coding Skills
• Java Objects
Java Methods
• A method is a block of code that performs a specific
task.
1. User-defined Methods
2. Standard Library Methods
Programming and Coding Skills
Java Constructors
What is a Constructor?
Java Constructors
Types of Constructor
1. No-Arg Constructor
2. Parameterized Constructor
3. Default Constructor
Programming and Coding Skills
Java Recursion
• A method that calls itself is known as a recursive
method. And this process is known as recursion. Working of Java Recursion
Source: https://www.programiz.com/java-programming/recursion
Programming and Coding Skills
Java Strings
• In Java, a string is a sequence of characters.
this Keyword
In Java, this keyword is used to refer to the current object inside a method or a constructor.
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Source: https://emergetech.org/tag/java/
Programming and Coding Skills
Inheritance
• Inheritance lets programmers create new classes
that share some of the attributes of existing
classes.
Source: https://techvidvan.com/tutorials/java-inheritance
Programming and Coding Skills
Source: https://www.programiz.com/java-programming/inheritance
Programming and Coding Skills
Types of Inheritance
There are five types of inheritance.
• Single
• Multiple
• Multilevel
• Hierarchical
• Hybrid
Source: https://bytesofgigabytes.com/java/inheritance-in-java
Programming and Coding Skills
Java Inheritance
• Inheritance is one of the key features of OOP that allows us to create a new class from an existing
class. Java does not support multiple inheritance
1 2
3 4 5
Hierarchical Inheritance Multiple Inheritance Hybrid Inheritance
Programming and Coding Skills
Java Super
• The super keyword in Java is used in subclasses to access superclass members (attributes,
constructors and methods).
2. To access attributes (fields) of the superclass if both superclass and subclass have attributes with the
same name.
3. To explicitly call superclass no-arg (default) or parameterized constructor from the subclass
constructor.
Programming and Coding Skills
Polymorphism
• Allows programmers to use the same word in Java
to mean different things in different contexts.
Source: https://codegym.cc/groups/posts/polymorphism-in-java
Programming and Coding Skills
Source: https://www.javaguides.net/2018/08/polymorphism-in-java-with-example.html
Programming and Coding Skills
Java Polymorphism
• Polymorphism is an important concept of object-
oriented programming. Working of Java polymorphism
1. Method Overriding
2. Method Overloading
Source: https://www.programiz.com/java-programming/polymorphism
Programming and Coding Skills
Method Overloading
Source: https://techvidvan.com/tutorials/method-overloading-and-overriding
Programming and Coding Skills
Method Overriding
• The child class can override a method of its parent
class.
Source: https://techvidvan.com/tutorials/method-overloading-and-overriding
Programming and Coding Skills
Source: https://www.programiz.com/java-programming/method-overriding
Programming and Coding Skills
• Both the superclass and the subclass must have the same method name, the same return type
and the same parameter list.
Abstraction
• In Java, abstraction means simple things like
objects, classes and variables represent more
complex underlying code and data.
Source: https://www.tutorialkart.com/java/abstraction-in-java
Programming and Coding Skills
Source: https://techvidvan.com/tutorials/abstraction-in-java
Programming and Coding Skills
• An abstract class can have both the regular methods and abstract methods.
• If a class contains an abstract method, then the class should be declared abstract.
Java Interface
• An interface is a fully abstract class. It includes a group of abstract methods .
Encapsulation
• Encapsulation is a protective barrier that keeps the
data and code safe within the class itself.
Source: https://techblogstation.com/java/oops-concepts-in-java
Programming and Coding Skills
Source: https://javatutorial.net/java-encapsulation-example
Programming and Coding Skills
• In Java, access modifiers are used to set the Accessibilty of all access modifiers in java
accessibility (visibility) of classes, interfaces,
variables, methods, constructors, data members,
and the setter methods.
1. Default
2. Private
3. Protected
4. Public
Source: https://www.programiz.com/java-programming/access-modifiers
Programming and Coding Skills
Java Exceptions
• An exception is an unexpected event that occurs Java Exception Hierarchy
during program execution. Image source:
Errors
Exceptions
Source: https://www.programiz.com/java-programming/exceptions
Programming and Coding Skills
Java try...catch
• The try...catch block in Java is used to handle exceptions and prevents the abnormal termination
of the program.
• The try block includes the code that might generate an exception.
• The catch block includes the code that is executed when there occurs an exception inside the try
block.
• In Java, we can use a try block without a catch block. However, we cannot use a catch block
without a try block.
Programming and Coding Skills
Finally Block
• The finally block is always executed whether there
is an exception inside the try block or not.
• The process of converting the value of one data type (int, float, double, etc.) to another data type is
known as typecasting.
• In Widening Type Casting, Java automatically converts one data type to another data type.
• The lower data type (having smaller size) is converted into the higher data type (having larger size).
Hence there is no loss in data.
• We manually convert one data type into another using the parenthesis.
• The String array stores all the arguments passed through the
command line.
Programming and Coding Skills
Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/aziz where
jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also
use IP address, 3306 is the port number and aziz is the database name. We may use any database, in such
case, we need to replace the aziz with our database name.
Password: It is the password given by the user at the time of installing the mysql database. In this example,
we are going to use root as the password.
Programming and Coding Skills
Let's first create a table in the mysql database, but before creating table, we need to create database
first.
In this example, aziz is the database name, root is the username and password both.
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/aziz","root","root");
//here aziz is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
Programming and Coding Skills
Summary
Quiz
a) Database management
b) Graphic design
c) Cross-platform programming
d) Video editing
Answer: c
Cross-platform programming
Programming and Coding Skills
Quiz
a) int
b) float
c) string
d) char
Answer: c
string
Programming and Coding Skills
Quiz
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Abstraction
Answer: b
Encapsulation
Programming and Coding Skills
Quiz
a) object
b) instance
c) new
d) create
Answer: c
new
Programming and Coding Skills
Thank you!