Core Java - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

First Java Program

First Java Program

shape Description

The chapter First Java Program will explain the basic program. Following are the some of the First Java Program steps needs to be followed while executing the code.

shape Step-1

Open Eclipse -> File -> New -> Java Project -> Enter App Name.

shape Step-2

Then click finish.

shape Step-3

src folder contains the source file of the project.(.java).To create source file right click on src -> package -> enter package name -> finish.

shape Step-4

Right click on package -> class -> enter name of the class -> finish.

shape Conceptual figure

shape More Info

A Java package is a group of Java program developed for a particular purpose. The packages are more re-usable during development. During development Java files are stored in separate folders by name, search where as, class files are stored in another directory by name bin folder. When the program is compiled, all classes are bound to one package. We can tell the compiler to keep the class file inside a specific folder by using an option -d >java -d  destinationfolder  Classname.java

shape Example

[java] package com.spl.firstapp; public class FirstApp { public static void main(String args[]){ System.out.println("Hello Welcome to SPLessons"); } } [/java] Output:Click on Run ,you can see output in console. [c]Hello Welcome to SPLessons[/c]

Understanding Java Program:

shape Terms

Class is a keyword which is used to declare a class in java.
public a keyword which is an access modifier which represents visibility.Using this keyword means it is visible to all.
Static is a keyword.If any method is declared as static, it is known as static method or if any variable is declared as a static it is known as static variable. To know more refer this.
void is the return type of the method.It means void doesn't return any value.
Main is a method which represents startup of the program.
String args[] is used for command line argument.
It is used to print statement or print the data.

Main method can be written in following ways:

shape Ways

shape More Info

  • Main method of a java class is developed to read command line argument.
  • Any information passed to the java class while running, is known as command line argument.
  • The command line argument are converted into string array and passed to main method.
  • Whenever a program is developed, it should read data from command line, then string array args of main is used.
  • If notepad is being used,save above class file as FirstApplication.java .Then go to command prompt,navigate to path where.java file is stored.
  • shape Compilation

    To compile use command as : javac classname.java To run use command as : java classname. The following is an example to find the factorial of a number. [java] class FactorialExample{ public static void main(String args[]){ int i,fact=1; int number=5;//It is the number to calculate factorial for(i=1;i<=number;i++){ fact=fact*i; } System.out.println("Factorial of "+number+" is: "+fact); } } [/java] The factorial is normally used in permutations and combinations, in the above example, for loop is used to compute the result, the same can be done with recursion also. Output: The following is the result will be displayed. [java] Factorial of 6 is: 720 [/java]

    Summary

    shape Key Points

    • First Java Program - Make sure to save the file with class name.java.
    • First Java Program - System.out.println(""); is used to print the statements.