// This is the simple program in java
class Example
{
public static void main(String args[])
{
System.out.println("This is a simple program in java");
}
}

output:: This is a simple program in java

  • Java source code called as a compilation unit.
  • should be saved as .java file name extension with a class name.
  • java is case sensitive.
  • save the source file as Example.java

Compiling the program

  • using javac command followed by file name as javac Example.java
  • javac compiler generates Example.class file which is in byte code version.
  • JVM runs this byte code file.
  • the output of javac is not a code that executes directly.

Executing the Program

using java command followed by class file name as java Example.

The output is :: This is a simple program in java.

Reason to use class name as file name

  • javac generates a class file for each class having name same as
  • To run the program java command uses class file name so we have to save the source file as a class name so that the class file name and the source file name can be same without any ambiguous.

Key features common to all java programs are

  • comment- the content ignored by the compiler which explains the operation of the program.

single line comment- // for line by line description.

multi line comment- /* */ for 3-4 lines description.

Documentation comment- in html document like /** */.

  • class Example{ - class is a keyword to create a new class.

    the __Example__ is the identifier of class.

    all members of class will be within the opening curly braces. {}

  • public static void main(String args[ ]) { -The execution of java program starts from this function.

The public keyword is an access modifier, which allows the programmer to control the visibility of class members.

public - these members accessed by code outside the class in which it is declared.

private -which prevents a member from being used by code defined outside of its class

The main must be declared as public since it must be called by code outside of its class.

The keyword static allows main\( \) to be called without having to instantiate a particular instance of the class since JVM runs this before any objects are made.

void keyword tells that main method do not have return type.

String args[]- args is an array instance of class String. It receives any command-line arguments present when the program

is executed.

System.out.println("This is a simple Java program.");- the built-in println( ) method displays the string which is passed to it.System is a predefined class that provides access to the system,+

and out is the output stream that is connected to the console.

results matching ""

    No results matching ""