Unit-1 Java
Unit-1 Java
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.
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.
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.
The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes.
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.
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.
(Think of it this way: you have a group of superheroes with different abilities, but they
can all be referred to as "superheroes".)
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:
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:
2. Period-Delimited Structure:
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