Java methods to convert Double to String

Krishna Kasyap
Published on 15-Oct-2019 11:03:34
Following are methods available in Java using which you can convert Double value to String −The toString() method of the class DoubleThis method returns the String format of the current Double object. To convert Double value to String.Read the required primitive double value in to the Double class reference variable ... Read More

How to wrap a JSON using flexjson in Java?

raja
Published on 15-Oct-2019 11:01:06
The Flexjson library is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can wrap a JSON object using the rootName() method of JSONSerializer class, ... Read More

Removing leading zeros from a String using apache commons library in Java

Krishna Kasyap
Published on 15-Oct-2019 11:00:38
The stripStart() method of the org.apache.commons.lang.StringUtils class accepts two strings and removes the set of characters represented by the second string from the string of the first string.To remove leading zeros from a string using apache communal library −Add the following dependency to your pom.xml file<dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-lang3</artifactId> ... Read More

Remove Leading Zeroes from a String in Java using regular expressions

Krishna Kasyap
Published on 15-Oct-2019 10:57:46
The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.Following is the regular expression to match the leading zeros of a string −The ^0+(?!$)";To remove the leading zeros from a string pass this as ... Read More

Can an interface in Java extend multiple interfaces?

Krishna Kasyap
Published on 15-Oct-2019 10:54:28
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static. Just like classes you can extend one interface from another using the extends keyword as shown below:interface ArithmeticCalculations {    public abstract int addition(int a, int b);    public ... Read More

What is loose coupling how do we achieve it using Java?

Krishna Kasyap
Published on 15-Oct-2019 10:45:13
Coupling refers to the dependency of one object type on another, if two objects are completely independent of each other and the changes done in one doesn’t affect the other both are said to be loosely coupled.You can achieve loose coupling in Java using interfaces -Example Live Demointerface Animal {   ... Read More

While overriding can the subclass choose not to throw an exception in java?

Krishna Kasyap
Published on 15-Oct-2019 10:42:20
If the super-class method throws certain exception, you can override it without throwing any exception.ExampleIn the following example the sampleMethod() method of the super-class throws FileNotFoundException exception and, the sampleMethod() method does not throw any exception at all. Still this program gets compiled and executed without any errors. Live Demoimport java.io.File; ... Read More

Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?

Krishna Kasyap
Published on 15-Oct-2019 10:39:31
If the super-class method throws certain exception, the method in the sub-class should not throw its super type.ExampleIn the following example the readFile() method of the super-class throws FileNotFoundException exception and, the readFile() method of the sub-class throws an IOException, which is the super type of the FileNotFoundException. Live Demoimport java.io.File; ... Read More

How to make a collection thread safe in java?

Krishna Kasyap
Published on 15-Oct-2019 10:37:08
The Collections class of java.util package methods that exclusively work on collections these methods provide various additional operations which involves polymorphic algorithms.This class provides different variants of the synchronizedCollection() method as shown below −Sr.NoMethods & Description1static <T> Collection<T> synchronizedCollection(Collection<T> c)This method accepts any collection object and, returns a synchronized (thread-safe) ... Read More

How to make a collection read only in java?

Krishna Kasyap
Published on 15-Oct-2019 10:33:56
The Collections class of java.util package methods that exclusively work on collections these methods provide various additional operations which involves polymorphic algorithms.This class provides different variants of the unmodifiable() method as shown below −Sr.NoMethods & Description1static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)This method accept any of the collection object and ... Read More
Advertisements