What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called Greentalk and the file extension was .gt . After that, it was called Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, g...
First Java Program
As when learning any new language, the place to start is with the classic Hello World! program. Our first Java program is as:
class FirstProgram{
public static void main(String[] argc){
System.out.print("Hello World!");
}
}
public static void main(String[] argc){
System.out.print("Hello World!");
}
}
To Compile:
javac FirstProgram.java
To Execute or interpret:
java FirstProgram
Output:
Hello World!
Let's break down the code to understand each line:
- class FirstProgram:
- A Java program must contain at least one class.
- class keyword is used to declare a class.
- The file name should be same as the class name that contains main() function.
- Here, our class name is FirstProgram.
- All the code (i.e. variables and functions) should be inside our class whether in a single class or in different classes, but will be inside class only.
- public static void main(String[] args){..}
- public keyword is an access modifier that represents visibility. public keyword means it is visible to all.
- static is a keyword. In Java, we need to create an object to call a function or a method. But by declaring a method as static, we doesn't need to create an object to call our function.
- void is the return type of the method or function. It means it doesn't return any value.
- main is the function name and every Java program must contain the main() method.
- String[] args or String args[] is a command line argument. It means an array of sequence of characters (i.e. String) that are passed to the main() function. This happens during the execution time of program.
- System.out.print("Hello World!")
- System is an inbuilt class and out is the object of System class used to call the print() function.
- System.out represents the output stream, where our output will go.
- print is the function name.
- The whole line is used to display the result on the screen.
- Anything (except escape sequences) inside the inverted commas are treated as a string and will be printed on the screen as plain text.
Naming Conventions
- For classes, we use PascalConvention. The first subsequent characters from a word are capital letters. Example: FirstProgram, CodingBro.
- For functions and variables, we use camelCaseConvention. Here, the first character is lowercase, and the subsequent characters are uppercases like myArray, myFunction.
Comments
Post a Comment