What is the Catch Block in Java

Swarali Sree
Updated on 25-Feb-2020 10:42:48

414 Views

A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in the try block, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.Exampleimport java.io.File; import java.io.FileInputStream; public class Test {    public static void main(String args[]) {       System.out.println("Hello");       try {          File file = new File("my_file");       ... Read More

What are I/O Classes in Java

Swarali Sree
Updated on 25-Feb-2020 10:31:37

5K+ Views

The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, object, localized characters, etc.A stream can be defined as a sequence of data. There are two kinds of Streams −InputStream: The InputStream is used to read data from a source.OutputStream: The OutputStream is used for writing data to a destination.As described earlier, a stream can be defined as a sequence of data. The InputStream is used to read data from ... Read More

What is Deserialization in Java

Monica Mona
Updated on 25-Feb-2020 10:30:46

341 Views

After a serialized object has been written into a file, it can be read from the file and Deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.Exampleimport java.io.*; public class DeserializeDemo {    public static void main(String [] args) {       Employee e = null;       try {          FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");          ObjectInputStream in = new ObjectInputStream(fileIn);          e = (Employee) in.readObject();          in.close(); ... Read More

Difference Between Serialization and Deserialization in Java

Samual Sam
Updated on 25-Feb-2020 10:28:26

2K+ Views

Serialization Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.Exampleimport java.io.*; public class SerializeDemo {    public static void main(String [] args) {       Employee e = new Employee();       e.name = "Reyan Ali";       e.address = "Phokka Kuan, Ambehta Peer";       e.SSN = 11122333;       e.number = 101;       try {         ... Read More

File Operations in Java

Monica Mona
Updated on 25-Feb-2020 10:23:05

304 Views

File class provides various methods to perform respective file operations.canRead(): This method tests whether the application can read the file denoted by this abstract pathname. It returns true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise.canWrite(): This method tests whether the application can modify the file denoted by this abstract pathname. It returns true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.createNewFile(): This method atomically creates a ... Read More

Method Must Be Implemented by All Threads in Java

Monica Mona
Updated on 25-Feb-2020 10:22:01

1K+ Views

While creating a thread class we must override the run() method of the Thread class. This method provides an entry point for the thread and you will put your complete business logic inside this method.Exampleclass ThreadDemo extends Thread {    private String threadName;    ThreadDemo( String name) {       threadName = name;       System.out.println("Creating " +        threadName );    }    public void run() {       System.out.println("Running " +          threadName );       try {          for(int i = 4; i ... Read More

What Does the Method remove(int) Do in Java

Govinda Sai
Updated on 25-Feb-2020 10:19:36

135 Views

The remove(int index) method of the java.util.ArrayList class removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(20);       arrlist.add(15);       arrlist.add(30);       arrlist.add(45);       System.out.println("Size of list: " + arrlist.size());       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       ... Read More

Create a Private Constructor in Java

Swarali Sree
Updated on 25-Feb-2020 10:17:06

366 Views

We can use a private contractor in a Java while creating a singleton class. The Singleton's purpose is to control object creation, limiting the number of objects to only one. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources, such as database connections or sockets.ExampleThe easiest implementation consists of a private constructor and a field to hold its result, and a static accessor method with a name like getInstance().The private field can be assigned from within a static initializer block ... Read More

What Does the Method ensureCapacity(int minCapacity) Do in Java

Govinda Sai
Updated on 25-Feb-2020 10:10:30

174 Views

The ensureCapacity(int minCapacity) method of the class java.util.ArrayList increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String args[]) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(10);       arrlist.add(50);       arrlist.add(30);       arrlist.ensureCapacity(15);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }OutputNumber = 10 Number = 50 Number = 30

What Does the Method firstElement Do in Java

Paul Richard
Updated on 25-Feb-2020 10:09:47

168 Views

The firstElement() method is used to return the first component (the item at index 0) of this vector.Exampleimport java.util.Vector; public class VectorDemo {    public static void main(String[] args) {       Vector vec = new Vector(4);       vec.add(4);       vec.add(3);       vec.add(2);       vec.add(1);       System.out.println("First element is :"+vec.firstElement());    } }OutputFirst element is :4

Advertisements