Solved

How does System.out..println work??

Posted on 2002-04-12
17
770 Views
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
0
Comment
Question by:priyendra
[X]
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
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6937951
Yes it is implemented in native code.
0
 
LVL 2

Expert Comment

by:mzimmer74
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.
0
 
LVL 35

Expert Comment

by:girionis
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.
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 92

Expert Comment

by:objects
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.
0
 
LVL 35

Expert Comment

by:girionis
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 ]:-)
0
 

Author Comment

by:priyendra
ID: 6938490
Hi!!

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
0
 

Author Comment

by:priyendra
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
0
 
LVL 92

Accepted Solution

by:
objects earned 200 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.
0
 

Expert Comment

by:rohitgautam
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 6940753
rohitgautam,

That has already mean stated.
0
 

Author Comment

by:priyendra
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
0
 
LVL 92

Expert Comment

by:objects
ID: 6943368
That aint a Java question.
0
 
LVL 92

Expert Comment

by:objects
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.
0
 

Author Comment

by:priyendra
ID: 6944147
That isn't enough to answer my question.
0
 
LVL 1

Expert Comment

by:Moondancer
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
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.
http://www.experts-exchange.com/questions/Q.20117772.html
http://www.experts-exchange.com/questions/Q.20127028.html
http://www.experts-exchange.com/questions/Q.20135961.html
http://www.experts-exchange.com/questions/Q.20157396.html
http://www.experts-exchange.com/questions/Q.20170687.html
http://www.experts-exchange.com/questions/Q.20186885.html
http://www.experts-exchange.com/questions/Q.20246420.html
http://www.experts-exchange.com/questions/Q.20277925.html
http://www.experts-exchange.com/questions/Q.20288205.html




*****  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.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 9

Expert Comment

by:Venci75
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.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:modulo
ID: 7990824
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

691 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