Tag Archives: introduction to java

Introduction to Java

Java is a high level, modern programming language designed in the early 1990s by Sun Microsystems, and currently owned by Oracle.
Java is Platform Independent, which means that you only need to write the program once to be able to run it on a number of different platforms!
Java is simple,portable, robust, dynamic,and secure,with the ability to fit the needs of virtually any type of application.
Java guarantees that you’ll be able to Write Once, Run Anywhere.

               More than 3 billion devices run Java.

Java is used to develop apps for Google’s Android OS, various Desktop Applications, such as media players, antivirus programs, Web Applications, Enterprise Applications (i.e. banking), and many more!

             Your First Java Program

Let’s start by creating a simple program that prints “Hello World” to the screen.

public class HelloWorld {
 public static void main(String[ ] args) {
 System.out.println("Hello World");
 }
}

In Java, every line of code that can actually run needs to be inside a class.
In our example, we named the class HelloWorld.

In Java, each application has an entry point, or a starting point, which is a method called main. Along with main, the keywords public and static will also be explained below.
As a summary:
a) Every program in Java must have a class.
b) Every Java program starts from the main method.

The main Method

To run our program, the main method must be identical to this signature:

public static void main(String[ ] args)
1. public: anyone can access it.
2. static: method can be run without creating an instance of the class containing the main method.
3. void: method doesn’t return any value.
4. main: the name of the method

For example, the following code declares a method called foo, which does not return anything and has no parameters:

void foo()
The method‘s parameters are declared inside the parentheses that follow the name of the method. For main, it’s an array of strings called args.
Next is the body of the main method, enclosed in curly braces:
{
 System.out.println("Hello World!");
}
The println method prints a line of text to the screen.
The System class and its out stream are used to access the println method.
In classes, methods, and other flow-control structures code is always enclosed in curly braces { }.

              Semicolons in Java

You can pass a different text as the parameter to the println method to print it.

class DemoClass {
 public static void main(String[ ] args) {
 System.out.println("I am learning Java");
 }
}

In Java, each code statement must end with a semicolon.
Remember: do not use semicolons after method and class declarations that follow with the body defined using the curly braces.