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 32 of 450
Can a final keyword alone be used to define a constant in Java?
A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.Unlike other languages java does not support constants directly. But, you can still create a constant by declaring a variable static and final.Static − Once you declare a variable static they will be loaded into the memory at the compile time i.e. only one copy of them is available.Final − once you declare a variable final you cannot modify its value ...
Read MoreDifference between next() and hasNext() in java collections?
Java provides Iterator and ListIterator classes to retrieve the elements of the collection objects.The hasNext() methodThe hasNext() method of these interfaces returns true if the collection object has the next element else it returns false.Exampleimport java.util.ArrayList; import java.util.Iterator; public class hasNextExample{ public static void main(String[] args){ ArrayList list = new ArrayList(); //Instantiating an ArrayList object list.add("JavaFX"); list.add("Java"); Iterator it = list.iterator(); System.out.println(it.hasNext()); it.next(); System.out.println(it.hasNext()); it.next(); System.out.println(it.hasNext()); } ...
Read MoreHow to remove the redundant elements from an ArrayList object in java?
The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true if you try to add an existing element using this method, the addition operations fails to return false.Therefore, to remove redundant elements of an ArrayList object −Get/create the required ArrayList.Create an empty set object.Try to add all the elements of the ArrayList object to set objectives.Clear the contents of the ArrayList using the clear() method.Now, using the addAll() method add the contents of the set object to the ArrayList again.Exampleimport ...
Read MoreHow to insert an object in an ArrayList at a specific position in java?
The add() method of the ArrayList class helps you to add elements to an array list. It has two variants −add(E e) − This method accepts an object/elements as a parameter and adds the given element at the end of the list.public void add(int index, E element) − This method accepts an element and an integer value representing the position at which we need to insert it and inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Therefore ...
Read MoreDifference between peek(), poll() and remove() method of Queue interface in java?
This represents a collection that is indented to hold data before processing. It is an arrangement of the type First-In-First-Out (FIFO). The first element put in the queue is the first element taken out from it.The peek() methodThe peek() method returns the object at the top of the current queue, without removing it. If the queue is empty this method returns null.Exampleimport java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String args[]) { Queue queue = new LinkedList(); queue.add("Java"); queue.add("JavaFX"); queue.add("OpenCV"); ...
Read MoreIs parent child hierarchy important on throws while overriding in Java?
When you try to handle an exception (checked) thrown by a particular method, you need to catch it using the Exception class or super class of the Exception occurred.In the same way while overriding the method of a super class, if it throws an exception −The method in the sub-class should throw the same exception or its sub type.The method in the sub-class should not throw its super type.You can override it without throwing any exception.When you have three classes named Demo, SuperTest and, Super in (hierarchical) inheritance, if Demo and SuperTest have a method named sample().Exampleclass Demo { ...
Read MoreWhat is meant by re-throwing exceptions in Java?
When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is without adjusting it as −try { int result = (arr[a])/(arr[b]); System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArithmeticException e) { throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing a ...
Read MoreHow to avoid ConcurrentModificationException while iterating a collection in java?
When you are working with collection objects, while one thread is iterating over a particular collection object, if you try to add or remove elements from it, a ConcurrentModificationException will be thrown.Not only that, If you are iterating a collection object, add or remove elements to it and try to iterate its contents again it is considered that you are trying to access the collection object using multiple threads and ConcurrentModificationException is thrown.Exampleimport java.util.ArrayList; import java.util.Iterator; public class OccurenceOfElements { public static void main(String args[]) { ArrayList list = new ArrayList(); //Instantiating ...
Read MoreWhat is the difference between getter/setter methods and constructor in Java?
ConstructorsA constructor in Java is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have the same name as their class and, have no return type.If you do not provide a constructor the compiler defines one on your behalf, which initializes the instance variables with default values.You can also accept parameters through constructors and initialize the instance variables of a class using the given values, these are known as parameterized constructors.ExampleThe following Java program has a class named student ...
Read MoreCan we initialize static variables in a default constructor in Java?
Class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the class, there is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.If you declare a static variable in a class, if you haven’t initialized it, just like with instance variables compiler initializes these with default values ...
Read More