Java Programming Practices
Name of the Experiment
1. To Study Basic Features of Java and Installation of Java.
Java Install:-
How to Install Java on Windows, Linux and macOS? :
Java is a versatile programming language widely used for building applications. To start coding
in Java, you first need to install the Java Development Kit (JDK) on your system. This article
provides detailed steps for installing Java on Windows 7, 8, 10, 11, Linux Ubuntu, and macOS.
Download and Install
Java - Windows Operating System
Here, we will discuss how to download and install Java on a Windows 64-bit Machine and set up
the environment to run the first Java program on the command prompt. The process is the same
for all Windows i.e. 7, 8, 10, and 11.
Step 1: Visit the official Oracle website [Link] Click on the
"Download Java"
Step 2: Now you can see the latest version is JDK 25 and there are options for Linux, macOS,
and Windows. Click on the Windows option and then click the x64 Installer option to download
the .exe file for 64-bit Windows OS.
Java Programming Practices
Step 3: Go to Downloads and double-click on that downloaded jdk-25_windows-
x64_bin.exe file. So now Java Installation Wizard get open and then click Next button
Again click on the Next button to install JDK
Java Programming Practices
The installation will begin as shown below
Java Programming Practices
The Java installation is successfully completed as shown below. Now click on "close" to finish.
Java Programming Practices
Step 4: Now we will set the environment variables for Windows OS. Open the C drive, go
to Program Files > Java > jdk-25 (or your installed version) > bin folder. Now, copy the path and
we will use this path when configuring the environment variables
C:\Program Files\Java\jdk-25\bin
To Set Java Path in Windows:-
Step 1: Go to the Search box and type Advanced System settings in it. Now click on the View
Advanced System settings.
Java Programming Practices
In the system variable section, select the path variable and then click the option Edit.
Step 2: Select the Advanced tab and then click environment variables.
Java Programming Practices
Paste the path that copied earlier, and click OK to save the changes.
Step 3: In the system, variables click the New button. Now in the edit System variable, type
variable name as JAVA_HOME and variable path as the path where the JDK folder is saved
and click on OK button Usually the path of the JDK file will be C:\Program
Files\Java\jdk1.8.0_60.
Step 4: Now in the system variables go to the path and click the Edit button
Java Programming Practices
Step 5: Click the New button.
Step 6: Now add the following path: %JAVA_HOME%\bin
Java Programming Practices
Step 5: Verify the Installation. Open command prompt and type the below command to verify
the Java version that is installed in the system.
java –version--
Java Programming Practices
2. To Study basic Object-Oriented Programming concepts and
compare these concepts in Java with C++.
Aim
To study the basic Object-Oriented Programming (OOP) concepts and compare these
concepts in Java with C++.
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design
around objects and classes. Both Java and C++ support OOP concepts, but their implementation
differs in several ways.
Basic OOP Concepts
1. Class
A class is a blueprint for creating objects.
Java Example
class Student {
int id;
String name;
}
C++ Example
class Student {
int id;
string name;
};
2. Object
An object is an instance of a class.
Java
Student s1 = new Student();
Java Programming Practices
C++
Student s1;
3. Encapsulation
Encapsulation means binding data and methods together into a single unit.
Java Example
class Employee {
private int salary;
public void setSalary(int s) {
salary = s;
}
public int getSalary() {
return salary;
}
}
C++ Example
class Employee {
private:
int salary;
public:
void setSalary(int s) {
salary = s;
}
int getSalary() {
return salary;
}
};
4. Inheritance
Inheritance allows one class to acquire properties of another class.
Java Example
class Animal {
void sound() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
Java Programming Practices
}
}
C++ Example
class Animal {
public:
void sound() {
cout << "Animal makes sound";
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks";
}
};
5. Polymorphism
Polymorphism means one method having many forms.
Java Example
class Shape {
void draw() {
[Link]("Drawing Shape");
}
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
}
C++ Example
class Shape {
public:
virtual void draw() {
cout << "Drawing Shape";
}
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle";
}
};
Java Programming Practices
6. Abstraction
Abstraction hides implementation details and shows only essential features.
Java Example
abstract class Vehicle {
abstract void start();
}
C++ Example
class Vehicle {
public:
virtual void start() = 0;
};
Comparison Between Java and C++
Feature Java C++
Platform Dependency Platform Independent Platform Dependent
Compilation Bytecode (JVM) Machine Code
Multiple Inheritance Not supported with classes Supported
Pointer Support No explicit pointers Supports pointers
Automatic Garbage Manual memory
Memory Management
Collection management
Operator Overloading Not supported (except +) Supported
Header Files No Yes
Friend Function Not available Available
Global Variables Not supported Supported
Less secure compared
Security More secure
to Java
Java Programming Practices
Advantages of Java Over C++
Platform independent
Automatic memory management
Better security
Simpler syntax
No pointer complexity
Advantages of C++ Over Java
Faster execution
Supports system-level programming
Multiple inheritance support
Better performance for games and embedded systems
3. Write a program to implement class and object in Java.
public class objectPro {
public static void main(String[] args){
class student{
String name;
int id;
void display(){
[Link]("show the valur of Name: "+ name+" "+"show the value id:"+id);
}
}
student obj = new student();
[Link] = "ram";
[Link] = 105;
[Link]();
}
Java Programming Practices
}
4. Write a program to demonstrate access specifiers and non-access
specifiers in Java.
Access Specifiers
public
private
protected
default
Non-Access Specifiers
final
static
class Test {
public int a = 10;
private int b = 20;
protected int c = 30;
int d = 40; // default
static int count = 1;
final double PI = 3.14;
void show() {
[Link]("Public: " + a);
[Link]("Private: " + b);
[Link]("Protected: " + c);
[Link]("Default: " + d);
[Link]("Static: " + count);
[Link]("Final: " + PI);
}
}
public class AccessDemo {
public static void main(String[] args) {
Test t = new Test();
[Link]();
}
}
Java Programming Practices
5. Write a program to implement constructors including private
constructor and singleton pattern, and demonstrate deep and shallow
copy of objects.
(A) Constructor
class student{
String name;
int id;
student(String x, int i){
name = x;
id = i;
}
void display(){
[Link]("User Name:-"+name +" "+ "User Id:-"+id);
}
}
class Main {
public static void main(String[] args) {
student s1 = new student("ram", 105);
[Link] = "Rahul Sharma";
[Link] = 109;
[Link]();
}
}
Java Programming Practices
(B) Singleton Pattern
class Singleton {
private static Singleton obj = new Singleton();
private Singleton() {
[Link]("Private Constructor");
}
static Singleton getInstance() {
return obj;
}
void display() {
[Link]("Singleton Object");
}
}
public class SingletonDemo {
public static void main(String[] args) {
Singleton s = [Link]();
[Link]();
}
}
(C) Shallow Copy
class Student implements Cloneable {
int id = 101;
public Object clone() throws CloneNotSupportedException {
return [Link]();
}
}
public class ShallowCopyDemo {
public static void main(String[] args) throws Exception {
Student s1 = new Student();
Student s2 = (Student)[Link]();
[Link]([Link]);
[Link]([Link]);
}
}
Java Programming Practices
(D) Deep Copy
class Address {
String city;
Address(String city) {
[Link] = city;
}
}
class Student {
int id;
Address address;
Student(int id, Address address) {
[Link] = id;
[Link] = new Address([Link]);
}
}
public class DeepCopyDemo {
public static void main(String[] args) {
Address a = new Address("Delhi");
Student s1 = new Student(101, a);
Student s2 = new Student([Link], [Link]);
[Link] = "Indore";
[Link]([Link]);
[Link]([Link]);
}
}
Java Programming Practices
(B) Singleton Pattern with Private Constructor
Definition
Singleton Pattern is a design pattern that ensures only one object of a class is created and
provides a global access point to that object.
class Singleton{
private static Singleton obj = new Singleton();
private Singleton(){
[Link]("Private Contructon Call...");
}
static Singleton getInstance(){
return obj;
}
void display(){
[Link]("singleton method");
}
}
public class singletonDemo{
public static void main(String[] args){
Singleton s = [Link]();
[Link]();
}
}
Key Points
1. private Singleton() prevents object creation using new.
2. private static Singleton obj creates a single object.
3. getInstance() returns the same object every time.
4. Singleton is used when only one instance of a class is needed.
Java Programming Practices
(C) Shallow Copy
class Address {
String city;
Address(String city) {
[Link] = city;
}
}
class Student {
int id;
Address addr;
Student(int id, Address addr) {
[Link] = id;
[Link] = addr;
}
}
public class ShallowCopyDemo {
public static void main(String[] args) {
Address a1 = new Address("Delhi");
Student s1 = new Student(101, a1);
Student s2 = s1; // shallow copy
[Link] = "Mumbai";
[Link]([Link]);
}
}
Java Programming Practices
(D) Deep Copy
class Address {
String city;
Address(String city) {
[Link] = city;
}
}
class Student {
int id;
Address addr;
Student(int id, Address addr) {
[Link] = id;
[Link] = new Address([Link]);
}
}
public class DeepCopyDemo {
public static void main(String[] args) {
Address a1 = new Address("Delhi");
Student s1 = new Student(101, a1);
[Link] = "Mumbai";
[Link]([Link]);
}
}
Java Programming Practices
6. Garbage Collection and finalize() Method
Description
Garbage Collector removes unused objects.
finalize() executes before object destruction.
class Test {
protected void finalize() {
[Link]("Finalize Method Called");
}
}
public class GCDemo {
public static void main(String[] args) {
Test t1 = new Test();
t1 = null;
[Link]();
[Link]("Garbage Collection Requested");
}
}
7. Inheritance
class Animal {
void sound() {
[Link]("Animal Sound");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog Bark");
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}
Java Programming Practices
8. Interface (Multiple Inheritance)
interface A {
void show();
}
interface B {
void display();
}
class Test implements A, B {
public void show() {
[Link]("Show");
}
public void display() {
[Link]("Display");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
Test t = new Test();
[Link]();
[Link]();
}
}
Java Programming Practices
9. Polymorphism (Upcasting and Downcasting)
class Animal {
void sound() {
[Link]("Animal Sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog Bark");
}
void run() {
[Link]("Dog Running");
}
}
public class PolymorphismDemo {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
[Link]();
Dog d = (Dog)a; // Downcasting
[Link]();
}
}
10. Exception Handling
public class ExceptionDemo {
public static void main(String[] args) {
try {
int x = 10 / 0;
}
catch(Exception e) {
[Link](e);
}
[Link]("Program Continues");
}
}
Java Programming Practices
11. Collection Framework
import [Link].*;
public class CollectionDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link]("C++");
for(String s : list)
[Link](s);
}
}
12. Thread using Thread Class and Runnable
class MyThread extends Thread {
public void run() {
[Link]("Thread Running");
}
}
public class ThreadDemo {
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
Runnable
class Demo implements Runnable {
public void run() {
[Link]("Runnable Thread");
}
}
public class RunnableDemo
public static void main(String[] args) {
Thread t = new Thread(new Demo());
[Link]();
}
}
Java Programming Practices
13. Synchronization
class Table {
synchronized void printTable(int n) {
for(int i=1;i<=5;i++) {
[Link](n*i);
}
}
}
class MyThread1 extends Thread {
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
[Link](5);
}
}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
[Link](10);
}
}
public class SyncDemo {
public static void main(String args[]) {
Table obj = new Table();
new MyThread1(obj).start();
new MyThread2(obj).start();
}
}
Java Programming Practices
14. Socket Programming (Server)
import [Link].*;
import [Link].*;
public class Server {
public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(5000);
Socket s = [Link]();
DataInputStream dis = new DataInputStream([Link]());
[Link]([Link]());
[Link]();
}
}
Client
import [Link].*;
import [Link].*;
public class Client {
public static void main(String args[]) throws Exception {
Socket s = new Socket("localhost",5000);
DataOutputStream dos = new DataOutputStream([Link]());
[Link]("Hello Server");
[Link]();
}
}
15. RPC Simulation using Socket
The client sends two numbers to the server, and the server returns their sum.
Server
import [Link].*;
import [Link].*;
public class RPCServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5001);
Socket s = [Link]();
DataInputStream in = new DataInputStream([Link]());
DataOutputStream out = new DataOutputStream([Link]());
int a = [Link]();
int b = [Link]();
[Link](a + b);
[Link]();
[Link]();
}
Java Programming Practices
Client
import [Link].*;
import [Link].*;
public class RPCClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost",5001);
DataOutputStream out = new DataOutputStream([Link]());
DataInputStream in = new DataInputStream([Link]());
[Link](10);
[Link](20);
[Link]("Sum = " + [Link]());
[Link]();
}
}
16. Lambda Expression
interface Demo {
void show();
}
public class LambdaDemo {
public static void main(String[] args) {
Demo d = () -> [Link]("Hello Lambda");
[Link]();
}
}
17. AWT
import [Link].*;
public class AWTDemo {
public static void main(String[] args) {
Frame f = new Frame("AWT Example");
Label l = new Label("Welcome");
[Link](100,100,100,30);
[Link](l);
[Link](300,300);
[Link](null);
[Link](true);
}
}
Java Programming Practices
18. Applet (HELLO JAVA)
import [Link];
import [Link];
public class HelloApplet extends Applet {
public void init() {
[Link]("Init");
}
public void start() {
[Link]("Start");
}
public void paint(Graphics g) {
[Link]("HELLO JAVA",100,100);
}
public void stop() {
[Link]("Stop");
}
public void destroy() {
[Link]("Destroy");
}
}
19. Servlet
import [Link].*;
import [Link].*;
import [Link].*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
PrintWriter out = [Link]();
[Link]("Hello Servlet");
}
}
Java Programming Practices
20. JDBC
import [Link].*;
public class JDBCDemo {
public static void main(String args[]) {
try {
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/test,"root",
"password");
Statement st = [Link]();
ResultSet rs = [Link]("select * from student");
while([Link]()) {
[Link]([Link](1)+" "+[Link](2));
}
[Link]();
}
catch(Exception e) {
[Link](e);
}
}
}