How to print to screen in java . With examples

Getting started with java. 


The Java Development Kit – JDK

In order to get started in Java programming, one needs to get a recent copy of the Java JDK. This can

be obtained for free by downloading it from the Sun Microsystems website, http://java.sun.com/

Once you download and install this JDK you are ready to get started. You need a text editor as well

and Microsoft’s Notepad (standard with all Windows versions) suits fine.

My first Java program

Open your text editor and type the following lines of code:

/*

My first program

Version 1
*/



public class Example1 {

 public static void main (String args []) {

 System.out.println ("My first Java program");

 }

}



Save the file as Example1.java2

. The name of the program has to be similar to the filename.

Programs are called classes. Please note that Java is case-sensitive. You cannot name a file

“Example.java” and then in the program you write “public class example”. It is good practice to

insert comments at the start of a program to help you as a programmer understand quickly what the

particular program is all about. This is done by typing “/*” at the start of the comment and “*/”

when you finish. The predicted output of this program is:

My first Java program

In order to get the above output we have to first compile the program and then execute the

compiled class. The applications required for this job are available as part of the JDK:

 javac.exe – compiles the program

 java.exe – the interpreter used to execute the compiled program

In order to compile and execute the program we need to switch to the command prompt. On

windows systems this can be done by clicking Start>Run>cmd



Ideally you should create a folder on the root disk (c:\) and save the file there.



At this point one needs some basic DOS commands in order to get to the directory (folder), where

the java class resides:

 cd\ (change directory)

 cd\[folder name] to get to the required folder/directory

When you get to the required destination you need to type the following:

c:\[folder name]\javac Example1.java

The above command will compile the java file and prompt the user with any errors. If the

compilation is successful a new file containing the bytecode is generated: Example1.class

To execute the program, we invoke the interpreter by typing:

c:\[folder name]\java Example1

The result will be displayed in the DOS window.

Comments