2. Basic Introduction to JVM
• Java Virtual Machine is a set of software & program
components. As name suggests, it is a virtual computer
that resides within real computer.
• Java can achieve platform independent feature due to
JVM.
• When we want to write java program, we write the
source code into notepad & store it in the file having
extension .java
Prof. Aadil Ansari
3. Continue..
• The .java file is compiled using javac compiler. On
compilation a .class file gets generated. This class file is
actually a byte code.
• JVM takes byte code as an input, reads it, interprets it &
then execute it.
• JVM can generate output corresponding to the
underlying operating system.
Prof. Aadil Ansari
4. JVM act as an Mediator between OS & Java
Text Editor
Java Source
Code
javac
Byte code(.class File)
Windows Mac Linux
Run using java
Compile using javac
Java Virtual
Machine
Prof. Aadil Ansari
9. • System.out.print(“Hello”);
• System.out.print(“n”);
• System.out.print(“Java!”);
print( )method //print and wait
println( )method //print a line and move to the next line
• System.out.println(“Hello”);
• System.out.println(“Java!”);
Prof. Aadil Ansari
10. Classes
• A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
• The basic syntax for a class definition:
• For Example:-
class Rectangle {
// my Rectangle class
}
class classname [extends SuperClassName]
{
[fields declaration]
[methods declaration]
}
Prof. Aadil Ansari
11. Adding Variables: Class Rectangle with fields
• Add variables
• The fields (data) are also called the instance variables.
class Rectangle
{
int length; // Data fields OR instance Variables
float width;
}
Prof. Aadil Ansari
12. Adding Methods
• Methods are declared inside the body of the
class but immediately after the declaration of
instance variable.
• The general form of a method declaration is:
type methodname (parameter-list)
{
Method-body;
}
Prof. Aadil Ansari
13. Adding Methods to Class Rectangle
class Rectangle
{
int length;
float width;
void getData(int x, float y) //Method declaration
{
length =x;
width= y;
}
}
Method Body
Prof. Aadil Ansari
14. Constructor
• Constructor is a special method with same name
as it’s class name
• No return type for constructor. Not even void
• Constructors are implicitly called when objects
are created
• A constructor without input parameter is default
constructor
• Constructor can be overloaded
Prof. Aadil Ansari
15. class Student4
• {
int id;
String name;
Student4(int i,String n) //constructor
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}Prof. Aadil Ansari
17. Creating objects of a class
• Objects in java are created using new operator.
• The new operator creates an object of specified class &
returns a reference of that object.
• For Example:- Rectangle rect1;
rect1 = new Rectangle();
Prof. Aadil Ansari
18. Continue..
Both the statement can be combined into one as shown
below.
Rectangle rect1 = new Rectangle();
The method Rectangle() is default constructor of class.
We can create any number of objects of Rectangle class.
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
Prof. Aadil Ansari
20. Exception handling & Error
• Signal occurs when some unusual condition occurs.
• Various types of signals are Exceptions, Errors,
Interrupts & Controls.
• Exception:- Appropriate execution is raised when some
unusual condition occurs.
• Errors:- Compile time error.
Prof. Aadil Ansari
21. Continue
• Exception Handling:-The statements that are
likely to cause an exception are enclosed within
a try block.
• There is another block defined by keyword
catch which responsible for handling the
exception thrown by the try block.
• The catch is added immediately after the try
block.
Prof. Aadil Ansari
22. General syntax of try & catch
try
{
// exception get generated here.
}
catch(Type_of_Exception e)
{
// exception is handled here
}
Prof. Aadil Ansari
23. For Example:-
class RunErrDemo
{
public static void main(String arg[])
{
int a,b,c;
a= 10, b=0;
try
{
c = a/b; //Exception occurs because the element is
divided by 0
}
Prof. Aadil Ansari