Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Java Virtual Machine handle I/O operations?

Tags:

java

io

jvm

I was looking through the list of Java bytecode instructions and I noticed that there aren't any I/O instructions. That intrigued me. How does the JVM execute methods like System.out.println when it doesn't support I/O instructions?

If it uses some form of memory mapped I/O then how does it communicate with the OS to read file descriptors, etc? Does the JVM implement its own layer of abstraction for handling I/O operations? Are the Java I/O packages (java.io & java.nio) implemented in C/C++ instead?

like image 998
Aadit M Shah Avatar asked Dec 13 '12 16:12

Aadit M Shah


People also ask

How does the Java Virtual Machine work?

The JVM converts the compiled binary byte code into a specific machine language. Java Virtual machine acts as a subpart of Java Runtime Environment(JRE). The JVM is an abstract machine that works on the top of existing processes. We can implement it in hardware or software.

Does Java run on a virtual machine?

A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes what is required in a JVM implementation.

What is the Java Virtual Machine JVM and what does it do?

The Java Virtual Machine (JVM) is the runtime engine of the Java Platform, which allows any program written in Java or other language compiled into Java bytecode to run on any computer that has a native JVM. JVMs run in both clients and servers, and the Web browser can activate the JVM when it encounters a Java applet.

What are the benefits of Java Virtual Machine?

Advantages and Disadvantages of JVM The primary advantage of Java JVM is code compatibility as it eases a programmer's job to write code only once and run anywhere. Once the application is built it can be run on any device that has JVM. Apart from this it provides security.

Is JVM a compiler or interpreter?

The JVM converts that code into machine code using the Java interpreter. The JVM uses the interpreter at runtime, after that it execute the code on the host machine. As the Java compiler compiles the source code into the Java bytecode.


1 Answers

If you look at the library source code, you'll see that all interfacing with low-level APIs (OS etc) is done using native code.

For example, take FileOutputStream:

/**
 * Opens a file, with the specified name, for writing.
 * @param name name of file to be opened
 */
private native void open(String name) throws FileNotFoundException;

/**
 * Writes the specified byte to this file output stream. Implements
 * the <code>write</code> method of <code>OutputStream</code>.
 *
 * @param      b   the byte to be written.
 * @exception  IOException  if an I/O error occurs.
 */
public native void write(int b) throws IOException;

/**
 * Writes a sub array as a sequence of bytes.
 * @param b the data to be written
 * @param off the start offset in the data
 * @param len the number of bytes that are written
 * @exception IOException If an I/O error has occurred.
 */
private native void writeBytes(byte b[], int off, int len) throws IOException;

And then there's the corresponding C file (often OS-specific).

like image 99
NPE Avatar answered Oct 04 '22 21:10

NPE