JAVA PROGRAMMING – COMPLETE STUDENT
NOTES (CS22409)
These notes are prepared for students studying Java Programming: Theory and Practices. The
purpose of these notes is to provide clear explanations, examples and programs for important
concepts required for university examinations and practical understanding.
UNIT I – INTRODUCTION TO OOP AND JAVA
Object Oriented Programming (OOP) is a programming paradigm where programs are organized
using objects and classes.
OOP makes programs easier to design, reuse and maintain.
The four major principles of OOP are Abstraction, Encapsulation, Inheritance and Polymorphism.
OOP Concepts
Abstraction hides internal implementation details and shows only necessary features.
Encapsulation combines data and methods into one unit called a class.
Inheritance allows one class to acquire properties of another class.
Polymorphism allows methods to perform different tasks depending on input.
Example Program: Simple Class and Object
This program demonstrates creation of a class and object.
class Student{
int roll;
String name;
void display(){
[Link](roll + " " + name);
}
public static void main(String args[]){
Student s = new Student();
[Link] = 101;
[Link] = "Arun";
[Link]();
}
}
Java Features
Simple – Easy to learn compared to other programming languages.
Object Oriented – Uses OOP principles.
Platform Independent – Write once run anywhere.
Secure – Java programs run inside JVM.
Multithreaded – Supports multiple tasks simultaneously.
Structure of Java Program
Every Java program contains at least one class and one main method.
class Hello{
public static void main(String args[]){
[Link]("Hello Java");
}
}
Primitive Data Types
Type Example Description
int 10 Integer numbers
float 10.5 Decimal numbers
double 20.55 Large decimal values
char 'A' Single character
boolean true/false Logical values
Operators
Arithmetic Operators: + - * / %
Relational Operators: < > <= >= == !=
Logical Operators: && || !
Assignment Operators: = += -= *= /=
Control Statements Example
Example program to check whether number is even or odd.
import [Link];
class EvenOdd{
public static void main(String args[]){
Scanner sc = new Scanner([Link]);
int n = [Link]();
if(n % 2 == 0)
[Link]("Even");
else
[Link]("Odd");
}
}
Arrays
Array is a collection of elements of same data type.
Each element in array is accessed using index value.
class ArrayDemo{
public static void main(String args[]){
int a[] = {10,20,30,40,50};
for(int i=0;i<[Link];i++){
[Link](a[i]);
}
}
}
UNIT II – CLASSES, PACKAGES AND INHERITANCE
A class is a blueprint used to create objects.
Objects contain data members and member functions.
Constructor Example
Constructor initializes object when it is created.
class Test{
Test(){
[Link]("Constructor called");
}
public static void main(String args[]){
Test t = new Test();
}
}
Inheritance Example
Child class inherits properties of parent class.
class Animal{
void eat(){
[Link]("Eating");
}
}
class Dog extends Animal{
void bark(){
[Link]("Barking");
}
public static void main(String args[]){
Dog d = new Dog();
[Link]();
[Link]();
}
}
Abstract Class Example
Abstract class may contain abstract methods.
abstract class Shape{
abstract void draw();
}
class Circle extends Shape{
void draw(){
[Link]("Drawing Circle");
}
}
UNIT III – INTERFACES
Interface contains abstract methods.
A class implements an interface and provides implementation.
interface Printable{
void print();
}
class Printer implements Printable{
public void print(){
[Link]("Printing document");
}
}
String Manipulation
String represents sequence of characters.
class StringDemo{
public static void main(String args[]){
String s = "Java Programming";
[Link]([Link]());
[Link]([Link]());
[Link]([Link](0,4));
}
}
Exception Handling
Exception handling prevents program from crashing.
class ExceptionDemo{
public static void main(String args[]){
try{
int a = 10/0;
}
catch(Exception e){
[Link]("Error occurred");
}
}
}
UNIT IV – FILE STREAMS
Streams represent flow of data.
Java supports reading and writing files.
import [Link].*;
class FileWrite{
public static void main(String args[]) throws Exception{
FileOutputStream f = new FileOutputStream("[Link]");
String s = "Hello Students";
byte b[] = [Link]();
[Link](b);
[Link]();
}
}
Multithreading Example
Thread allows multiple tasks to run simultaneously.
class MyThread extends Thread{
public void run(){
for(int i=1;i<=5;i++){
[Link](i);
}
}
public static void main(String args[]){
MyThread t = new MyThread();
[Link]();
}
}
UNIT V – APPLET
Applet is a Java program that runs inside browser or applet viewer.
Applet has a specific life cycle.
import [Link];
import [Link];
public class HelloApplet extends Applet{
public void paint(Graphics g){
[Link]("Hello Applet",50,50);
}
}
JavaFX
JavaFX is used to build graphical user interfaces.
It provides controls such as Button, Label, TextField and layouts.
Important Examination Questions
Explain OOP concepts.
Explain inheritance with example.
What is interface?
Explain exception handling.
Explain multithreading.
Explain Java streams.
Explain Applet life cycle.