Maruthi Krishna has Published 951 Articles

Explain about StringJoiner in java8?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 07:05:21

91 Views

Since Java8 StringJoiner class is introduced this you can construct a sequence of characters separated by desired delimiter.Sr.NoConstructor & Description1StringJoiner(CharSequence delimiter)This constructor creates an empty (no characters) StringJoiner, just with a copy of the specified delimiter.2StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)This constructs a StringJoiner without characters.Methods of StringJoiner classadd() − ... Read More

How do we extract all the words start with a vowel and length equal to n in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 06:58:55

1K+ Views

To find a word starts with a vowel letter −The split() method of the String class Split the given string into an array of Strings using the split() method of the String class.In the for loop traverse through each word of the obtained array.Get the first character of each word ... Read More

How to convert Java object to JSON using GSON library?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 06:35:44

1K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.There are several Java libraries available to handle JSON objects. Google Gson is a simple Java-based library to serialize ... Read More

In how many ways we can convert a String to a character array using Java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 06:32:04

200 Views

You can convert a String to a character array either by copying each element of the String to an array or, using the toCharArray() method.Copying each elementGet the String to be converted.Create an empty character array with the length of the String.The charAt() method of the String class returns the ... Read More

IlleagalStateException Vs NoSuchElementException in java?

Maruthi Krishna

Maruthi Krishna

Updated on 19-Sep-2019 15:31:21

144 Views

When you call a method at illegal or inappropriate time an IlleagalStateException is generated.For example, the remove() method of the ArrayList class removes the last element after calling the next() or previous methods.After removing the element at the current position you need to move to the next element to remove ... Read More

Can we throw an Unchecked Exception from a static block in java?

Maruthi Krishna

Maruthi Krishna

Updated on 12-Sep-2019 08:57:15

1K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class MyClass {    static{       System.out.println("Hello this is a static ... Read More

How do you throw an Exception without breaking a for loop in java?

Maruthi Krishna

Maruthi Krishna

Updated on 12-Sep-2019 08:54:22

8K+ Views

Whenever an exception occurred in a loop the control gets out of the loop, by handling the exception the statements after the catch block in the method will get executed. But, the loop breaks.Example Live Demopublic class ExceptionInLoop{    public static void sampleMethod(){       String str[] = {"Mango", "Apple", ... Read More

Can we to override a catch block in java?

Maruthi Krishna

Maruthi Krishna

Updated on 12-Sep-2019 08:46:56

856 Views

DescriptionWhen a piece of code in particular method throws an exception, and is handled using try-catch pair. If we are calling this method from another one and, the calling line is wrapped within try-catch pair. Now, how can I override the catch block by the catch block of the calling ... Read More

How to get Exception log from a console and write it to external file in java?

Maruthi Krishna

Maruthi Krishna

Updated on 12-Sep-2019 08:39:04

1K+ Views

There are several logging frame works available to log your data in to files. You can also define your own method.Example − Using I/O packageFollowing Java program has an array storing 5 integer values, we are letting the user to choose two elements from the array (indices of the elements) ... Read More

Does code form Java finally block

Maruthi Krishna

Maruthi Krishna

Updated on 12-Sep-2019 08:26:37

124 Views

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.The return statement in the method too does not stop the finally block from getting executed.ExampleIn the following Java program we are using return statement at the ... Read More

Advertisements