Java is similar to the C & C++ Language, which is an object originated. most of the functionality of Java is taken from these two languages. Many of Java’s object-oriented features were taken from C++.
The Original motive for Java was not the building computer software or build websites. They wanted to create a language that is platform-independent and could be used for embedded systems in electronic devices, such as TV and remote controls. The problem with C and most other languages were that they are designed to be compiled for a specific architecture. so they need to recompile the program again and again for a different architecture. and many time changes needed in the program to make the execution happen according to the platform. this will turn out to be the platform-dependent code. To overcome this problem they invented java.
Below are the advantage of java
- Simple
- Secure
- Portable
- Object-oriented
- Robust
- Multithreaded
- Architecture-neutral
- High performance
A First Simple Program
Let's take a look at the simple java program below that printout hello world on console
/*
This is Hello World Java Program
Store in the file name HelloWorldJava.java
*/
public class HelloWorldJava {
// Your program execution start from here
public static void main(String args[]) {
System.out.println("Hello World !");
}
}
Compiling the Program
$\> javac HelloWorldJava.java
$\>ll
-rw-r--r-- 1 in-anil.sable 435B Feb 26 21:51 HelloWorldJava.class
-rw-r--r-- 1 in-anil.sable 258B Feb 26 21:50 HelloWorldJava.java
Running the Program
$\> java HelloWorldJava
Hello World !
A Closer Look at the First Sample Program
/*
This is Hello World Java Program
Store in the file name HelloWorldJava.java
*/
This section in the program represent the Multi Line Comment in the program. these line would be consider as a comments and completely ignore by the Java while compiling the program. We ofter call this as a multi line comment since once can specify many lines withing the pair of /* */
// Your program execution start from here
This line in the java program represents the Single line Comment. similar to the multiline comment, all the content specified on this line would be ignore by the java while compiling the program. We call it as a single line comment since you can write only 1 line of comment using //. once the line break java treat this as a executable instruction unless new line again start with single line or multi line comment.
Many Developer use the comments to add few details about the method like what is the usage of the method, what kind of parameter we are expecting and what could be the return values. But I personally feel you should avoid adding comments in the program/module unless it is absolutely necessary. I feel your code itself should be so clean that one should not feel the need for comment while reading your code. everything in your program should be self explanatory like name of classes, name of methods and variable name.
public class HelloWorldJava { .... }The above example uses keyword 'class' to declare the class in java followed by the name of your class. In this example it is 'HelloWorldJava'. You can also see the keyword 'public' which is an access specifier. Dont worry about that now, we would be going to explore all access specifier in java. for now you can just think that this class is Accessiable from other class also. In java programming eveyting like method, variables should be declared inside class only.
public static void main(String args[]) { .... }Above line is the declaration on main method. likewise in every other programming language, Application execution start from the main method. This would be the entry point for the the java application. every main method in java should be exactly similar to the above. when we execute the java program, java try to find out the main method in the class, which should be exactly similer to above. talking about the keywords, public is the access specifier which tell the scope of method from which it can get call. since main method is the entry point it should be public so that java can invoke it. We will talk more about the access specifier going ahead in this blog. Static is Non Access specifier, we would talk about this also going ahead in blog, As of now you can think static like, we can Access static things without its object. just by class name. void is return type since this application is not returning anything, we can specify void in its return type
System.out.println("Hello World !");Result of above line is that whatever is specified in the println("") is getting printed on the terminal. Here System is class inbuilt in java which has out public static variable / data member on which we have println method. System.out by default point out to the output device. in our case it is terminal. There are other data members like System.err, which point out to default error output device and System.in would be default input device, it would be keyboard
An Another Simple Example
class AnotherSimpleExample {
public static void main(String args[]) {
int firstNumber = 10;
int secondNumber = 15;
int sum = firstNumber + secondNumber;
System.out.println("Sum of numbers is :" + sum);
}
}
Above Example would be little beyond the scope of this lesson, but it will give you the feel of java program. and you can understand how the programs are written in java. By reading each statement you can eaisly understand what this program is doing.
So that All for this Lesson, Let me know if you did not understood anything on this lesson in below comment section, i will try to revert your or i will improve this lesson in this blog.
Till then Good Bye, Happy Coding ! ! !