Basic Structure of a Java Program
Basic Structure of a Java Program:
Tutorial 2 --- Understanding our First Java Hello World Program
In our previous tutorial we made our first JAVA program, but
we didn’t understand anything, so this tutorial is the anatomy of our previous
tutorial’s program’
Here’s the CODE:
package
com.company;
public class Main
{
public static void main(String[]
args) {
// write your code here
System.out.println("Hello World");
}
}
ANATOMY:
package com.company;
A package in Java is used to group related classes.
Think of it is as a folder in a file directory.
The company is function, all functions have their
classes; Here the company function’s class is main
What
is a Function?
A method is a block of code which only runs when it is
called. You can pass data, known as parameters, into a method. Methods are used
to perform certain actions, and they are also known as functions.
public class Main {
What is a Class?
A class--the basic
building block of an object-oriented language such as Java--is a template that describes the data and behavior
associated with instances of that class. When you
instantiate a class you create an
object that looks and feels like other instances of the same class.
public static void main(String[] args) {
Here the main() method is the entry into the
application.
We didn’t use any functions in here, instead we used
the keyword static. Static means we don’t have to make class/object to run this
program.
In void main(), void is to tell us that it’ll not
return any value from the program, void means the value is empty.
public here is a access modifier, that means can use
this program from anywhere, we will discuss more of this in our next tutorials.
// write your code here
System.out.println("Hello World");
In this section our code will execute. We write in
this area for code execution.
// write your code here
This line is called a comment, a comment is a line
in a code which ignored by the interpreter/compiler. This a single line comment.
We will know more about comments in our next tutorials.
System.out.println("Hello World");
This the print function.
You can see that we used class Main’s M in upper
case and in below function’s m as lower case; here we’ve used “Naming conventions”.
For classes we use
PascalConvention.
Example
– AddTwoNumbers
For functions we use camelCaseConvention
Eaxmple - addTwoNumbers
Comments
Post a Comment