Object Oriented Programming Articles

Page 195 of 589

What is Pass By Reference and Pass By Value in PHP?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 7K+ Views

In PHP, you can pass arguments to functions using two methods: pass by value (default) and pass by reference. Understanding the difference is crucial for effective programming. By default, PHP uses pass by value, meaning the function receives a copy of the variable. Changes inside the function don't affect the original variable. However, pass by reference allows the function to modify the original variable by using the ampersand (&) symbol. Pass By Reference To pass a variable by reference, prepend an ampersand (&) to the parameter name in the function definition. This allows the function to modify ...

Read More

What is method overloading in PHP?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 11K+ Views

Method overloading is a concept in Object Oriented Programming that allows creating multiple methods with the same name that behave differently based on the number or type of parameters they accept. While traditional method overloading (static polymorphism) isn't directly supported in PHP, we can achieve similar functionality using PHP's magic methods. Traditional Method Overloading Issue Unlike other programming languages, PHP doesn't support traditional method overloading. The following example demonstrates this limitation ? Fatal error: Cannot redeclare machine::doTask() This error occurs because PHP treats both methods as duplicate declarations, regardless of ...

Read More

How do you convert an ArrayList to an array in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 769 Views

An ArrayList provides two toArray() methods to convert it into an array − one returns an Object[] array, and the other returns a typed array. Method 1: toArray() (Returns Object[]) Object[] toArray() Returns an array containing all elements in proper sequence. The returned array type is Object[], so casting is needed to use specific types. Method 2: toArray(T[]) (Returns Typed Array) T[] toArray(T[] a) Returns a typed array containing all elements. Pass an empty array of the desired type and Java allocates the correct size. This is the preferred ...

Read More

How do I add an element to an array list in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 958 Views

We can add elements to an ArrayList easily using its add() method. The method appends the specified element to the end of the list, or inserts it at a specific index. Syntax boolean add(E e) void add(int index, E element) The first form appends the element to the end. The second form inserts the element at the specified index, shifting existing elements to the right. Example The following example shows how to add elements to an ArrayList using both forms of the add() method ? import java.util.ArrayList; import java.util.List; public ...

Read More

Difference between ArrayBlockingQueue and LinkedBlockingQueue

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 585 Views

ArrayBlockingQueue and LinkedBlockingQueue both implement the BlockingQueue interface from the java.util.concurrent package. Both store elements in FIFO order, are thread-safe, and do not accept null elements. They differ in their internal data structure, capacity behavior, and locking mechanism. ArrayBlockingQueue ArrayBlockingQueue is backed by a fixed-size array. Once created, the capacity cannot be changed. It uses a single lock with two conditions (notEmpty and notFull) for both put and take operations, meaning producers and consumers cannot operate concurrently. LinkedBlockingQueue LinkedBlockingQueue is backed by linked nodes. It is optionally bounded − if no capacity is specified, it defaults ...

Read More

How do you create a list in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 8K+ Views

A Java List can be created in multiple ways depending on whether you need a modifiable list, a fixed-size list, or want to initialize it with values in a single statement. Way 1: Raw Type (Not Recommended) Create a List without specifying the type of elements. The compiler will show an unchecked warning − List list = new ArrayList(); Way 2: Generic Type (Recommended) Create a List with a specified element type for type safety − List list = new ArrayList(); Way 3: Initialize in One Line Create ...

Read More

How do you add two lists in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 1K+ Views

The addAll() method of the List interface can be used to combine two lists in Java. It comes in two variants − one appends elements at the end, and another inserts elements at a specific index. addAll() Without Index Appends all elements from the specified collection to the end of the list − boolean addAll(Collection

Read More

How do I search a list in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 389 Views

Java Streams (Java 8+) can be used to search for an item within a list by filtering elements based on a condition. The filter() method applies the search criteria, and findAny() returns the first matching element or null if no match is found. Syntax Student result = list.stream() .filter(s -> s.getRollNo() == rollNo) .findAny() .orElse(null); This filters the list for a student with the matching roll number. findAny() returns an Optional, and orElse(null) returns null if no match is found. Example ...

Read More

How can we convert list to Set in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 30K+ Views

A Java List can be converted to a Set to eliminate duplicate entries. The resulting Set will contain only unique values. There are three common ways to perform this conversion − Method 1: Using Set Constructor Pass the list directly to the HashSet constructor − Set set = new HashSet(list); Method 2: Using addAll() Create an empty set and use addAll() to add all elements from the list − Set set = new HashSet(); set.addAll(list); Method 3: Using Streams (Java 8+) Use the Stream API to collect list ...

Read More

How to copy a list to another list in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 28K+ Views

A List of elements can be copied to another List in Java using multiple approaches. All methods create a shallow copy − the new list contains references to the same objects as the original. Way 1: Constructor Pass the source list to the ArrayList constructor − List copy = new ArrayList(list); Way 2: addAll() Create an empty list and use addAll() to add all elements from the source − List copy = new ArrayList(); copy.addAll(list); Way 3: Collections.copy() Use Collections.copy() to copy elements into an existing destination list. ...

Read More
Showing 1941–1950 of 5,881 articles
« Prev 1 193 194 195 196 197 589 Next »
Advertisements