Found 2620 Articles for Java

Is parent child hierarchy important on throws while overriding in Java?

Maruthi Krishna
Updated on 14-Oct-2019 10:46:54

190 Views

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().Example Live Democlass Demo { ... Read More

Can we override only one method while implementing Java interface?

Maruthi Krishna
Updated on 14-Oct-2019 10:12:28

847 Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.Example Live Demointerface Sample {    void demoMethod1();    void demoMethod2();    void demoMethod3(); } public class InterfaceExample implements Sample {    public void demoMethod1() {       System.out.println("This is demo method-1");    } ... Read More

Is it possible to override toString method in an array using Java?

Maruthi Krishna
Updated on 14-Oct-2019 09:14:21

1K+ Views

You can override the toString() method of the Object class but, if you are creating an object array of a particular class and want to print the contents of this array by overriding the toString() method instead of the, you cannot do that there is no solution for that in Java as of now.But you can achieve this using various other methods −Using the toString() method of the Arrays classThe toString() method of the Arrays class accepts a String array (in fact any array) and returns it as a String. Pass your String array to this method as a parameter. ... Read More

How to Sort a String in Java alphabetically in Java?

Maruthi Krishna
Updated on 02-Sep-2023 10:27:00

72K+ Views

Using the toCharArray() methodThe toCharArray() method of this class converts the String to a character array and returns it. To sort a string value alphabetically −Get the required string.Convert the given string to a character array using the toCharArray() method.Sort the obtained array using the sort() method of the Arrays class.Convert the sorted array to String by passing it to the constructor of the String array.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class SortingString {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string value: ");       ... Read More

In how many ways you can retrieve the elements of a collection in Java?

Maruthi Krishna
Updated on 14-Oct-2019 09:03:48

2K+ Views

You can retrieve the contents of a collection object in three ways −Using for each loopThe foreach loop or enhanced for loop, which enables you to traverse the complete collection object sequentially.Example Live Demoimport java.util.ArrayList; public class RetrievingData {    public static void main(String[] args) {       ArrayList list = new ArrayList();       //Instantiating an ArrayList object       list.add("JavaFX");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       list.add("OpenNLP");       list.add("JOGL");       list.add("Hadoop");       list.add("HBase");       list.add("Flume");       ... Read More

How many ways are there to convert an Array to ArrayList in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:59:09

157 Views

By adding each element of the arrayThe add() method of the ArrayList class accepts an element and adds it to the current array list. To convert an array to array list using this method −Get the string array.Create an empty ArrayList object.Add each element of the array to the ArrayList.Print the contents of the array list.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class ArrayToArrayList {    public static void main(String args[]) {       String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"};       ArrayList arrayList = new ArrayList();       for(int ... Read More

Remove all non-alphabetical characters of a String in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:54:42

2K+ Views

The split() method of the String class accepts a String value representing the delimiter and splits into array of tokens (words), treating the string between the occurrence of two delimiters as one token.For example if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.If the String does not contain the specified delimiter this method returns an array containing the whole string as element.The regular expression “\W+” matches all ... Read More

How to convert LinkedList to Array in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:51:15

5K+ Views

The toArray() method of the LinkedList class converts the current Linked List object into an array of object type and returns it. This array contains all of the elements in this list in proper sequence (from first to last element). This acts as bridge between array-based and collection-based APIs.Therefore, to convert a LinkedList to an array −Instantiate the LinkedList class.Populate it using the add() method.Invoke the toArray() method on the above created linked list and retrieve the object array.Convert each and every element of the object array to string.Example Live Demoimport java.util.Arrays; import java.util.LinkedList; public class LinkedListToArray {    public static ... Read More

How to remove an element from ArrayList or, LinkedList in Java?

Maruthi Krishna
Updated on 17-Feb-2020 07:04:08

594 Views

The ArrayList and, LinkedList classes implements the List interface of the java.util package. This interface provided two variants of the remove() method to remove particular elements as shown below −E remove(int index)boolean remove(Object o) −Using one of these methods you can delete a desired element from the List or, linkedList in Java.E remove(int index) − This method accepts an integer representing a particular position in the List object and, removes the element at the given position. If the remove operation is successful, this method returns the element that has been removed.If the index value passed to this method is less ... Read More

How to make an ArrayList read only in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:37:04

387 Views

The unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.ExampleFollowing Java program creates an ArrayList object, adds elements to it, converts it into a read-only List object. Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListReadOnly {    public static void main(String[] args) {       //Instantiating an ArrayList object       ArrayList list = new ArrayList();       list.add("JavaFx");       list.add("Java");       list.add("WebGL");   ... Read More

Advertisements