Java Notes Unit 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________

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:

Prof. Bhak+ Chaudhari 1


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
• Multithreading in Java allows the concurrent execution of multiple threads within a
program. Threads are lightweight processes that share the same memory space, enabling
parallel execution of tasks.
• This feature is particularly bene2icial in scenarios where responsiveness and ef2iciency are
crucial, such as in graphical user interfaces (GUIs) or server applications.
• Java's built-in support for multithreading simpli2ies the development of responsive and
scalable softwar

2. What is meant by instance variable and static variable? Describe with


example.
Instance Variable: An instance variable in Java is a variable that belongs to an instance of a class. It is unique
to each object created from that class and, therefore, can have different values for different instances.
Instance variables are declared within a class but outside of any method, constructor, or block.

public class Car {


// Instance variables
String brand;
int year;
double price;

// Constructor
public Car(String brand, int year, double price) {
this.brand = brand;
this.year = year;
this.price = price;
}

// Method to display information


public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Year: " + year);
System.out.println("Price: $" + price);
}

public static void main(String[] args) {


// Creating instances of the Car class
Car car1 = new Car("Toyota", 2022, 25000.50);
Car car2 = new Car("Honda", 2021, 22000.75);

// Accessing and modifying instance variables


2 Prof. Bhak+ Chaudhari
SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
car1.displayInfo();
car2.displayInfo();

// Modifying the price of car1


car1.price = 26000.0;

// Displaying updated information


car1.displayInfo();
}
}

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();

// Displaying the count using a static method


Counter.displayCount();
}
}

Prof. Bhak+ Chaudhari 3


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
In this example, count is a static variable in the Counter class. Each time an object of the class is created, the
count is incremented. The displayCount method is static, allowing it to access the static variable directly
without the need for an instance.

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;
}

// Method to display information


public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String[] args) {
// Creating an instance using the default constructor
Person person = new Person();

// Displaying information
person.displayInfo();
}
4 Prof. Bhak+ Chaudhari
SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
}

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.

public class Student {


// Instance variables
String name;
int age;

// Parameterized constructor
public Student(String studentName, int studentAge) {
name = studentName;
age = studentAge;
}

// Method to display information


public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

public static void main(String[] args) {


// Creating an instance using the parameterized constructor
Student student = new Student("Alice Smith", 20);

// 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

Prof. Bhak+ Chaudhari 5


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
String title;
String author;

// Copy constructor
public Book(Book originalBook) {
title = originalBook.title;
author = originalBook.author;
}

// Method to display information


public void displayInfo() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
}

public static void main(String[] args) {


// Creating an instance
Book originalBook = new Book();
originalBook.title = "Java Programming";
originalBook.author = "John Smith";

// Creating a copy using the copy constructor


Book copiedBook = new Book(originalBook);

// 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.

4. What is method overloading?


Method Overloading in Java:
Method overloading is a feature in Java that allows a class to have multiple methods with the same
name but different parameter lists within the same class. In other words, you can de8ine multiple
methods in a class with the same name, but each method has a unique set of parameters.

6 Prof. Bhak+ Chaudhari


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
Key points of Method Overloading:
• Same Method Name:
• Different Parameter Lists:
• Return Type is Not Considered:
• Enhances Readability and Flexibility:
• Compile-Time Polymorphism:
Example
public class Calculator {
// Method to add two integers
public int add(int num1, int num2) {
return num1 + num2;
}

// Method to add three integers


public int add(int num1, int num2, int num3) {
return num1 + num2 + num3;
}

// Method to add two doubles


public double add(double num1, double num2) {
return num1 + num2;
}

public static void main(String[] args) {


Calculator calculator = new Calculator();

// Calling different overloaded methods


System.out.println("Sum of two integers: " + calculator.add(10, 20));
System.out.println("Sum of three integers: " + calculator.add(10, 20, 30));
System.out.println("Sum of two doubles: " + calculator.add(10.5, 20.5));
}
}

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.

Prof. Bhak+ Chaudhari 7


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________

5. Discuss about access speci-ier in java.


Access speci(iers in Java de(ine the visibility or accessibility of classes, methods, and variables
within a program. They control the level of encapsulation and help manage the scope of members
in a class. Java provides four types of access speci4iers:

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;
}

class Child extends Parent {


void accessProtected() {
protectedVariable = 10; // Accessing protected variable from the subclass
}
}

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:

8 Prof. Bhak+ Chaudhari


SYIT SEM IV

Course Name: Java Programming


____________________________________________________________________________________
class Example {
int defaultVariable; // Default access speci4ier
}

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--------------

Prof. Bhak+ Chaudhari 9

You might also like