Purpose of the Flush Method in BufferedWriter Class in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:44:38

415 Views

While you are trying to write data to a Stream using the BufferedWriter object, after invoking the write() method the data will be buffered initially, nothing will be printed.The flush() method is used to push the contents of the buffer to the underlying Stream.ExampleIn the following Java program, we are trying to print a line on the console (Standard Output Stream). Here we are invoking the write() method by passing the required String.import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; public class BufferedWriterExample {    public static void main(String args[]) throws IOException {       //Instantiating the OutputStreamWriter class     ... Read More

Convert String to StringBuilder in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:38:52

681 Views

The append() method of the StringBuilder class accepts a String value and adds it to the current object.To convert a String value to StringBuilder object −Get the string value.Append the obtained string to the StringBuilder using the append() method.ExampleIn the following Java program, we are converting an array of Strings to a single StringBuilder object. Live Demopublic class StringToStringBuilder {    public static void main(String args[]) {       String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };       StringBuilder sb = new StringBuilder();       sb.append(strs[0]);       sb.append(" "+strs[1]);       sb.append(" ... Read More

Remove Leading Zeros from a String Using Apache Commons Library in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:31:57

2K+ Views

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    org.apache.commons    commons-lang3    3.9 Get the string.Pass the obtained string as first parameter and a string holding 0 as second parameter to the stripStart() method of the StringUtils class.ExampleThe following Java program reads an integer value from the user into a String and removes the leading zeroes from it using the stripStart() ... Read More

What is Loose Coupling and How to Achieve it Using Java

Maruthi Krishna
Updated on 15-Oct-2019 07:22:53

908 Views

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 {    void child(); } class Cat implements Animal {    public void child() {       System.out.println("kitten");    } } class Dog implements Animal {    public void child() {       System.out.println("puppy");    } } public class LooseCoupling {    public static void main(String args[]) {       Animal obj = new Cat();       obj.child();    } }Outputkitten

Can Subclass Choose Not to Throw an Exception When Overriding in Java?

Maruthi Krishna
Updated on 15-Oct-2019 07:13:35

656 Views

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; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super {    public void sampleMethod()throws FileNotFoundException {       System.out.println("Method of superclass");    } } public class ExceptionsExample extends Super {    public void sampleMethod() {       System.out.println("Method of Subclass");    }    public static void main(String args[]) ... Read More

Overriding Method Exception Handling in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:10:50

307 Views

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; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super {    public String readFile(String path)throws FileNotFoundException {       throw new FileNotFoundException();    } } public class ExceptionsExample extends Super {    @Override    public String readFile(String path)throws IOException {       //method body ......    } }Compile ... Read More

Make a Collection Thread-Safe in Java

Maruthi Krishna
Updated on 15-Oct-2019 07:08:23

2K+ Views

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 Collection synchronizedCollection(Collection c)This method accepts any collection object and, returns a synchronized (thread-safe) collection backed by the specified collection.2static List synchronizedList(List list)This method accepts an object of the List interfacereturns a synchronized (thread-safe) list backed by the specified list.3static Map synchronizedMap(Map m)This method accepts an object of the Map interface and, returns a synchronized (thread-safe) map backed by the specified map.4static ... Read More

Transient Keyword in Java Serialization

Maruthi Krishna
Updated on 15-Oct-2019 06:56:27

1K+ Views

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).Transient variables − The values of the transient variables are never considered (they are excluded from the serialization process). i.e. When we declare a variable transient, after de-serialization its value will always be null, false, or, zero (default value).Therefore, While serializing an object of a class, if you want JVM to neglect a particular instance variable you need can declare it transient.public transient int limit = ... Read More

Initialize Static Variables in a Default Constructor in Java

Maruthi Krishna
Updated on 15-Oct-2019 06:50:25

8K+ Views

Class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the class, there is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.If you declare a static variable in a class, if you haven’t initialized it, just like with instance variables compiler initializes these with default values ... Read More

Make Elements of Array Immutable in Java

Maruthi Krishna
Updated on 15-Oct-2019 06:44:33

3K+ Views

No, you cannot make the elements of an array immutable.But 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.And the asList() method of the ArrayList class accepts an array and returns a List object.Therefore, to convert an array immutable −Obtain the desired array.Convert it into a list object using the asList() method.Pass the obtained list as a parameter to the unmodifiableList() method.Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample ... Read More

Advertisements