L2: Simple java programs

Q1. Differentiate between a statement and a comment in the context of programming. How to represent a comment in java ?

A statement in a program is a line of code that performs a basic operation. For example, System.out.println("Welcome to Sotantra"); is a statement that displays the message Welcome to Sotantra on the screen.

Comments in a program are used by the programmers to explain the code. These are only for the human readers and totally ignored by the compiler. Line comment in java is preceded by two slashes(//). Java compiler ignores // and all text that follows // on the same line. Block comment or paragraph comment in java is enclosed between the delimiters /* and */ and consists of one or more than one lines. The compiler ignores all texts written between /* and */ in a program. Some example of comments are as follows:

// This program computes area of a rectangle
// this is an example of single line comment
/* block comment in a single line */
/* example block comment consisting
of multiple lines */

Q2. How to compile and run a java program ?

First write the java program using any text editor i.e. notepad. Then, save the java program or java source code file with .java extension. The file name must be same as the public class name or the class name that contains main method. For example, if you write a java program with public class Test then, the file name must be Test.java for the program. Next, compile the java source file using the command javac Test.java because in this example the file name is Test, otherwise you can replace Test with your file name. Java compiler takes the java source file with .java extension as input and produces the bytecode with .class extension if there are no syntax errors in the program. To execute a java program, we need to run the bytecode generated from the program. In the present context the command java Test is used to run the bytecode otherwise, you can replace Test with your file name.

Q3. Write java statement using the method System.out.println to print Hello Sotantra on one line in the command window.

System.out.println ("Hello Sotantra");

Q4. Write java statement using the method System.out.println to print Welcome to "Sotantra" on one line in the command window.

System.out.println ("Welcome to \" Sotantra\" ");