Java Notes Unit 1
Java Notes Unit 1
UNIT 1
1. List out features of Java and explain any four features.
key features of Java:
• Platform Independence
• Object-Oriented
• Simple and Readable Syntax
• Multithreading
• Security
• Distributed Computing (Networking)
• Dynamic and Extensible
• High Performance
• Rich Standard Library (Java API)
• Automatic Memory Management (Garbage Collection)
1. Platform Independence:
• Java achieves platform independence through the "Write Once, Run Anywhere" (WORA)
principle. When you compile a Java program, it is translated into bytecode, which is a
platform-neutral intermediate code.
• This bytecode can be executed on any device that has a compatible JVM, irrespective of the
underlying hardware or operating system.
• This feature simpli/ies the deployment of Java applications across diverse environments.
2. Object-Oriented:
• Java's object-oriented nature facilitates modular and organized code development.
• The use of classes and objects encourages the encapsulation of data and behavior, leading
to more maintainable and scalable software.
• Inheritance allows the creation of hierarchies, promoting code reuse, while polymorphism
enables the development of 0lexible and extensible applications.
3. Simple and Readable:
• Java's syntax is intentionally designed to be simple and readable.
• It adopts a clear and straightforward syntax, which reduces the chances of coding errors
and makes it easier for developers to understand and maintain code.
• Java avoids the complexities of languages like C++ (e.g., explicit pointers and operator
overloading), contributing to a more accessible language for a broader developer audience.
4. Multithreading:
// Constructor
public Car(String brand, int year, double price) {
this.brand = brand;
this.year = year;
this.price = price;
}
In this example, brand, year, and price are instance variables of the Car class. Each instance of the Car class
(e.g., car1 and car2) has its own set of these variables.
Static Variable: A static variable in Java is a variable that belongs to the class rather than a speci6ic instance
of the class. It is shared among all instances of the class, and there is only one copy of the static variable
regardless of how many objects are created. Static variables are declared using the static keyword.
public class Counter {
// Static variable
static int count = 0;
// Constructor
public Counter() {
count++;
}
// Method to display the count
public static void displayCount() {
System.out.println("Count: " + count);
}
public static void main(String[] args) {
// Creating instances of the Counter class
Counter obj1 = new Counter();
Counter obj2 = new Counter();
3. Explain about the Constructors and its type with example program.
Constructors in Java:
In Java, a constructor is a special method used to initialize objects of a class. It has the same name
as the class and is called when an object is created. Constructors are primarily used to set initial
values for the instance variables of an object.
Types of Constructors:
1. Default Constructor:
A default constructor is automatically provided by Java if no constructor is explicitly de6ined in a
class. It initializes the object with default values.
public class Person {
// Instance variables
String name;
int age;
// Default constructor
public Person() {
name = "John Doe";
age = 30;
}
// Displaying information
person.displayInfo();
}
4 Prof. Bhak+ Chaudhari
SYIT SEM IV
2. Parameterized Constructor:
A parameterized constructor is a constructor with parameters. It allows you to pass values during
object creation to initialize instance variables with speci3ic values.
// Parameterized constructor
public Student(String studentName, int studentAge) {
name = studentName;
age = studentAge;
}
// Displaying information
student.displayInfo();
}
}
3. Copy Constructor:
A copy constructor is used to create a new object as a copy of an existing object. It takes an object
of the same class as a parameter and initializes the new object with the values of the existing
object.
public class Book {
// Instance variables
// Copy constructor
public Book(Book originalBook) {
title = originalBook.title;
author = originalBook.author;
}
// Displaying information
originalBook.displayInfo();
copiedBook.displayInfo();
}
}
These are the main types of constructors in Java. Constructors play a crucial role in
initializing objects and ensuring that they start with valid and meaningful values.
In this example, the Calculator class has three overloaded add methods. The +irst method adds two
integers, the second adds three integers, and the third adds two doubles. The compiler determines
which method to call based on the number and types of arguments passed during the method
invocation.
1. Public:
Members declared as public are accessible from any other class. There are no restrictions on
accessing public members. This is the least restrictive access level.
Example:
public class Example {
public int publicVariable;
public void publicMethod() {
// Code here
}
}
2. Protected:
Members declared as protected are accessible within the same package or by subclasses. They are
not accessible outside the package unless there is an inheritance relationship.
Example:
class Parent {
protected int protectedVariable;
}
3. Default (Package-Private):
If no access speci,ier is speci,ied (default), the member is accessible only within the same package.
It is also known as package-private.
Example:
4. Private:
Members declared as private are accessible only within the same class. They are not accessible
outside the class, even by subclasses.
Example:
public class Example {
private int privateVariable;
private void privateMethod() {
// Code here
}
}
-----------x--------------