How does System.out..println work??

Posted on 2002-04-12
Medium Priority
Last Modified: 2007-12-19
I tried to decompile the JDK osurce code to figure out  how exactly does the function call System.out.println() communicate with the console and produce output on the screen. However, in the decompiled code, I only saw normal JVM instructions. Nothing special which would have hinted that the actual printing is taking place.

Could any one please explain how does the JVM manage to produce output on the screen?? Does this have something to do with JNI native methods??

-- Priyendra
Question by:priyendra
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +5
LVL 92

Expert Comment

ID: 6937951
Yes it is implemented in native code.

Expert Comment

ID: 6938209
Just as an fyi for you...when you download the jdk, you also get a file called src.jar.  This is all the source code that comes with the jdk.  If you want to look at any particular portion of it, you can unjar it (jar -xvf src.jar) and open the java file up.
LVL 35

Expert Comment

ID: 6938268
> Could any one please explain how does the JVM manage to produce output on the screen?? Does this have
something to do with JNI native methods??

  Well it is actually very simple I believe (but do not take it for granted). As you know the bytecode is translated by the virtual machine to assembly instructions that each processor understands. In order to print on the screen you need the appropriate bytecode to do so. The specific bytecode in the println() method does exactly this, prints the characters on the screen.

  I guess if you read the "The Java language specification" and understand how bytecode works you could write your own source code that compiles and does a similar thing.

  This is just my opinion. Maybe someone who is more familiar can shed some light.
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

LVL 92

Expert Comment

ID: 6938279
> The specific bytecode in the println() method does exactly this

The actually handling of writing to the console is handled in native code, and NOT bytecode. The bytecode merely calls the native code.
LVL 35

Expert Comment

ID: 6938321
> The actually handling of writing to the console is handled in native code, and NOT bytecode. The bytecode
merely calls the native code.

  Hmmm... Something I did not know as well. Everyday for me something new. Thanks objects ]:-)

Author Comment

ID: 6938490

The general opinion out here seems that this thing is done using native code. Actually I am implementing a simple JVM for a course of mine. I read somewhere in the book "Inside the Java Virtual Macine" , that implementations may choose to NOT support native method calls. So I had decided that to make things simpler, I will not suport native methods. So in that case, will I have to write my own Java class library. I say this because, I feel, that most of the class library that ships with Sun's JDK would be using native code and therefore I will not be able to use those functions. So for example will I have to write my own functions for printing to the screen which do not use any native code?

-- Priyendra

Author Comment

ID: 6938491
Also, are there any virtual machine specific insturctions that allow programs to call native methods.  I mean is there any instruction in the invoke_ family that lets you make native method calls??

-- Priyendra
LVL 92

Accepted Solution

objects earned 800 total points
ID: 6938502
> So in that case, will I have to write my own Java class library.

There are some parts of Sun's class library that do not use native methods, but there are also a lot that do. So you should be able to use some but definitely not all.

> will I have to write my own functions for printing to
> the screen which do not use any native code?

AWT certainly used native code, but Swing does not use as much. Think it uses native code mainly for top level windows.

Expert Comment

ID: 6939799
As u know java is also written in c and c++. The functionality of println method in class System is written in C++ i.e. native code.
LVL 92

Expert Comment

ID: 6940753

That has already mean stated.

Author Comment

ID: 6941979
Hi all!!

I consulted a few other people about this native code thing and came to the conclusion that if I have to make println and all work in my JVM then I willhave to link to the native code Sun has provided. Any idea anyones, as to how does one load a .so file in linux and call the functions in it??

-- Priyendra
LVL 92

Expert Comment

ID: 6943368
That aint a Java question.
LVL 92

Expert Comment

ID: 6943389
An answer has been proposed for this question so it is locked. If it answers your question then accept it, otherwise reject it to unlock your question.

Author Comment

ID: 6944147
That isn't enough to answer my question.

Expert Comment

ID: 6956451
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thank you everyone.
Moderator @ Experts Exchange

Expert Comment

ID: 7940652
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: objects
Please leave any comments here within the next seven days.
EE Cleanup Volunteer

Expert Comment

ID: 7990824
Finalized as proposed


Community Support Moderator
Experts Exchange

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question