Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 8 of 50

Types of quantifiers in Java regex

Maruthi Krishna
Maruthi Krishna
Updated on 13-Jul-2020 931 Views

If you want to specify the number of occurrences while constructing a regular expression you can use quantifiers. Java supports three types of quantifiers namely: greedy quantifiers, reluctant quantifiers and possessive quantifiers.Greedy quantifiers − Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");     ...

Read More

While chaining, can we throw unchecked exception from a checked exception in java?

Maruthi Krishna
Maruthi Krishna
Updated on 03-Jul-2020 813 Views

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 with out 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 higher ...

Read More

Is there any method to convert a Set to immutable in Java

Maruthi Krishna
Maruthi Krishna
Updated on 03-Jul-2020 2K+ Views

Whenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.A Set is an interface in collection framework, which does not allow duplicate values.Method to convert a set to immutableYes, Java provides a method named unmodifiableSet() in the Collections class. This method accepts a collection object as a parameter and returns an unmodifiable i.ie immutable form of it.ExampleIn the following Java program, we have created a HashSet object ...

Read More

Why TreeSet Does not allow null values in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 03-Jul-2020 3K+ Views

TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in a sorted and ascending order.Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.The reason is, if you look at the internal implementation of the TreeSet, it uses natural ordering, that means TreeSet uses Comparable interface by default to sort its value by comparing other value.Examplepublic class TreeSetDemo {    public static void main(String args[]) {       TreeSet treeSet = new TreeSet();       ...

Read More

How to convert an array to Set and vice versa in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 03-Jul-2020 2K+ Views

Array is a container which can hold a fix number of entities, which are of the same type. Each entity of an array is known as element and, the position of each element is indicated by an integer (starting from 0) value known as index.Exampleimport java.util.Arrays; public class ArrayExample {    public static void main(String args[]) {       Number integerArray[] = new Integer[3];       integerArray[0] = 25;       integerArray[1] = 32;       integerArray[2] = 56;       System.out.println(Arrays.toString(integerArray));    } }Output[25, 32, 56]Whereas a Set object is a collection (object) stores ...

Read More

Is there any way to skip finally block even if some exception occurs in exception block using java?

Maruthi Krishna
Maruthi Krishna
Updated on 03-Jul-2020 5K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Try, catch, finally blocksTo handle exceptions Java provides a try-catch block mechanism.A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code.Syntaxtry {    // Protected code } catch (ExceptionName e1) {    // Catch block }When an exception raised inside a try block, instead of terminating the program JVM stores ...

Read More

Is it possible to resume java execution after exception occurs?

Maruthi Krishna
Maruthi Krishna
Updated on 03-Jul-2020 6K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.There are two types of exceptions in Java.Unchecked Exception − An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Checked Exception − A checked exception is an exception that occurs at ...

Read More

How can we decide that custom exception should be checked or unchecked in java?

Maruthi Krishna
Maruthi Krishna
Updated on 03-Jul-2020 6K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.User defined exceptionsYou can create your own exceptions in Java and they are known as user defined exceptions or custom exceptions.To create a user defined exception extend one of the above mentioned classes. To display the message override the toString() method or, call the superclass parameterized constructor by passing the message in String format.MyException(String msg){    super(msg); } Or, public String toString(){    return ...

Read More

How to print custom message instead of ErrorStackTrace in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 4K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Printing the Exception messageYou can print the exception message in Java using one of the following methods which are inherited from Throwable class.printStackTrace() − This method prints the backtrace to the standard error stream.getMessage() − This method returns the detail message string of the current throwable object.toString() − This message prints the short description of the current throwable object.Exampleimport java.util.Scanner; public class PrintingExceptionMessage { ...

Read More

What is the difference between throw e and throw new Exception(e) in catch block in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 6K+ Views

An exception is an issue (run time error) occurred during the execution of a program. Here are some example scenarios −If you have an array of size 10 if a line in your code tries to access the 11th element in this array.If you are trying to divide a number with 0 which (results to infinity and JVM doesn’t understand how to valuate it).When exception occurs the program terminates abruptly at the line that caused exception, leaving the remaining part of the program unexecuted. To prevent this, you need to handle exceptions.There are two types of exceptions in java.Unchecked Exception ...

Read More
Showing 71–80 of 500 articles
« Prev 1 6 7 8 9 10 50 Next »
Advertisements