
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

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

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

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

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

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

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

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

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

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

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