java unit-1 final (1)
java unit-1 final (1)
Prepared By:
Dr. Nidhi H. Divecha
• James Gosling is known as the father of Java. Before Java, its name
was Oak. Since Oak was already a registered company, so James
Gosling and his team changed the name from Oak to Java.
It is used for:
4) JavaFX
It is used to develop rich internet applications. It uses a lightweight user
interface API.
19-Jan-24 Dr. Nidhi Divecha, S. K.P.I.M.C.S. 5
Types of Java Applications
There are mainly 4 types of applications that can be created using Java
programming:
1) Standalone Application
Standalone applications are also known as desktop applications or
window-based applications. These are traditional software that we
need to install on every machine. Examples of standalone
application are Media player, antivirus, etc. AWT and Swing are
used in Java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates a dynamic
page is called a web application.
Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc.
technologies are used for creating web applications in Java.
4) Mobile Application
An application which is created for mobile devices is
called a mobile application. Currently, Android and
Java ME are used for creating mobile applications.
19-Jan-24 Dr. Nidhi Divecha, S. K.P.I.M.C.S. 7
How to run
• JDK – Java Development Kit
– Basic compiler, linker, and libraries
• In Java, the Just In Time Code generator interprers the bytecode into the native
machine code which are at the same programming levels at the same time.
• No explicit pointer
• Java Programs run inside a virtual machine sandbox
• Multi-line Comment
/* a multi-line comment is declared like this
and can have multiple lines as a comment */
• Documentation Comment
/** a documentation comment starts with a delimiter and end with */
1. package student;
2. package employee;
• An import statement is always written after the package statement but it has to be
before any class declaration.
interface stack{
void push(int item);
void pop();
}
• Classes are an essential part of any Java program. It defines the information about
the user-defined classes in a program.
• Every program in Java will have at least one class with the main method.We use
the class keyword to define the class. For Example:
Class Student //Defining a Student class.
{
field;
method;
}
• The execution of all Java programs starts from the main() method, main() method is an entry point
of the class. It must be inside the class.
• Inside the main method, User can create objects and call the methods.
A variable is a container which holds the value while the Java program is
executed. A variable is assigned with a data type.
Types of Variables
in Java:local variable
instance
variable
static
variable
class A
{
int data=50;//instance
variable static int
m=100;//static variable
void method()
{
int n=90;//local variable
}
}
Arithmetic multiplicative * / %
additive + -
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ? :
Java Classes/Objects
Java is an object-oriented programming language.
Everything in Java is associated with classes and objects, along with its attributes and
methods.
Create a Class
To create a class, use the keyword class:
int x = 5;
Create an Object
In Java, an object is created from a class. We have already created the class named Test,
so now we can use this to create objects.
To create an object of Test, specify the class name, followed by the object name, and use
the keyword new
Example
Create an object called "myObj" and print the value of x:
int x = 5;
Multiple Objects
You can create multiple objects of one class:
Example
Create two objects of Test:
int x = 5;
System.out.println(myObj1.x);
System.out.println(myObj2.x);
Method Overloading
If a class has multiple methods having same name but different in parameters, it is known
as Method Overloading.
class Over
return a+b;
}
return a+b+c;
class OverDemo
Java Constructors
A Java constructor is a special method which is used to initialize object. A constructor is
called automatically whenever an object is created.
It is a special method because its name and class name both are same.
Student4(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
s1.display();
s2.display();
}
}
1. Private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
3. Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within
the class, outside the class, within the package and outside the package.
Access Modifier within class within package outside package by subclass only outside package
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.Some of
the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data
types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations. (input streams and
output streams used to read and write data to files )
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: awt stands for Abstract Window Toolkit Contain classes for implementing the
components for graphical user interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.
Inheritance in JAVA
Inheritance is one of the key features of OOP that allows us to create a new class from an
existing class.
It is the mechanism in java by which one class is allow to inherit the features (attributes
and methods) of another class.
The new class that is created is known as subclass (child or derived class) and the
existing class from where the child class is derived is known as superclass (parent or
base class).
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.
In java programming, multiple and hybrid inheritance is supported through interface only. We will
learn about interfaces later.
Types of Inheritance
• On the basis of class, there can be three types of inheritance in
java:
Single Inheritance:
When Derived a new class from single base class is known as Single inheritance.
class A
void display_A()
class B extends A
void display_B()
class SingleDemo
B b1 = new B();
b1.display_A();
b1.display_B();
}
}
Multilevel Inheritance:
When derive a new class from already derived class is known as Multilevel
inheritance.
Hierarchical inheritance
The arguments passed from the console can be received in the java program and it can be used as
an input.
class CommandLineExample{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
class A{
public static void main(String args[]){
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}