Found 9326 Articles for Object Oriented Programming

Final Arrays in Java

Rishi Raj
Updated on 21-Jun-2020 14:07:08

1K+ Views

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object.However, the data within the object can be changed. So, the state of the object can be changed but not the reference. As an array is also an object and it is referred by a reference variable which if set as final then cannot be reassigned. Let's see the examples for further explanation.Examplepublic class Tester {    public static void main(String []args) {           final int[] arr = {1, 2, 3};   ... Read More

File Handling in Java using FileReader and FileWriter

Rishi Raj
Updated on 21-Jun-2020 14:23:52

1K+ Views

Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are, FileReader and FileWriter. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here the major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.Following example, which makes the use of these two classes to copy an input file (having unicode characters) into an output file −Exampleimport java.io.*; ... Read More

Factory method to create Immutable Set in Java SE 9

Paul Richard
Updated on 21-Jun-2020 13:58:41

80 Views

With Java 9, new factory methods are added to Set 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.Collections; import java.util.HashSet; import java.util.Set; public class Tester {    public static void main(String []args) {       Set set = new HashSet();       set.add("A");       set.add("B");       set.add("C");       Set readOnlySet = Collections.unmodifiableSet(set);       System.out.println(readOnlySet);       try {          readOnlySet.remove(0);       ... Read More

Factory method to create Immutable Map in Java SE 9

Vikyath Ram
Updated on 21-Jun-2020 14:01:01

135 Views

With Java 9, new factory methods are added to Map 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.Collections; import java.util.HashMap; import java.util.Map; public class Tester {    public static void main(String []args) {       Map map = new HashMap();       map.put("A", "Apple");       map.put("B", "Boy");       map.put("C", "Cat");       Map readOnlyMap = Collections.unmodifiableMap(map);       System.out.println(readOnlyMap);       try {          readOnlyMap.remove(0); ... Read More

Factory method to create Immutable List in Java SE 9

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

161 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

948 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

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

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

321 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

Do we need forward declarations in Java?

Paul Richard
Updated on 21-Jun-2020 12:43:56

450 Views

Forward declarations means the declaration of a method or variable prior to its implementation. Such declaration is necessary in C/C++ programming language in order to be able to use a variable or object before its implementation. In case, if we want to use a library code, then we need to create its header file and use it. But this is not a case in Java.Java allows using a variable, class prior to its declaration and implementation.Java allows using libraries code without any need of header files.Following example showcases the same. Here we have used a class object before its declaration.Examplepublic ... Read More

Advertisements