Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 2 of 450
How Much Java is Better than C?
Java and C are two popular programming languages with different features, syntax, and applications. Java was first introduced by Sun Microsystems in 1995 and operates on the Java Virtual Machine (JVM). C is a procedural programming language developed by Dennis Ritchie at Bell Labs in 1972. Both Java and C have their pros and cons, but here we will explore how Java is better than C in various aspects. Memory Management One of the notable distinctions between Java and C is in memory management. C uses manual memory management, which requires the programmer to allocate and deallocate memory ...
Read MoreFloating Point Operations and Associativity in C, C++ and Java
In C, mathematical operations with floating point numbers do not always follow the associativity rule. This means that (a + b) + c may not equal a + (b + c) due to precision limitations and rounding errors in floating-point arithmetic. Syntax float result1 = a + (b + c); /* Right associativity */ float result2 = (a + b) + c; /* Left associativity */ Example: Floating Point Associativity Issue Here's an example demonstrating how floating point operations violate associativity − #include int ...
Read MoreHow do you convert an ArrayList to an array in Java?
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 MoreHow do I add an element to an array list in Java?
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 MoreDifference between ArrayBlockingQueue and LinkedBlockingQueue
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 MoreHow do you create a list in Java?
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 MoreHow do you add two lists in Java?
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 MoreHow do I search a list in Java?
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 MoreHow can we convert list to Set in Java?
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 MoreHow to copy a list to another list in Java?
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