Found 7442 Articles for Java

final, finally and finalize in Java

Fendadis John
Updated on 29-Jul-2021 14:01:06

12K+ Views

The final keyword can be used with class method and variable. A final class cannot be inherited, a final method cannot be overridden and a final variable cannot be reassigned.The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred. Using a finally block allows you to run any cleanup-type statements that you just wish to execute, despite what happens within the protected code.The finalize() method is used just before object is destroyed and can be called just prior to object ... Read More

final, finally and finalize in Java

Fendadis John
Updated on 29-Jul-2021 14:01:06

12K+ Views

The final keyword can be used with class method and variable. A final class cannot be inherited, a final method cannot be overridden and a final variable cannot be reassigned.The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred. Using a finally block allows you to run any cleanup-type statements that you just wish to execute, despite what happens within the protected code.The finalize() method is used just before object is destroyed and can be called just prior to object ... Read More

Final Arrays in Java

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

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

Final Arrays in Java

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

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

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

151 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 Set in Java SE 9

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

151 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

213 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 Map in Java SE 9

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

213 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

Advertisements