Monday, July 27, 2020

Understand Java programming by Hello World Example

If you want to learn any computer programming languages, must starts with Hello World! example program. If we understand the this sample program, will learn around 25 to 30% basics and syntax, but generally we don't care about Hello World program and most of the time, just see it's a simple Hello World! message being printed and move on learn advance feature of it. That's were we lack our basics and programming syntax.

I will try to explain Java programming basics and it's syntax with help of Hello World! program. You can use Notepad, Eclipse or Intellij IDE to create sample program. As a beginner we must use Notepad, as progress and learn basics and syntax we can use the IDE.

package java.example;

public class HelloWorld {

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

The first line
package java.example;
specifies the package name were our class resides in. What is package in Java?
Well,
  • A package is a namespace that organizes a set of related classes and interfaces (will be discussed later blogs)
  • You can think of packages as being similar to different folders/directories on your computer
  • Packages are used in Java in order to prevent naming conflicts and access control
  • Interfaces and classes with the same name cannot appear in the same package, they can appear in different packages.
  • You can name them anything you wish too, but Sun has published some naming conventions that you should use when naming packages.
  • Example.
    • com.mydomain
    • net.example
    • in.domain.myblog

The line
    public class HelloWorld

The word "public" is the access specifier/modifier in java, it means access level modifiers determine whether other classes can use a particular field or invoke a particular method.

Let's what are the access specifier available in Java?
there are 4 access modifier in Java
  1. public
  2. protected
  3. no modifier - means we wont specify any modifier sometime call it default
  4. private
The scope access specifier is mentioned in the below tabular form.



The word "class" in the second line, everything in java wrapped under the class. Class in java is blueprint of object and describes behavior of object.

The word "HelloWorld"

<in progess>



No comments:

Post a Comment

Java - History and Evolution

Knowing the history and evolution is very important for any language. Lets look at the Java history and its evolution. Java was started a pr...