Easy Tutorial
For Competitive Exams

Java Programming Introduction Theory

Programming in Java

Java is a programming language created by James Gosling from Sun Microsystems (Sun) in 1991.Oracle bought Sun, and thus they became the owners of Java.Java is open source, and you are free to use it.

Java program can be developed once and run on multiple operating systems. Java does not execute instructions on a computer directly, Instead it uses Java Virtual Machine (JVM).

Quick Q&A Session:

14949.Who invented java?
James Gosling
Dennis Ritchie
14950.Java is currently owned by?
Microsoft
Oracle

A Sample Java Program

In this example public, class, void, main are keyword, which we will learn shortly. Let us learn about main function and statements.

Main Function

Main function is from where the program execution begins. When you execute a program, Java starts your program by calling its main() function and executes statements, one by one. The syntax for the main function is as shown below.

Syntax for main function

 public static void main(String args[]) { 
   // Code Execution Begins Here. 
 }

Printing Text in Java

System.out.println statement is used to print text in java and here is the syntax.

Syntax

 System.out.println(" Your Text Here. "); 

Example: Printing Text in Java

 
public class JavaProgram {   
    public static void main(String[] args) {      
        System.out.println(" My First Java Program. ");  
    }       
}                           
 
Try it Yourself

Exercises

Exercise 1: Using two Statements

System.out.println(" Your Text Here. "); 
System.out.println(" Your Second Text Here. "); 
Do it Yourself

Please copy the two lines of above code and paste at main function in "Do it yourself" link.

Exercise 2: Using System.out.print

System.out.print(" Any Text Here. "); 
System.out.print(" More Text in Same Line."); 
Do it Yourself

Note: System.out.println prints next line after text. System.out.print doesn't, so any text follows is printed on the same line.

Compiling and Executing a Java Program

A compiler is a software that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor understands. In java a program is converted to bytecode, the compiler creates a .class file.

A compile time error is an error that is detected by the compiler, while converting java program to bytecode. If there is a error, .class file will not be created.

10744.

Exercise: Find the syntax error in the following program:

public class JavaProgram {
    public static void main(String[] args) {
       System.out.println(" A Java Program.")
    }       
}
A Java Program.
error: ';' expected
Explanation:
Every statement must be terminated with a semicolon(;).
10601.

Find the output of the following program:

                                               
public class FindTheError {
    public static void main(String[] args) {
       System.out.println("My Name"  "is John");
     }
}
compiler error
My Name is John
John
No Output
Explanation:
Simply use:System.out.println("My Name is John");
To join two strings, we learn shortly.

JVM

JVM (Java Virtual Machine) is a software. It is a specification that provides Runtime environment in which java bytecode can be executed.

Function of JVM

JVM mainly performs following operations.

  • Allocating sufficient memory space for the class properties.
  • Provides runtime environment in which java bytecode can be executed
  • Converting byte code instruction into machine level instruction.
  • JVM is separately available for every Operating System while installing java software so that JVM is platform dependent.

Exercises

10600.Find the output of the following program:
public class HelloWorld {
    public static void main(String[] args) {
       System.out.print("Hello ");
       System.out.println("World");
     }
}
Compiler Error
println and print function cannot be given together.
Hello World
Hello
  World
Explanation:
System.out.println prints text in next line. System.out.print function is used to print text in the same line.
Share with Friends