What is Java ?
Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems’ Java platform (Java 1.0 [J2SE]).
First Java Program:
public class MyProgram
{
public static void main(String []args)
{
System.out.println("Hello World"); // prints Hello World
}
}
class : class keyword is used to declare classes in Java
public : It is an access specifier. Public means this function is visible to all.
static : static is again a keyword used to make a function static. To execute a static function you do not have to create an Object of the class. The main() method here is called by JVM, without creating any object for class.
void : It is the return type, meaning this function will not return anything.
main : main() method is the most important method in a Java program. This is the method which is executed, hence all the logic must be inside the main() method. If a java class is not having a main() method, it causes compilation error.
String[] args : This represents an array whose type is String and name is args. We will discuss more about array in Java Array section.System.out.println : This is used to print anything on the console like printf in C language.
In the Java programming language, all source code is first written in plain text files ending with the .java
extension. Those source files are then compiled into .class
files by the javac
compiler. A .class
file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine1 (Java VM). The java
launcher tool then runs your application with an instance of the Java Virtual Machine.
Let’s look at how to save the file, compile, run the program. Please follow the subsequent steps −
- Open notepad and add the code as above.
- Save the file as: MyProgram.java.
- Open a command prompt window and go to the directory where you saved the class. Assume it’s C:\.
- Type javac MyProgram.java and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption : The path variable is set).
- Now, type java MyFProgram to run your program.
- You will be able to see ‘ Hello World ‘ printed on the window.
Output:
C:\> javac MyProgram.java
C:\> java MyProgram
Hello World
Run Java in VScode Editor
An overview of the software development process.
Because the Java VM is available on many different operating systems, the same .class
files are capable of running on Microsoft Windows, the Solaris TM Operating System (Solaris OS), Linux, or Mac OS.
Some virtual machines, such as the Java HotSpot virtual machine, perform additional steps at runtime to give your application a performance boost.
This include various tasks such as finding performance bottlenecks and recompiling (to native code) frequently used sections of code.
Through the Java VM, the same application is capable of running on multiple platforms.
Installation of Java
The Java Platform
A platform is the hardware or software environment in which a program runs. We’ve already mentioned some of the most popular platforms like Microsoft Windows, Linux, Solaris OS, and Mac OS. Most platforms can be described as a combination of the operating system and underlying hardware. The Java platform differs from most other platforms in that it’s a software-only platform that runs on top of other hardware-based platforms. The Java platform has two components:
- The Java Virtual Machine
- The Java Application Programming Interface (API)
You’ve already been introduced to the Java Virtual Machine; it’s the base for the Java platform and is ported onto various hardware-based platforms.
The API is a large collection of ready-made software components that provide many useful capabilities. It is grouped into libraries of related classes and interfaces; these libraries are known as packages.
The API and Java Virtual Machine insulate the program from the underlying hardware.
NOTE:Before running any java program set the path.
How to set path in Java
The path is required to be set for using tools such as javac, java etc.
If you are saving the java source file inside the jdk/bin directory, path is not required to be set because all the tools will be available in the current directory.
But If you are having your java file outside the jdk/bin folder, it is necessary to set path of JDK.
There are 2 ways to set java path:
- temporary
- permanent
1) How to set Temporary Path of JDK in Windows
To set the temporary path of JDK, you need to follow following steps:
- Open command prompt
- copy the path of jdk/bin directory
- write in command prompt: set path=copied_path
For Example:
set path=C:\Program Files\Java\jdk1.6.0_23\bin
2) How to set Permanent Path of JDK in Windows
For setting the permanent path of JDK, you need to follow these steps:
- Go to MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok
Recommended Articles
- Basics of Java
- Features of Java
- Control Statements in Java
- Loop Statements in Java
- Array Handling in Java
- OOP’s Concept in Java
- Java Networking
- Exception Handling in Java