Java Notes
Java Notes
Java is technology from Sun Micro Systems developed by James Gosling in 1991 with
initial name ‘Oak’. It was renamed to Java in 1995.
Versions in Java
Requirements
Softwares
Java Development Kit 1.5+
Java Runtime Environment 1.5+
IDE
Kawa
EditPlus
BlueJ
Eclipse
NetBeans (Sun)
Visual Age (IBM)
JDeveloper (Oracle)
Etc.
File Extension : .java
Features of Java
1. Simple
2. Secure
3. Object Oriented Programming
4. Portable
5. Platform Independent
a. Write Once, Run Anywhere
6. Multi-Threaded
7. Robust
a. Java provides big set of classes for almost every process
b. Built-in capabilities of Garbage Collection using Garbage Collector
c. Built-in capabilities of Exception Handling
Resource: http://java.sun.com
Every Java program must have at least one class and every executable Java program must
have an entry point.
JAVAC <programname>
Example
Running a class
JAVA <classname>
Example
Java First
Note: If unable to run the class, add the current folder into the CLASSPATH variable
SET CLASSPATH=%CLASSPATH%;.
14.01.2009
F7 – Compile
F4 – Run
Terminologies in Java
Package
- A collection of related set of classes and interfaces
- java.lang
i. Default Package that provide generally used classes
1. System, String, Math, Integer, Float, Double etc.
- java.io
i. Provides classes related with File Handling and other input/output
operator
1. File, FileReader, FileWriter, InputStreamReader,
BufferedReader etc.
- java.awt
i. For GUI Programming in Java
1. Button, TextField, Checkbox, Menu etc.
- java.net
- java.util
- javax.swing
- javax.servlet
- javax.servlet.jsp
Example
import java.io.BufferedReader;
import java.io.*;
Class
- A set of specification about an entity
- It contains a set of properties and methods
Object
- A real entity created based on class definition
Conditional Statements
1. if
2. switch
3. ternary operator (?:)
Looping Statements
- while
- do-while
- for loops
i. General for loop
ii. For-Each loop used with Arrays and collections
Example
//Using For-Each loop
class ForEachTest
{
public static void main(String args[])
{
int num[]=
{6,8,9,2,4,5
};
for(int i : num)
System.out.printf("%d\n",i);
}
}
Next Topics
Arrays
Reading Data from Keyboard
Wrapper Classes
Getting using Command Line
16.01.2009
Example
- Get name and age of a person check for validity of voter
Conversion Formula
Datatype variable=WrapperClass.parseDatatype(stringdata);
Example
int age=Integer.parseInt(br.readLine());
double basic=Double.parseDouble(br.readLine());
args[0] Rakesh
args[1] 45
19.01.2009
Arrays
Types of Arrays
1. Single Dimensional
a. int n[]=new int[5];
2. Two dimensional
a. int [][]n=new int[2][3];
Example
//Using Arrays
class ArrayTest
{
public static void main(String args[])
{
int num[]=new int[5];
num[0]=44;
num[1]=333;
num[2]=444;
num[3]=555;
num[4]=2222;
for(int i=0;i<num.length;i++)
System.out.println(num[i]);
}
}
Static Blocks: special blocks that executes the code before main() and used to initialize
the static fields of a class
Example
class StaticTest
{
static
{
System.out.println("Hello");
}
public static void main(String args[])
{
System.out.println("Hi");
}
static
{
System.out.println("abc");
}
Types of Methods
1. General Methods
2. Abstract Methods
a. A method which has the signature but no body contents
b. Body is provided by the child class through overriding
c. Such methods can be declared in two places
i. Class
ii. Interfaces
d. If declared inside the class, use abstract keyword
e. If declared inside the interface, no keyword is required
3. Final Methods
Special method that can never be overridden
a. Use final keyword with such methods
Types of classes
1. General classes
2. Abstract class
a. Special class that can never be instantiated
b. They are always used for inheritance purpose only
c. An abstract class may or may not have any abstract method
d. If a class contains any abstract method the it must be declared as abstract
e. Use abstract keyword with such classes
3. Final class
a. A class that can never be inherited
b. Use final keyword with such classes
Assignment
Email: [email protected]
Q1: Create a class to manage information about the bank customers with the following
information
1. Account Number
2. Name
3. Current Balance
21.01.2009
class <classname>
{
//members
}
Abstraction is defined for controlling accessibility of class members and class itself. Java
provides four kinds of abstraction layers
Private – within the class
Public – anywhere access
Protected – in same or child class
Package or Default – within the package or folder
Introduction to Packages
- A package is a special folder that contains the classes for specific purpose
- Every class that get placed in a package must have package command on
top of it
i. package <packagename>;
- Java provides a big set of packages
i. java.lang (default)
ii. java.io
iii. java.util
iv. java.awt
v. java.net
vi. java.applet
vii. java.awt.event
viii. javax.swing
ix. javax.servlet
x. javax.servlet.jsp
xi. javax.servlet.jsp.tagext
23.01.2009
Polymorphism
- A concept that allows to do multiple operations by one item. It get
implemented by overloading.
- Java allows only method overloading
- Multiple methods with same name but different number of arguments or
the type of arguments
Inheritance
- Provides re-usability of code by reducing the code size hence reducing the
development time, cost as well as standardization of methods
- The class are categorized at three levels
i. Parents
ii. Super Class
iii. Current class
- Every Java class is child of java.lang.Object class
- Use extends keyword to inherit a class into other class
- Use JAVAP tool to view members of a class
- Use this keyword for current class objects and super keyword for super
class objects
-