0% found this document useful (0 votes)
2 views45 pages

Unit-1 Java

The document provides an overview of various Java programming concepts including arithmetic, assignment, comparison, logical, bitwise, and control flow operators. It discusses object-oriented programming principles such as encapsulation, inheritance, polymorphism, and abstraction, along with the use of classes, methods, and constructors. Additionally, it covers packages, CLASSPATH, JAR files, and naming conventions in Java, concluding with a note on import statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views45 pages

Unit-1 Java

The document provides an overview of various Java programming concepts including arithmetic, assignment, comparison, logical, bitwise, and control flow operators. It discusses object-oriented programming principles such as encapsulation, inheritance, polymorphism, and abstraction, along with the use of classes, methods, and constructors. Additionally, it covers packages, CLASSPATH, JAR files, and naming conventions in Java, concluding with a note on import statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 45

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.


Assignment Operators
Assignment operators are used to assign values to variables.
Comparison Operators
Comparison operators are used to compare two values (or variables). This is important
in programming, because it helps us to find answers and make decisions.
The return value of a comparison is either true or false.
Logical Operators
You can also test for true or false values with logical operators.
Logical Operators
You can also test for true or false values with logical operators.
Bitwise Operators
Bitwise operators are used to performing the manipulation of individual bits of a
number. They can be used with any integral type (char, short, int, etc.).
Control Flow in Java
● Java compiler executes the code from top to bottom.
● Java provides statements that can be used to control the flow of Java code.
Such statements are called control flow statements.
● It is one of the fundamental features of Java, which provides a smooth flow of
program.

Java provides three types of control flow statements.


1. Decision Making Statements -> if statements and switch statement
2. Loops Statements -> for, while, do while, for each loop
3. Jump Statements -> Break and Continue Statement
1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is
diverted depending upon the specific condition. The condition of the If statement gives
a Boolean value, either true or false. In Java, there are four types of if-statements given
below.

1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement

LETS CODE….
SWITCH:
In Java, a switch statement is a control flow statement that allows you to execute
different blocks of code based on the value of a variable or expression.
Loop Statements
In programming, sometimes we need to execute the block of code repeatedly while
some condition evaluates to true. However, loop statements are used to execute the set
of instructions in a repeated order. The execution of the set of instructions depends
upon a particular condition.

1. for loop
2. while loop
3. do-while loop
4. for-each loop

Lets Code…
while loop flowchart do-while loop flowchart
Jump Statements
Jump statements are used to transfer the control of the program to the specific
statements.

Java break statement


the break statement is used to break the current flow of the program and transfer
the control to the next statement outside a loop or switch statement.

continue statement
Unlike break statement, the continue statement doesn't break the loop, whereas, it
skips the specific part of the loop and jumps to the next iteration of the loop
immediately.
Arrays in Java
Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure
where we store similar elements. We can store only a fixed set of elements in a Java
array.
Advantages:
Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
Random access: We can get any data located at an index position.
Disadvantages:
Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its
size at runtime.
String in Java
In Java, string is basically an object that represents sequence of char values. An
array of characters works same as Java string.

How to create a string object?

There are two ways to create String object:

1. By string literal
2. By new keyword
OOPs (Object-Oriented Programming System)
Object means a real-world entity such as a pen, chair, table, computer, watch, etc.
Object-Oriented Programming is a methodology or paradigm to design a program
using classes and objects. It simplifies software development and maintenance by
providing some concepts:

● Object
● Class
● Inheritance
● Polymorphism
● Abstraction
● Encapsulation
Class
A class is a basic building block. It can be defined as template that describes the data
and behaviour associated with the class instantiation. Instantiating is a class is to create
an object (variable) of that class that can be used to access the member variables and
methods of the class.
An object in Java is the physical as well as a logical entity, whereas, a class in Java is a
logical entity only.
Syntax:
<access specifier> class class_name
{
}
What is a method in Java?
A method is a reusable block of code designed to perform specific tasks. By calling
or invoking it, we execute its functionality without needing to rewrite the code.
This promotes code reusability, easy modification, and enhances code readability.
Access Specifier:
Access specifier or modifier is the access type of the method. It specifies the visibility
of the method. Java provides four types of access specifier:

● Public: The method is accessible by all classes when we use public specifier in our
application.
● Private: When we use a private access specifier, the method is accessible only in
the classes in which it is defined.
● Protected: When we use protected access specifier, the method is accessible within
the same package or subclasses in a different package.
● Default: When we do not use any access specifier in the method declaration, Java
uses default access specifier by default. It is visible only from the same package
only.
Static Members:
● Static members belong to the class rather than to any specific instance of the class.
● They are shared among all instances of the class.
● Static members can be accessed directly using the class name without needing to
instantiate an object.

Final Members:
● Final members are constants that cannot be changed once initialized.
● For variables, once a final variable is assigned a value, it cannot be reassigned.
● For methods, a final method cannot be overridden by subclasses.
● For classes, a final class cannot be subclassed.
● Final members are often used to define constants or to prevent further
modification of certain elements in a program.
Constructors in Java
A constructor is a block of codes similar to the method. It is called when an instance of
the class is created. At the time of calling constructor, memory for the object is
allocated in the memory.

It is a special type of method which is used to initialize the object.

Rules for creating Java constructor

1. Constructor name must be the same as its class name


2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and synchronized

Types: Default and Parameterised


Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system).

The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes.

Why use inheritance in java

● For Method Overriding (so runtime polymorphism can be achieved).


● For Code Reusability.
Terms used in Inheritance
● Class: A class is a group of objects which have common properties. It is a
template or blueprint from which objects are created.
● Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.
● Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
● Reusability: As the name specifies, reusability is a mechanism which facilitates
you to reuse the fields and methods of the existing class when you create a
new class.
The syntax of Java Inheritance
class Subclass-name extends Superclass-name

//methods and fields

The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
Method Overloading
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.

There are two ways to overload the method in java:

● By changing number of arguments


● By changing the data type
Method Overriding
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.

Rules for Java Method Overriding:

1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an inheritance.
Encapsulation
Encapsulation in Java is a process of wrapping code and data together into a single
unit, for example, a capsule which is mixed of several medicines.

We can create a fully encapsulated class in Java by making all the data members of the
class private. Now we can use setter and getter methods to set and get the data in it.

The Java Bean class is the example of a fully encapsulated class.


Polymorphism
Polymorphism in Java is a fundamental concept in object-oriented programming
(OOP) that allows objects of different classes to be treated as objects of a common
superclass. This allows for flexibility and extensibility in your code.

(Think of it this way: you have a group of superheroes with different abilities, but they
can all be referred to as "superheroes".)

There are two main types of polymorphism in Java:

1. Compile-time Polymorphism: This is achieved through method overloading.


2. Runtime Polymorphism: This is achieved through method overriding.
Abstraction
Abstraction in Java is another core concept of object-oriented programming (OOP),
alongside encapsulation, inheritance, and polymorphism. It involves hiding the
complex implementation details of a system and showing only the essential features
of the object.
Abstraction can be achieved in Java using two main mechanisms:
1. Abstract Classes
2. Interfaces

Abstraction allows you to focus on what an object does rather than how it does it,
promoting code reusability, maintainability, and flexibility in large software systems.
Abstract Classes
An abstract class is a class that cannot be instantiated on its own and may contain
abstract methods (methods without a body) as well as concrete methods (methods
with a body). Abstract methods serve as placeholders for methods that must be
implemented by subclasses. Abstract classes can have constructors and instance
variables just like any other class.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}}
In this example, Shape is an abstract class with an abstract method draw(). Subclasse
Circle extend the Shape class and provide concrete implementations of the draw()
method.
Interfaces
An interface in Java is a blueprint of a class that defines a set of abstract methods.
Unlike abstract classes, interfaces cannot have instance variables or constructors.
Classes can implement multiple interfaces, allowing them to inherit behavior from
multiple sources.
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}}
In this example, the Animal interface declares an abstract method sound().The
Dog class implement the Animal interface and provide their own implementation
of the sound() method.
Package in JAVA
In Java, a package is a mechanism for organizing classes, interfaces, and
Sub-Packages, which helps in avoiding name conflicts and controlling access.
Packages are also used to group related classes and interfaces together, making
the code more modular and easier to manage.
Package in java can be categorized in two form, built-in package and
user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io,
util, sql etc.
Example of java package
The package keyword is used to create a package in java.
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
CLASSPATH in Java
The CLASSPATH is an environment variable used by the Java compiler
(javac) and the Java runtime (java) to locate classes that are referenced in your
program. Setting the CLASSPATH correctly is crucial for compiling and
running Java programs that are organized into packages.
You can set the CLASSPATH in various ways:
a. Setting CLASSPATH Temporarily
b. Setting CLASSPATH Permanently
a. Setting CLASSPATH Temporarily
On Windows:

set PATH=.;C:\Program Files\Java\JDK1.6.20\bin


On Unix/Linux/Mac:

sudo <editor name> /etc/environment


JAVA_HOME = "/usr/lib/jvm/<java folder (eg. java-1.8.0-openjdk-amd64>)/bin"
export JAVA_HOME
CLASSPATH=".:/usr/lib/jvm/<java folder>/lib:/home/name/Desktop"
export CLASSPATH
b. Setting CLASSPATH Permanently
On Windows:

1. Right-click on This PC or My Computer and select Properties.


2. Click on Advanced system settings.
3. Click on the Environment Variables button.
4. In the System variables section, click New or select the existing CLASSPATH
variable and click Edit.
5. Add your paths to the CLASSPATH variable.
JAR files in Java
A JAR (Java Archive) is a package file format typically used to aggregate many
Java class files and associated metadata and resources (text, images, etc.) into one
file to distribute application software or libraries on the Java platform.

In simple words, a JAR file is a file that contains a compressed version of .class
files, audio files, image files, or directories. We can imagine a .jar file as a zipped
file(.zip) that is created by using WinZip software. Even, WinZip software can be
used to extract the contents of a .jar . So you can use them for tasks such as lossless
data compression, archiving, decompression, and archive unpacking.
Create a JAR file
Syntax:

jar cf jarfilename inputfiles


Here, cf represents to create the file. For example , assuming our package pack is
available in C:\directory , to convert it into a jar file into the pack.jar , we can give the
command as:

C:\> jar cf pack.jar pack


Naming Conventions for Java Packages:

For avoiding unwanted package names, we have some following naming


conventions which we use in creating a package.
1. Use Lowercase Letters:

E.g: package com.example.myapp;

2. Period-Delimited Structure:

E.g: package com.example.myapp.controllers;

3. Base Names on Company or Organization:

E.g: package com.example.myapp.services;


import and static import statement
Java 5.0 introduced static import, also known as the tiger version, as a new language
feature. Static variables and static members can now be accessed directly without
having to use class names.

We had normal import statements for every java class before static import was
introduced; we used class names to call methods and classes. Below is an example of a
normal import for the List class.

import java.util.List;
import static java.lang.Math.*;
UNIT-1 is Completed
Thank You
Subscribe For More Amazing Explanations
Checkout Desc. For Notes and More Subjects Playlist

You might also like