Basics of Java
Basics of Java
Basics of Java
P.Suresh
1
Java is
Object Oriented
Platform independent
Simple :Java is designed to be easy to learn.
Secure
Architectural- neutral
Portable
Robust
Multi-threaded
Distributed
2
Popular Java Editors
Notepad
Netbeans
http://www.netbeans.org/index.html.
Eclipse http://www.eclipse.org/.
3
Classes
Circle
centre
radius
circumference()
area()
4
Classes
Add fields
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the
circle
6
Adding Methods
7
Adding Methods to Class Circle
public class Circle {
}
}
8
Data Abstraction
Circle aCircle;
Circle bCircle;
9
Class of Circle cont.
aCircle bCircle
null null
11
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
12
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P Q P Q
13
Automatic garbage collection
The object Q
does not have a
reference and cannot be used in future.
14
Accessing Object/Circle Data
15
Executing Methods in Object/Circle
double area;
aCircle.r = 1.0;
area = aCircle.area();
16
Using Circle Class
// Circle.java: Contains both Circle class and its user class
//Add Circle class code here
class MyMain
{
public static void main(String args[])
{
Circle aCircle; // creating reference
aCircle = new Circle(); // creating object
aCircle.x = 10; // assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area(); // invoking method
double circumf = aCircle.circumference();
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
}
}
[raj@mundroo]%: java MyMain
Radius=5.0 Area=78.5
Radius=5.0 Circumference =31.400000000000002
17
Basic Syntax
Case Sensitivity
Class Names - For all class names the first letter should
be in Upper Case.
Example class MyFirstJavaClass
Method Names - All method names should start with a
Lower Case letter. If several words are used, then each
inner word's first letter should be in Upper Case.
Example public void myMethodName()
Program File Name - Name of the program file should
exactly match the class name.
public static void main(String args[]) - java
program processing starts from the main() method which
is a mandatory part of every java program..
18
Java Identifiers
20
Operators
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Conditional operator
21
Conditional and Looping
Statements
if statements
switch statements
while Loop
do...while Loop
for Loop
22
Arrays
dataType arrayRefVar[];
Example:
double[] myList;
double myList[];
Creating Arrays:
arrayRefVar = new dataType[arraySize];
23
Another way
24
Creating a Method
25
Constructors
A constructor with no parameters is referred
to as a default constructor.
Constructors must have the same name as
the class itself.
Constructors do not have a return type—not
even void.
Constructors are invoked using the new
operator when an object is created.
Constructors play the role of initializing
objects.
26
Constructors
Circle(double r) {
radius = r;
}
Circle() {
radius = 1.0;
}
myCircle = new Circle(5.0);
Constructors are a special kind of methods that
are invoked to construct objects.
27