Maruthi Krishna has Published 870 Articles

What is the role of the String intern() method in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 08:55:14

130 Views

A String is a class in Java which stores a sequence of characters, it belongs to the java.lang package. Once you create a String object you cannot modify them (immutable).StorageAll String objects are stored in a separate memory location in the heap area known as, String Constant pool.Whenever you define ... Read More

How do we mix two strings and generate another in java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Oct-2019 08:20:46

14K+ Views

Strings are used to store a sequence of characters in Java, they are treated as objects. The String class of the java.lang package represents a String.You can create a String either by using the new keyword (like any other object) or, by assigning value to the literal (like any other ... Read More

Explain about StringJoiner in java8?

Maruthi Krishna

Maruthi Krishna

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

168 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 to convert Java object to JSON using GSON library?

Maruthi Krishna

Maruthi Krishna

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

2K+ 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

327 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

235 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

2K+ 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

9K+ 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

1K+ 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

Advertisements