Professional Documents
Culture Documents
Java 1 Unit
Java 1 Unit
Background/History of Java
Java was developed by James Gosling and his team at Sun Microsystems in the
early 1990s. It was initially named "Oak" but later renamed "Java." The language's
design was inspired by the C++ programming language but aimed to eliminate
some of its complexities and security flaws.
Advantages of Java
Platform Independence: Java code can run on any device or operating system
with a JVM, making it highly portable.
Object-Oriented: Java's object-oriented approach allows for better code
organization and promotes reusability.
Rich Standard Library: Java comes with a vast standard library that provides
numerous pre-built classes and functions for various tasks.
Garbage Collection: Java's automatic garbage collection feature manages
memory, reducing the burden on developers to handle memory management
manually.
Security: Java's design includes security features that help protect against
malicious code and unauthorized access.
Community and Support: Java has a large and active community, leading to
extensive documentation, libraries, and tools.
These commands should display the installed Java version and Java compiler
version, respectively.
Using an IDE can simplify the development process, providing features like code
auto-completion, syntax highlighting, debugging, and project management.
Java Class:
A Java class is a template or blueprint for creating objects. It encapsulates data
(also known as fields or instance variables) and methods (also known as functions
or behaviors) that operate on that data.
// Constructor
public MyClass(int age, String name) {
this.age = age;
this.name = name;
}
// Methods (behaviors)
public void printDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Java Methods:
Methods in Java are blocks of code that perform specific tasks or actions. They can
take input parameters and return values. Methods are defined within a class and
can be accessed and invoked on objects of that class.
In this example, the "add" method takes two integer parameters, "num1" and "num2,"
and returns their sum as an integer.
// Procedure-Oriented Approach
int calculateSpeed(int distance, int time) {
return distance / time;
}
// Object-Oriented Approach
class Car {
private int distance;
private int time;
Here's a simple example to illustrate the difference between the two approaches:
// Procedure-Oriented Approach
int calculateSum(int a, int b) {
return a + b;
}
// Object-Oriented Approach
class Calculator {
private int result;
void showSides() {
System.out.println("Number of sides: " + sides);
}
}
In this example, the "Shape" class is abstract and has an abstract method "draw()"
that must be implemented by its subclasses. It also has a concrete method
"showSides()" with a default implementation.
Inheritance:
Inheritance is a powerful mechanism in OOP that enables a class (subclass) to
inherit properties and behaviors from another class (superclass). The subclass can
extend or specialize the functionality of the superclass while inheriting its common
features.
In this example, the "Animal" class has a method "makeSound()", and the "Dog"
class extends the "Animal" class. The "Dog" class overrides the "makeSound()"
method with its own implementation.
Encapsulation:
Encapsulation is the concept of restricting direct access to data, providing methods
(getters and setters) to manipulate the data. It helps protect the internal state of an
object from being modified by external entities and ensures data integrity.
Example - Encapsulation:
class Person {
private String name;
private int age;
In this example, the "Person" class has private fields "name" and "age." The access
to these fields is controlled through getter and setter methods, allowing controlled
access and modification of the data.
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}
In this example, the "Vehicle" class is the superclass, and both "Car" and "Bicycle"
classes are its subclasses. Each subclass overrides the "start()" method with its
own implementation.
class Calculator {
int add(int a, int b) {
return a + b;
}
In this example, the "Calculator" class has two "add()" methods, one for integers and
another for doubles. Java will automatically choose the appropriate method based
on the data types of the arguments during method invocation.
Message Communication:
In OOP, objects interact with each other by sending messages. A message is a
request for an object to perform a particular action or method. Objects
communicate and collaborate by invoking methods on each other, leading to the
flow of control between different objects in the program.
class Person {
void greet() {
System.out.println("Hello, I am a person.");
}
}
class Greeting {
void greetPerson(Person person) {
person.greet();
}
}
1. Setting Up Your Computer: Install the Java Development Kit (JDK) and set up
the environment variables.
2. Writing a Program: Create a Java source code file with the correct class and
method structure. Save it with a .java extension.
3. Compiling: Use the Java compiler (javac) to convert the Java source code into
bytecode. Open a terminal or command prompt and run:
javac HelloWorld.java
4. Interpreting and Running the Program: Execute the compiled bytecode using
the Java Virtual Machine (JVM). Run:
java HelloWorld