Solved

How does System.out..println work??

Posted on 2002-04-12
17
752 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
  • 6
  • 4
  • 2
  • +5
17 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
Yes it is implemented in native code.
0
 
LVL 2

Expert Comment

by:mzimmer74
Comment Utility
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
Comment Utility
> 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Expert Comment

by:rohitgautam
Comment Utility
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
Comment Utility
rohitgautam,

That has already mean stated.
0
 

Author Comment

by:priyendra
Comment Utility
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
Comment Utility
That aint a Java question.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
That isn't enough to answer my question.
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
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
Comment Utility
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
Comment Utility
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now