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
- public
- protected
- no modifier - means we wont specify any modifier sometime call it default
- private
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