DishuNotes
DishuNotes
URL:- https://github.com/meanbypawan/ITEP-5.git
What is Java?
Java is a high level, robust, object-oriented and secure and
simple programming language.
It is used for:
Features of Java
The features of Java are also known as Java buzzwords.
A list of the most important features of the Java language is given below.
java
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10.Multithreaded
11.Distributed
12.Dynamic
java
C++ vs Java
dependent C++ is platform- Java is platform-independent.
dependent.
ainly used
r
C++ is mainly used for
system programming.
java
Java is mainly used for application programming. It is widely used
Windows-based, web-based, enterprise, and mobile applications.
esign Goal C++ was designed for Java was designed and created as an interpreter for printing system
systems and but later extended as a support network computing. It was design
applications to be easy to use and accessible to a broader audience.
programming. It was
an extension of the C
programming
language.
ultiple C++ supports multiple Java doesn't support multiple inheritance through class. It can
heritance inheritance. achieved by using interfaces in java.
ointers C++ Java supports pointer internally. However, you can't write the point
supports pointers. You program in java. It means java has restricted pointer support in java.
can write a pointer
program in C++.
ompiler C++ uses compiler Java uses both compiler and interpreter. Java source code is converte
nd only. C++ is compiled into bytecode at compilation time. The interpreter executes th
terpreter and run using the bytecode at runtime and produces output. Java is interpreted that
compiler which why it is platform-independent.
converts source code
into machine code so,
C++ is platform
dependent.
all by Value C++ supports both call Java supports call by value only. There is no call by reference in java.
nd Call by by value and call by
ference reference.
ocumentati C++ doesn't support Java supports documentation comment (/** ... */) to crea
n comment documentation documentation for java source code.
comments.
java
First Java Program
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
Compilation Flow:
When we compile Java program using javac tool, the Java compiler converts the
source code into byte code.
Bytecode Verifier: Checks the code fragments for illegal code that can violate
access rights to objects.
Types of Variables
There are three types of variables in Java:
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use
this variable only within that method and the other methods in the class aren't even
aware that the variable exists.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared
among instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You
can create a single copy of the static variable and share it among all the instances of
the class. Memory allocation for static variables happens only once when the class is
loaded in the memory.
java
Class :- class is a template used to create objects and to define object data
types and methods.
Class is a user define type.
java
Class is the collection of similar type of objects.
Once we define a class then we create n number of object.
Class is a blueprint of object.
Each and everything is a member of the class
Object
A Java object is a member of a Java class. Each object has an identity, a
behavior and a state. The state of an object is stored in fields (variables),
while methods (functions) display the object's behavior. Objects are
created at runtime
Object which you want to manipulate first thing about that then identify
the properties of the object then identify the behavior of the object then
finally create a factory to form/represent the object.
Predefined methods: These are the methods that are already defined in
the Java class libraries and do not need to be created by the developer. For
example, the max() method is present in the Math class in Java.
User-defined methods: These are the methods that are created by the
developer to perform certain actions according to the requirements. These
methods can have parameters, return values, access modifiers, and
exceptions.
java
Encapsulation
Encapsulation is a wrapping up data and function together into a single unit is
known as encapsulation.
Abstraction
Hiding the unnecessary information from the user and showing only essential
thing.
or
Use something without knowing its background details / functioning.
Inheritance
By using existing thing to develop something new but there should not be any
changes in existing thing.
Polymorphism
One thing has a different implementation is called polymorphism.
or
To achieve more thing from one thing is called polymorphism
java
Identifier Naming Rules Examples
s Type
The following table shows the popular conventions used for the
different identifiers.
If the name is combined with two words, the second word will start
with uppercase letter always such as actionPerformed(),
firstName, ActionEvent, ActionListener, etc.
java
Java Package
There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc.
Built-in packages include java, lang, awt, javax, swing, net, io, util, sql etc 1
User-defined packages are created by developers to categorize their classes and
interfaces. The package statement should be the first statement in the source file.
Method in Java is a block of code or a collection of statements that performs a
specific task or operation1. A method in Java is also known as a function.
Predefined methods: These are the methods that are already defined in the
Java class libraries and do not need to be created by the developer. For
example, the max() method is present in the Math class in Java.
java
User-defined methods: These are the methods that are created by the
developer to perform certain actions according to the requirements. These
methods can have parameters, return values, access modifiers, and
exceptions.
Features of java
JVMs are available for many hardware and software platforms. JVM, JRE, and JDK
are platform dependent because the configuration of each OS is different from
each other. However, Java is platform independent. There are three notions of the
JVM: specification, implementation, and instance.
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The
Java Runtime Environment is a set of software tools which are used for developing
Java applications. It is used to provide the runtime environment. It is the
implementation of JVM. It physically exists. It contains a set of libraries + other
files that JVM uses at runtime.
JDK is an implementation of any one of the below given Java Platforms released by
Oracle Corporation:
The JDK contains a private Java Virtual Machine (JVM) and a few other resources
such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc), etc. to complete the development of a Java
Application.
java
6) How many types of memory areas are allocated by JVM?
Many types:
}
Class PrintStream {
Public void println()
{
}
java
Concept of OOPs
Datatype A datatype in Java is a way of specifying the size and type of variable
values. There are two types of datatypes in Java: primitive and non-primitive.
Primitive datatypes are the basic types of data that have no additional
methods. They include boolean, char, byte, short, int, long, float and double. Each
of these types has a different size and range of values.
Non-primitive datatypes are the types of data that are derived from primitive
types or defined by the programmer. They include String, Arrays and Classes. Non-
primitive datatypes can have methods to perform certain operations on their
values.
operator variable constant compilation
java
Int data range (-128 to 127)
java
Operator
(i) Associativity (ii) Precedence (Priority)
L R RL
Operators are used to perform operations on variables and values.
Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction,
multiplication, and division. They act as basic mathematical operations.
&& Logical and Returns true if both statements are x < 5 && x <
true 10
! Logical not Reverse the result, returns false if the !(x < 5 && x <
result is true 10)
In Java, operators that perform comparisons of two operands are called Relational
operators. There are a total of six of them:
~ 0101
________
1010 = 10 (In decimal)
java
Unary operator (- ++ -- ~)
Binary (+ - * / …)
Ternary (conditional operator)
1. Arithmetic operator(+ - / * %)
2. Relational operator ( < > = <= >= ~=)
3. Logical operator (&& || !)
4. Increment operator(++ --)
5. Assignment (=)
6. Bitwise (& | ! << >> ^ ~)
7. Conditional operator(? :)
8. Shorthand operator (+= -= *= ……)
9. Instance operator
Error : - Exception in thread “main” java.lang.AithmeticException: / by zero at
Test.main(file.java linenumber) runtime error
Floating point divided by zero = infinity
7/2: 3
2/7: 0
-7/2 :-3
7/-2 : -3
7%2 : 1
-7%-2 : -1
2%7 : 2
7.5 %2 : 1.5
7.5/0 : infinity
-7.5/0 : -infinity
7.5%0 : naN
Scanner:-
java
Control Statements In Java, control statements are used to control the flow of a
program. Control statements can be divided into three categories: selection
statements, iteration statements, and jump statements1.
Selection statements execute a piece of code based on some condition. The most
common selection statement is the if statement which determines whether a code
should be executed based on the specified condition2.
Jump statements transfer control to another part of the program. The most
common jump statement is the break statement which terminates the loop or
switch statement and transfers control to the statement immediately following the
loop or switch4
Loops in Java
The Java for loop is used to iterate a part of the program several
times. If the number of iteration is fixed, it is recommended to use
for loop.
\
BLACK u001B[30 BLACK_BACKGROUND \u001B[40m
m
\
RED u001B[31 RED_BACKGROUND \u001B[41m
m
\
GREEN u001B[32 GREEN_BACKGROUND \u001B[42m
m
\
YELLOW_BACKGROUN
YELLOW u001B[33 \u001B[43m
D
m
\
BLUE u001B[34 BLUE_BACKGROUND \u001B[44m
m
\
PURPLE u001B[35 PURPLE_BACKGROUND \u001B[45m
m
\
CYAN u001B[36 CYAN_BACKGROUND \u001B[46m
m
m
java
Constructor :-
1. class Bike9{
2. final int speedlimit=90;//final variable
3. void run(){
4. speedlimit=400;
5. }
6. public static void main(String args[]){
7. Bike9 obj=new Bike9();
8. obj.run();
9. }
10. }//end of class
java
Abstract class in Java
Inheritance
By using existing thing to develop something new but there should not any
change in existing thing.
Object of one class acquire the properties of another object is called inheritance.
Java using extends keyword to inherent property of another class.
There are three types of inheritance
Single
Multilevel
Hierarichal
Why Code reusability
Interface in Java
An interface in Java is a blueprint of a class. It has static
constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There
can be only abstract methods in the Java interface, not method
body. It is used to achieve abstraction and multiple inheritance in
Java.
In other words, you can say that Java Interface also represents
the IS-A relationship.
java
It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an
interface.
Since Java 9, we can have private methods in an interface.
Why use Java interface?
There are mainly three reasons to use interface. They are given
below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple
inheritance.
o It can be used to achieve loose coupling.
Syntax:
1. interface <interface_name>{
2. // declare constant fields
3. // declare methods that abstract
4. // by default.
5. }
interfaces can have abstract methods and variables. It cannot
have a method body.
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class,
an interface extends another interface, but a class implements
an interface.
java
Java Interface Example
In this example, the Printable interface has only one method, and
its implementation is provided in the A6 class.
1. interface printable{
2. void print();
3. }
4. class A6 implements printable{
5. public void print()
6. {
7. System.out.println("Hello");
8. }
9. public static void main(String args[]){
10. A6 obj = new A6();
11. obj.print(); } }
class outerClass {
class AnonymousDemo {
public void createClass() {
class Main {
public static void main(String[] args) {
AnonymousDemo an = new AnonymousDemo();
an.createClass();
}
}
Types of Exceptions
Java defines several types of exceptions that relate to its various
class libraries. Java also allows users to define their own
exceptions.
java
Exceptions can be categorized in two ways:
1. Built-in Exceptions
Checked Exception
Unchecked Exception
2. User-Defined Exceptions
Let us discuss the above-defined listed exception that is as
follows:
1. Built-in Exceptions
Built-in exceptions are the exceptions that are available in Java
libraries. These exceptions are suitable to explain certain error
situations.
Checked Exceptions: Checked exceptions are called
compile-time exceptions because these exceptions are
checked at compile-time by the compiler.
Unchecked Exceptions: The unchecked exceptions are
just opposite to the checked exceptions. The compiler will
not check these exceptions at compile time. In simple
words, if a program throws an unchecked exception, and
even if we didn’t handle or declare it, the program would
not give a compilation error.
2. User-Defined Exceptions:
Sometimes, the built-in exceptions in Java are not able to
describe a certain situation. In such cases, users can also create
exceptions, which are called ‘user-defined Exceptions’.
The advantages of Exception Handling in Java are as follows:
1. Provision to Complete Program Execution
2. Easy Identification of Program Code and Error-Handling
Code
3. Propagation of Errors
4. Meaningful Error Reporting
5. Identifying Error Types
Keyword Description
IO Or File Handling
In Java, with the help of File Class, we can work with files. This File
Class is inside the java.io package. The File class can be used by
creating an object of the class and then specifying the name of
the file.
Why File Handling is Required?
File Handling is an integral part of any programming language
as fi2153le handling enables us to store the output of any
particular program in a file and allows us to perform certain
operations on it.
In simple words, file handling means reading and writing data
to a file.
java
// Importing File Class
import java.io.File;
class GFG {
System.out.println("File Created!");
Output
File Created!
No Object Class
.
9)Example: Example:
public abstract class public interface Drawable{
Shape{ void draw();
public abstract void draw(); }
}
java
Difference between method overloading and method overriding in java
47) What is the difference between static (class) method and instance
method?