Found 7442 Articles for Java

Factory method to create Immutable List in Java SE 9

Vikyath Ram
Updated on 21-Jun-2020 14:03:37

237 Views

With Java 9, new factory methods are added to List interface to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in concise way.Old way to create collectionsExampleimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class Tester {    public static void main(String []args) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       List readOnlylist = Collections.unmodifiableList(list);       System.out.println(readOnlylist);       try {          readOnlylist.remove(0);       ... Read More

Factory method to create Immutable List in Java SE 9

Vikyath Ram
Updated on 21-Jun-2020 14:03:37

237 Views

With Java 9, new factory methods are added to List interface to create immutable instances. These factory methods are convenience factory methods to create a collection in less verbose and in concise way.Old way to create collectionsExampleimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class Tester {    public static void main(String []args) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       List readOnlylist = Collections.unmodifiableList(list);       System.out.println(readOnlylist);       try {          readOnlylist.remove(0);       ... Read More

Externalizable Interface in Java

Vikyath Ram
Updated on 21-Jun-2020 13:49:30

1K+ Views

Externalization is used whenever we need to customize serialization mechanism. If a class implements an Externalizable interface then, object serialization will be done using writeExternal() method. Whereas at receiver's end when an Externalizable object is a reconstructed instance will be created using no argument constructor and then the readExternal() method is called.If a class implements only Serializable interface object serialization will be done using ObjectoutputStream. At the receiver's end, the serializable object is reconstructed using ObjectInputStream.Below example showcases usage of Externalizable interface.Exampleimport java.io.Externalizable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; public class ... Read More

Externalizable Interface in Java

Vikyath Ram
Updated on 21-Jun-2020 13:49:30

1K+ Views

Externalization is used whenever we need to customize serialization mechanism. If a class implements an Externalizable interface then, object serialization will be done using writeExternal() method. Whereas at receiver's end when an Externalizable object is a reconstructed instance will be created using no argument constructor and then the readExternal() method is called.If a class implements only Serializable interface object serialization will be done using ObjectoutputStream. At the receiver's end, the serializable object is reconstructed using ObjectInputStream.Below example showcases usage of Externalizable interface.Exampleimport java.io.Externalizable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; public class ... Read More

Dynamic method dispatch or Runtime polymorphism in Java

Rishi Raj
Updated on 21-Jun-2020 13:12:05

12K+ Views

Runtime Polymorphism in Java is achieved by Method overriding in which a child class overrides a method in its parent. An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method. This method call resolution happens at runtime and is termed as Dynamic method dispatch mechanism.ExampleLet us look at an example.class Animal {    public void move() {       System.out.println("Animals can move");    } } class Dog extends Animal {    public void move() {       System.out.println("Dogs can walk and ... Read More

Dynamic method dispatch or Runtime polymorphism in Java

Rishi Raj
Updated on 21-Jun-2020 13:12:05

12K+ Views

Runtime Polymorphism in Java is achieved by Method overriding in which a child class overrides a method in its parent. An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method. This method call resolution happens at runtime and is termed as Dynamic method dispatch mechanism.ExampleLet us look at an example.class Animal {    public void move() {       System.out.println("Animals can move");    } } class Dog extends Animal {    public void move() {       System.out.println("Dogs can walk and ... Read More

Download webpage in Java

Vikyath Ram
Updated on 21-Jun-2020 13:16:19

3K+ Views

We can download a web page using its URL in Java. Following are the steps needed.Create URL object using url string.Download webpage in JavaCreate a BufferReader object using url.openStream() method.BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));Create a BufferWriter object to write to a file.BufferedWriter writer = new BufferedWriter(new FileWriter("page.html"));Read each line using BufferReader and write using BufferWriter.String line; while ((line = reader.readLine()) != null) { writer.write(line); }Following is the complete program to download a given URL page at current location.import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class Tester {    public static void main(String ... Read More

Download webpage in Java

Vikyath Ram
Updated on 21-Jun-2020 13:16:19

3K+ Views

We can download a web page using its URL in Java. Following are the steps needed.Create URL object using url string.Download webpage in JavaCreate a BufferReader object using url.openStream() method.BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));Create a BufferWriter object to write to a file.BufferedWriter writer = new BufferedWriter(new FileWriter("page.html"));Read each line using BufferReader and write using BufferWriter.String line; while ((line = reader.readLine()) != null) { writer.write(line); }Following is the complete program to download a given URL page at current location.import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class Tester {    public static void main(String ... Read More

Double brace initialization in Java

Paul Richard
Updated on 21-Jun-2020 12:59:12

401 Views

Double braces can be used to create and initialize objects in a single Java expression. See the example below −Exampleimport java.util.ArrayList; import java.util.List; public class Tester{    public static void main(String args[]) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       list.add("D");       list.add("E");       list.add("F");       System.out.println(list);       List list1 = new ArrayList() {       {          add("A"); add("B");add("C");          add("D");add("E");add("F");       ... Read More

Double brace initialization in Java

Paul Richard
Updated on 21-Jun-2020 12:59:12

401 Views

Double braces can be used to create and initialize objects in a single Java expression. See the example below −Exampleimport java.util.ArrayList; import java.util.List; public class Tester{    public static void main(String args[]) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       list.add("D");       list.add("E");       list.add("F");       System.out.println(list);       List list1 = new ArrayList() {       {          add("A"); add("B");add("C");          add("D");add("E");add("F");       ... Read More

Advertisements