Beginning Java Programming
Some institutes/tutorials start java directly using Netbeans or Eclipse IDE. But this is not the correct way. A better method is to use a basic text editor like Notepad2 or Notepad++ to begin Java. IDE’s provide shortcuts which experienced programmers use. If beginners opt for these methods, they will leave half of the work for IDE and miss this ‘half’.
IDE is an Integrated Development Environment or a software which accelerates software development and provides features like syntax completion, error detection,error correction, auto indentation, drag and drop for G.U.I. and much more. One should go for an IDE only after practising a programming language on text editors.
The best text editor for starting java is TextPad (download from here). The best part of TextPad is that it provides Program compilation and execution shortcuts just like Turbo C++. ‘Ctrl + 1’ can be used to compile a Java program and ‘Ctrl + 2’ can be used to execute a compiled program.
First Simple Java Program
public static void main(String args)
System.out.println(“My first java program”);
Noteworthy points about this small program :
- In Java, even the main method lies inside a class, and Java file is saved with the name of the class containing main method so, the above code should be saved as “Demo.java”.
- In Java, ‘functions’ are known as ‘methods’ and variables are called as ‘attributes’
- Indentation is used to increase readability. Programmers must use indentation to make their code look clean.
- Main method is both public and static. Public methods are accessible outside the class also (public is similar to what it was in C++). As main method is accessed by Java Virtual Machine during execution, so it must be public. Static keyword is described later. For now, just keep in mind that ‘static’ method or variable has only copy for entire class irrespective of no. of objects.
- The main method takes a string array as an argument. This argument can be passed when executing the program.(Consider String to be a data type till further discussion)
- System.out.println() is similar to printf or cout<<. Here System is a class, out is a stream and println() is a method. ‘out’ is a static stream so it is accessed by name of class (System) rather than object of system class.
Write the above program (don’t copy) in a simple text editor (or Textpad). Save the file as “Demo.java” at some specific path. If you are using Textpad, use shortcuts described above to compile and execute the code.
If you are using a simple text editor, follow these steps
- Open command prompt (goto start -> run -> cmd – > Enter).
- Navigate to the directory containing the file which you have just saved.
- To compile java program, type “javac Demo.java”. Here ‘javac’ is the java compiler application and Demo.java is the name of the java program which we are compiling. After this step, Demo.class file is created in the directory.
- To execute java program, type “java Demo”. Here java is the name of application which will execute the .class file and show the output as shown here.