Found 2620 Articles for Java

How to send data over remote method in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:33:54

491 Views

RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java.rmi.To write an RMI Java application, you would have to follow the steps given below −Step1 − Define the Remote InterfaceA remote interface provides the description of all the methods of a particular remote object. The client communicates with this remote interface. Therefore, you need to create an interface that extends the predefined interface, ... Read More

What are the key steps in read/write from/to a URL connection in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:26:34

199 Views

The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource(file or, directory or a reference) in the world wide web.This class provides various constructors one of them accepts a String parameter and constructs an object of the URL class.The openStream() method of this class opens a connection to the URL represented by the current object and returns an InputStream object using which you can read data from the URL.Therefore, to read data from web page (using the URL class) −Instantiate the java.net.URL class by passing the URL of the desired web ... Read More

How to handle EOFException in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:20:58

1K+ Views

While reading the contents of a file in certain scenarios the end of the file will be reached in such scenarios a EOFException is thrown.Especially, this exception is thrown while reading data using the Input stream objects. In other scenarios a specific value will be thrown when the end of file reached.ExampleLet us consider the DataInputStream class, it provides various methods such as readboolean(), readByte(), readChar() etc.. to read primitive values. While reading data from a file using these methods when the end of file is reached an EOFException is thrown.import java.io.DataInputStream; import java.io.FileInputStream; public class EOFExample {    public ... Read More

What are the restrictions imposed on a static method or a static block of code in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:17:47

4K+ Views

static methods and static blocksStatic methods belong to the class and they will be loaded into the memory along with the class, you can invoke them without creating an object. (using the class name as reference).Whereas a static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class Sample {    static int num = 50;    static {       System.out.println("Hello this is a static block");    }    public static void ... Read More

How can we map multiple date formats using Jackson in Java?

raja
Updated on 06-Jul-2020 12:33:55

2K+ Views

A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. We can map the multiple date formats in the Jackson library using @JsonFormat annotation, it is a general-purpose annotation used for configuring details of how values of properties are to be serialized. The @JsonFormat has three important fields: shape, pattern,  and timezone. The shape field can define structure to use for serialization (JsonFormat.Shape.NUMBER and JsonFormat.Shape.STRING), the pattern field can be used in serialization and deserialization. For date, the pattern contains SimpleDateFormat compatible definition and finally, the timezone field can be used in serialization, default ... Read More

Can Enum extend any class in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:14:37

1K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Just like arrays, the elements/constants in an enumeration are identified using numbers starting from 0 in the above example the days are identified using numbers as shown in the following illustration −Enumerations are similar to classes and, you can have ... Read More

How to run a JAR file through command prompt in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:11:42

3K+ Views

For packaging of class files Java provides a file format known as JAR (Java Archive). Typically, a JAR file contains .class files, images, text files, libraries that are required to execute the application or, library.This file format is used to distribute application software and libraries in Java. All the predefined libraries are available in this format.If you have any library in this format to use it In your application either you need to place it in the current (or, lib) folder of the project or, you need to set the class path for that particular JAR file.Creating a Jar fileJava ... Read More

How many types of memory areas are allocated by JVM in java?

Maruthi Krishna
Updated on 14-Oct-2019 08:06:04

966 Views

Java Virtual Machine is a program/software which takes Java bytecode (.class files) and converts the byte code (line by line) into machine understandable code.JVM contains a module known as a class loader. A class loader in JVM loads, links and, initializes a program. It −Loads the class into the memory. Verifies the byte code instructions.Allocates memory for the program.JVM memory locationsJVM has five memory locations namely −Heap − Runtime storage allocation for objects (reference types).Stack − Storage for local variables and partial results. A stack contains frames and allocates one for each thread. Once a thread gets completed, this frame also ... Read More

Why can't a Java class be both abstract and final?

Maruthi Krishna
Updated on 14-Oct-2019 08:04:43

3K+ Views

Abstract classA class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.If you want to use the concrete method in an abstract class you need to inherit the class, provide implementation to the abstract methods (if any) and then, you using the subclass object you can invoke the required methods.ExampleIn the following Java example, the abstract class MyClass contains a concrete method with name display.From another class (AbstractClassExample) we are inheriting the class MyClass and invoking the its concrete method display using the subclass ... Read More

How to read a .txt file with RandomAccessFile in Java?

Maruthi Krishna
Updated on 14-Oct-2019 08:00:58

1K+ Views

In general, while reading or writing data to a file, you can only read or, write data from the start of the file. you cannot read/write from random position.The java.io.RandomAccessFile class in Java enables you to read/write data to a random access file.This acts similar to a large array of bytes with an index or, cursor known as file pointer you can get the position of this pointer using the getFilePointer() method and set it using the seek() method.This class provides various methods to read and write data to a file. The readLine() method of this class reads the next ... Read More

Advertisements