0% found this document useful (0 votes)
14 views

Computer Constructors

Class 10th icse computer application chapter constructors

Uploaded by

mithunraj7437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Computer Constructors

Class 10th icse computer application chapter constructors

Uploaded by

mithunraj7437
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

S.J.B.S.

S PUBLIC SCHOOL
2nd Block, Rajajinagar,Bangalore-560010

An Internal Assessment based on computer applications

In the partial fulfillment of the requirement for The Indian Certificate Of


Secondary Examination.

Topic: constructors in java

Under the guidance of


Mrs. Roopashree Mam
Submitted by : - Abhay.S.J
Class: - 10th standard
Roll no: - 01
Contents
1. Call by Value
2. Call by Reference
3. The 'this' Keyword
4. Function Overloading
5. Constructors
6. Types of Constructors
7. Constructor Overloading
8. Destructors
9. Copy Constructor
1. Call by Value
In Java, "call by value" means that a copy of the
actual value is passed to the method. This is
significant because when you pass a primitive
data type (like int, char, etc.) into a method, any
modifications made to the parameter inside the
method do not affect the original variable
outside of it.
java
Copy code
public class Test {
void modify(int num) {
num += 10; // Modifying the copy of 'num'
}
public static void main(String[] args) {
int a = 20;
Test obj = new Test();
obj.modify(a);
System.out.println(a); // Output will still be 20,
not 30
}
}
In this example, when a is passed to the modify()
method, a copy of a is sent. Thus, the value of a
in the main method remains unchanged even
though it is modified within modify().
Advantages of Call by Value:
Prevents unintended side-effects on the original
data.
Easier to debug and reason about since the
original data remains unchanged.
Safe when dealing with small data types such as
int, char, etc.
Disadvantages of Call by Value:
If the data type is large (e.g., objects like arrays
or classes), copying it can take time and memory.
Cannot directly modify the original value.
2. Call by Reference
In contrast to primitive types, objects and arrays
in Java are passed by reference. This means that
the method receives the memory address of the
original object, and any changes made to the
object inside the method will reflect outside the
method as well.
java
Copy code
public class Test {
void modifyArray(int[] arr) {
arr[0] = 50; // Modify the first element of the
array
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4};
Test obj = new Test();
obj.modifyArray(numbers);
System.out.println(numbers[0]); // Output will
be 50
}
}
In this example, the array numbers is passed by
reference. Since arrays are objects in Java, the
reference (address) is passed, and changes made
inside modifyArray() affect the original array.
Advantages of Call by Reference:
More efficient for large data structures as no
copying is involved.
Enables modifications to objects directly within
the method, which can be useful when you want
a method to change the state of an object.
Disadvantages of Call by Reference:
Unintentional changes can be made to the
original object, leading to bugs.
Requires caution when working with methods
that modify objects, especially in multi-threaded
environments.
3. The 'this' Keyword
The this keyword in Java refers to the current
object — the object that is invoking the method.
It helps in differentiating between class-level
variables and method parameters or local
variables that have the same name.
The this keyword is used for:
Referring to the current instance of the class.
Calling other constructors in constructor chaining
(this()).
Returning the current class instance.
Passing the current instance as an argument to
methods.
java
Copy code
class Box {
int length;
Box(int length) {
this.length = length; // 'this' distinguishes
between class variable and parameter
}
void display() {
System.out.println("Length: " + this.length); //
Refers to the instance variable
}
public static void main(String[] args) {
Box box = new Box(5);
box.display(); // Outputs: Length: 5
}
}
In this example, this.length refers to the instance
variable length, while the length without this
refers to the parameter passed to the
constructor.
Advantages of 'this':
Eliminates ambiguity between instance variables
and parameters with the same name.
Enables constructor chaining, which helps reduce
code duplication.
4. Function Overloading
Function overloading means having multiple
methods with the same name but different
parameter lists. This allows the same method to
perform different tasks depending on the
arguments passed.
Java determines which method to call based on
the number, types, and order of the parameters.
Overloading helps in creating more intuitive and
readable code, allowing a single method name
to be reused for multiple purposes.
java
Copy code
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
}
In this example, the method add() is overloaded
three times, each with different parameters: two
integers, three integers, and two doubles.
Advantages of Function Overloading:
Makes code easier to understand and manage.
Increases flexibility by allowing methods to be
reused for different types of data.
Improves readability and maintainability of
code.
5. Constructors
A constructor is a special type of method in a
class that gets invoked when an object of the
class is instantiated. Constructors are used to
initialize objects, setting up their initial state.
There are two main types of constructors:
Default Constructor: This constructor takes no
arguments and initializes object fields to default
values.
Parameterized Constructor: This constructor
takes parameters and allows for initializing the
object with specific values.
java
Copy code
class Person {
String name;
// Default constructor
Person() {
name = "John Doe";
}
// Parameterized constructor
Person(String name) {
this.name = name;
}
void display() {
System.out.println("Name: " + name);
}
}
In this example, the class Person has two
constructors: a default constructor and a
parameterized constructor.
Advantages of Constructors:
Automatically called during object creation,
ensuring the object is properly initialized.
Enables setting initial values for object attributes
at the time of object creation.
6. Types of Constructors
There are several types of constructors in Java:
Default Constructor: A constructor with no
parameters.
Parameterized Constructor: A constructor that
accepts parameters to set initial values.
Copy Constructor: Used to create a copy of an
existing object (although Java does not have
built-in copy constructors like C++).
java
Copy code
class Example {
int x;
// Default constructor
Example() {
x = 0;
}
// Parameterized constructor
Example(int a) {
x = a;
}
// Copy constructor
Example(Example obj) {
x = obj.x;
}
}
7. Constructor Overloading
Constructor overloading allows a class to have
multiple constructors, each with different
numbers or types of parameters. This provides
flexibility for object creation.
Constructor overloading improves code
readability and flexibility, allowing developers to
initialize objects in different ways.
java
Copy code
class Rectangle {
int width, height;
// Default constructor
Rectangle() {
width = 0;
height = 0;
}
// Parameterized constructor
Rectangle(int w, int h) {
width = w;
height = h;
}
void display() {
System.out.println("Width: " + width + ", Height:
" + height);
}
}
8. Destructors
Java does not have explicit destructors like C++.
Instead, Java relies on the garbage collector to
automatically manage memory. The finalize()
method is called before an object is destroyed by
the garbage collector, though it's rarely used and
discouraged in modern Java programming.
9. Copy Constructor
A copy constructor is used to create a new object
as a copy of an existing object. In Java, a custom
copy constructor needs to be defined since Java
does not provide one automatically.

You might also like