0% found this document useful (0 votes)
29 views

1695206978-4.1 Essentials of Java Programming Language

Uploaded by

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

1695206978-4.1 Essentials of Java Programming Language

Uploaded by

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

Programming and Coding Skills

Programming and Coding Skills


Programming and Coding Skills

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​

• Introduction to JAVA Programming


• Java Fundamentals
• Java Control Statements
• Java Arrays
• Java Class and Objects
• Important Topics of OOPS Concept
• Java Exception Handling
• Additional Topics

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

Java JDK, JRE and JVM


What is JVM?

JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java program.

Working of Java Program

Source: https://www.programiz.com/java-programming/jvm-jre-jdk
Programming and Coding Skills

Java JDK, JRE and JVM


What is JRE?

• JRE (Java Runtime Environment) is a software package.

• It provides Java class libraries, Java Virtual Machine (JVM), and other components that are required to run
Java applications.

Java Runtime Environment

Source: https://www.programiz.com/java-programming/jvm-jre-jdk
Programming and Coding Skills

Java JDK, JRE and JVM


What is JDK?

• JDK (Java Development Kit) is a software development kit required to develop applications in Java.

Java Development Kit

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.

• To indicate the storage area, each variable should


be given a unique name (identifier).
Programming and Coding Skills

Java Data Types


• Data types specify the type of data that can be stored
inside variables in Java.

There are 8 Primitive Data Types:

• Boolean
• Byte
• Short
• Int
• Long
• Double
• Float
• Char
Programming and Coding Skills

Java Operators

• Operators are symbols that perform operations on variables and values.

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

Lab 22: Installing Java and Writing a Hello World Program


Programming and Coding Skills

Java Basic Input and Output


Java Output: In Java, we simply use

• System.out.println(); or System.out.print(); or System.out.printf();

Java Input: Java provides different ways to get input from the user. One of them is by using
object of Scanner class.

• First, we need to import java.util.Scanner package.

• Then, we need to create an object of the Scanner class.

• We can use the object to take input from the user.


Programming and Coding Skills

Java Expressions, Statements and Blocks


• Java Expressions: A Java expression consists of variables, operators, literals, and method calls.

• Java Statements: In Java, each statement is a complete unit of execution.

• Expression statements: We can convert an expression into a statement by terminating the


expression with a.

• 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.

In Java, there are two types of comments:

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

Java if...else Statement


• We use the if..else statement to run a block of code
among more than one alternative.

• The if statement executes a certain section of code if the How the if...else statement works?
test expression is evaluated to true.

• However, if the test expression is evaluated too false, it


does nothing.

• Statements inside the body of else block are executed if


the test expression is evaluated too false.

Source: https://www.programiz.com/java-programming/if-else-statement
Programming and Coding Skills

Java Switch Statement


• The switch statement allows us to execute a block of code among Flowchart of Switch Statement
many alternatives.

• The expression is evaluated once and compared with the values of


each case.

• The working of the switch-case statement is like the Java if...else...if


ladder.

• However, the syntax of the switch statement is cleaner and much


easier to read and write.

Source: https://www.programiz.com/java-programming/switch-statement
Programming and Coding Skills

Lab 23: Java Conditional Statements


Programming and Coding Skills

Java for Loop


• In computer programming, loops are used to repeat Flowchart of Java for loop
a block of code.

• Java for loop is used to run a block of code for a


certain number of times.

Source: https://www.programiz.com/java-programming/for-loop
Programming and Coding Skills

Java for-each Loop


• In Java, the for-each loop is used to iterate through
elements of arrays and collections (like ArrayList).

• It is also known as the enhanced for loop.

The syntax of the Java for-each loop is:

for(dataType item : array) {


...
}
Programming and Coding Skills

Java while Loop


Java while loop is used to run a specific code until a Flowchart of while loop
certain condition is met.

Source: https://www.programiz.com/java-programming/do-while-loop
Programming and Coding Skills

Java break Statement


• While working with loops, it is sometimes desirable Working of Java break Statement
to skip some statements inside the loop or terminate
the loop immediately without checking the test
expression.

• The break statement in Java terminates the loop


immediately, and the control of the program moves
to the next statement following the loop.

• It is almost always used with decision-making


statements (Java if...else Statement).

Source: https://www.programiz.com/java-programming/break-statement
Programming and Coding Skills

Java continue Statement


• The continue statement skips the current iteration of a
Working of Java continue Statement
loop (for, while, do...while, etc).

• After the continue statement, the program moves to the


end of the loop.

• And test expression is evaluated (update statement is


evaluated in case of the for loop).

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.

• If the size of an array is n, then the last element of


the array will be at index n-1.

Source: https://www.programiz.com/java-programming/arrays
Programming and Coding Skills

Java Multidimensional Arrays


• A multidimensional array is an array of arrays. 2-dimensional Array

• Each element of a multidimensional array is an array


itself.

• Each element of the multidimensional array is an


array itself.

• unlike C/C++, each row of the multidimensional array


in Java can be of different lengths.

Source: https://www.programiz.com/java-programming/multidimensional-array
Programming and Coding Skills

Lab 24: Java Loop Statements


Programming and Coding Skills

Java Class and Objects


• Java Class

• A class is a blueprint for the object. Before we


create an object, we first need to define the
class.

• Java Objects

• An object is called an instance of a class.


Programming and Coding Skills

Java Methods
• A method is a block of code that performs a specific
task.

• Dividing a complex problem into smaller chunks


makes your program easy to understand and
reusable.

In Java, there are two types of methods:

1. User-defined Methods
2. Standard Library Methods
Programming and Coding Skills

Java Constructors

What is a Constructor?

• A constructor in Java is like a method that is


invoked when an object of the class is created.

• Unlike Java methods, a constructor has the same


name as that of the class and does not have any
return type.
Programming and Coding Skills

Java Constructors
Types of Constructor

In Java, constructors can be divided into 3 types:

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

• if...else statement is used to terminate the recursive call


inside the method.

• A recursive solution is much simpler and takes less


time to write, debug and maintain.

• Recursion generally uses more memory and is


generally slow.

Source: https://www.programiz.com/java-programming/recursion
Programming and Coding Skills

Lab 25: Java Methods, Constructors and Recursion


Programming and Coding Skills

Java Strings
• In Java, a string is a sequence of characters.

• We use double quotes to represent a string in Java.

• Strings in Java are not primitive types (like int, char,


etc). Instead, all strings are objects of a predefined
class named String.

• All string variables are instances of the String class.

• In Java, strings are immutable.


Programming and Coding Skills

this Keyword
In Java, this keyword is used to refer to the current object inside a method or a constructor.

There are various situations where this keyword is commonly used:

• Using this for ambiguity variable names


• Using this in constructor overloading
• Passing this as an argument
Programming and Coding Skills

Java final Keyword


• In Java, the final keyword is used to denote constants. It can be used with variables,
methods, and classes.

• The final variable cannot be reinitialized with another value

• The final method cannot be overridden

• The final class cannot be extended


Programming and Coding Skills

Understanding OOP concepts through core Java


OOP concepts include:

• 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.

• Using Inheritance lets us build on previous work


without reinventing the wheel.

Source: https://techvidvan.com/tutorials/java-inheritance
Programming and Coding Skills

How Inheritance Works?


• Inheritance lets a new class adopt the properties of another.
• Inheriting class is called a subclass or a child class.
• The original class is often called the parent.
• Keyword extends is used to define a new class that inherits
properties from an old class.

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

• The extends keyword is used to perform inheritance in Java.

There are five types of inheritance:

1 2

Single Inheritance Multilevel Inheritance

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

Uses of Super Keyword:

1. To call methods of the superclass that is overridden in the subclass.

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

Lab 26: Inheritance in JAVA


Programming and Coding Skills

Polymorphism
• Allows programmers to use the same word in Java
to mean different things in different contexts.

• One form of polymorphism is method overloading.

• The other form is method overriding.

Source: https://codegym.cc/groups/posts/polymorphism-in-java
Programming and Coding Skills

How Polymorphism Works?


• We might create a class called “horse” by extending
the “animal” class.

• That class might also implement the “professional


racing” class.

• The “horse” class is “polymorphic,” since it inherits


attributes of both the “animal” and “professional
racing” class.

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

• It simply means more than one form.

• Polymorphism allows us to create consistent code.

We can achieve polymorphism in Java using the


following ways:

1. Method Overriding
2. Method Overloading

Source: https://www.programiz.com/java-programming/polymorphism
Programming and Coding Skills

Method Overloading

• A single method may perform different functions


depending on the context in which it’s called.

• This means a single method name might work in


different ways depending on what arguments are
passed to it.

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.

• That allows a programmer to use one method in


different ways depending on whether it’s invoked by
an object of the parent class or an object of the child
class.

Source: https://techvidvan.com/tutorials/method-overloading-and-overriding
Programming and Coding Skills

Java Method Overriding


• If the same method is defined in both the superclass
and the subclass, then the method of the subclass
class overrides the method of the superclass.

• This is known as method overriding.

Source: https://www.programiz.com/java-programming/method-overriding
Programming and Coding Skills

Java Method Overriding


Java Overriding Rules:

• Both the superclass and the subclass must have the same method name, the same return type
and the same parameter list.

• We cannot override the method declared as final and static.

• We should always override abstract methods of the superclass


Programming and Coding Skills

Lab 27: Polymorphism in Java


Programming and Coding Skills

Abstraction
• In Java, abstraction means simple things like
objects, classes and variables represent more
complex underlying code and data.

• It lets you avoid repeating the same work multiple


times.

Source: https://www.tutorialkart.com/java/abstraction-in-java
Programming and Coding Skills

How Abstraction Works?


• Abstraction lets programmers create useful and
reusable tools.

• For example, a programmer can create several


different types of objects, which can be variables,
functions or data structures. Programmers can also
create different classes of objects as ways to define
the objects.

Source: https://techvidvan.com/tutorials/abstraction-in-java
Programming and Coding Skills

Java Abstract Class


• The abstract class in Java cannot be instantiated (we cannot create objects of abstract classes).

• We use the abstract keyword to declare an abstract class.

• 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.

• An abstract class can have constructors like the regular class.


Programming and Coding Skills

Java Interface
• An interface is a fully abstract class. It includes a group of abstract methods .

• We use the interface keyword to create an interface in Java.

• Like abstract classes, we cannot create objects of interfaces.

• To use an interface, other classes must implement it.

• We use the implements keyword to implement an interface.

• In Java, a class can also implement multiple interfaces.

• Similar to classes, interfaces can extend other interfaces.


Programming and Coding Skills

Lab 28: Abstraction in Java


Programming and Coding Skills

Encapsulation
• Encapsulation is a protective barrier that keeps the
data and code safe within the class itself.

• We can then reuse objects like code components or


variables without allowing open access to the data
system-wide.

Source: https://techblogstation.com/java/oops-concepts-in-java
Programming and Coding Skills

How Encapsulation Works?

• Encapsulation lets us reuse functionality without


compromising security.

• It’s a powerful, time-saving OOP concept in Java.

• For example, we may create a piece of code that


calls specific data from a database.

Source: https://javatutorial.net/java-encapsulation-example
Programming and Coding Skills

Java Access Modifiers

• 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.

There are four access modifiers keywords in Java:

1. Default
2. Private
3. Protected
4. Public

Source: https://www.programiz.com/java-programming/access-modifiers
Programming and Coding Skills

Lab 29: Encapsulation in Java


Programming and Coding Skills

Java Exceptions
• An exception is an unexpected event that occurs Java Exception Hierarchy
during program execution. Image source:

Errors

• Errors represent irrecoverable conditions


• Errors are usually beyond the control of the
programmer, and we should not try to handle errors.

Exceptions

• Exceptions can be caught and handled by the


program.

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

Multiple Catch Blocks


• For each try block, there can be zero or more catch
blocks.

• Multiple catch blocks allow us to handle each


exception differently.

• The argument type of each catch block indicates the


type of exception that can be handled by it.
Programming and Coding Skills

Finally Block
• The finally block is always executed whether there
is an exception inside the try block or not.

• The code inside the finally block is executed


irrespective of the exception.

• It is a good practice to use finally block to include


important cleanup code like closing a file or
connection.
Programming and Coding Skills

Java throw and throws


Java throws keyword

• The throws keyword in the method declaration to


declare the type of exceptions that might occur
within it.

Java throw keyword

• The throw keyword is used to explicitly throw a


single exception.
Programming and Coding Skills

Lab 30: Exception Handling in Java


Programming and Coding Skills

Java Scanner Class

• The Scanner class of the java.util package is used to read


input data from different sources like input streams, users,
files, etc.

• The Scanner class provides various methods that allow us


to read inputs of different types.

• Examples are nextInt(), nextFloat(), nextBoolean(),


nextLine(), etc.
Programming and Coding Skills

Java Wrapper Class

• The wrapper classes in Java are used to convert


primitive types (int, char, float, etc) into
corresponding objects.

• Each of the 8 primitive types has corresponding


wrapper classes.

• In Java, sometimes we might need to use objects


instead of primitive data types. For example, while
working with collections. In such cases, wrapper
classes help us to use primitive data types as
objects.

• We can store the null value in wrapper objects.


Programming and Coding Skills

Java Type Casting

• 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.

• This is also known as Explicit Type Casting.


Programming and Coding Skills

Java Command-Line Arguments


• The command-line arguments in Java allow us to pass
arguments during the execution of the program.

• public static void main(String[] args) {...}

• The String array stores all the arguments passed through the
command line.
Programming and Coding Skills

Lab 31: Taking User Inputs in Java


Programming and Coding Skills

Java Database Connectivity with MySQL


To connect Java application with the MySQL database, we need to follow 5 following steps.

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.

Username: The default username for the mysql database is root.

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

Lab 32: Database Connectivity in Java


Programming and Coding Skills

Java Database Connectivity with MySQL

Let's first create a table in the mysql database, but before creating table, we need to create database
first.

create database aziz;


use aziz;
create table emp(id int(10),name varchar(40),age int(3));
Programming and Coding Skills

Example to Connect Java Application with Mysql Database

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

• Java is an Object-Oriented Programming Language.

• Java is used for building a wide range of software


applications, from web and mobile apps to desktop
programs and server systems.

• Java can be integrated with different database


management system software too.
Programming and Coding Skills

Quiz

1. What is Java primarily known for?

a) Database management
b) Graphic design
c) Cross-platform programming
d) Video editing

Answer: c
Cross-platform programming
Programming and Coding Skills

Quiz

2. Which of the following is NOT a primitive data type in Java?

a) int
b) float
c) string
d) char

Answer: c
string
Programming and Coding Skills

Quiz

3. What is the process of combining data and functions into a


single unit called in Java?

a) Inheritance
b) Encapsulation
c) Polymorphism
d) Abstraction

Answer: b
Encapsulation
Programming and Coding Skills

Quiz

4. Which keyword is used to create a new instance of a


class in Java?

a) object
b) instance
c) new
d) create

Answer: c
new
Programming and Coding Skills

Thank you!

You might also like