Java is a high-level programming language developed by Sun Microsystems and released in 1995.
Java is guaranteed to be Write Once, Run Anywhere.
Java is used to develop mobile apps, web apps, desktop apps, games and much more.
Java is an object-oriented, class-based, concurrent, secured and general-purpose programming language.
JAVA is platform-independent language. It is not specific to any processor or operating system.
There are 4 platforms or editions of Java:
Java SE (Java Standard Edition): It includes core topics like OOPs concepts, Strings, Regular expressions, Exceptions, Inner classes, Multithreading, Input/Output Stream, Networking, AWT, Swing, Reflection, Collection, etc.
Java EE (Java Enterprise Edition): It is an enterprise edition which is mainly used to develop web and enterprise applications. It includes topics like Servlet, JSP etc.
Java ME (Java Micro Edition): It is a micro platform which is mainly used to develop mobile(android) applications.
JavaFX: It is used to develop rich internet applications. It uses a light-weight user interface API.
Java Features
The primary objective of Java programming language creation was to make it portable, simple and secure programming language.
The features of Java are also known as java buzzwords.
Simple: Java is very easy to learn, and its syntax is simple, clean and easy to understand.
Object-oriented: Java is an object-oriented programming language. Everything in Java is an object which has some data and behaviour. Basic concepts of OOPs are Object, Class, Inheritance, Polymorphism, Abstraction, Encapsulation
Platform Independent: Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. On compilation Java program is compiled into bytecode. This bytecode is platform independent and can be run on any machine. Any machine with Java Runtime Environment can run Java Programs.
Secured: Java is best known for its security. With Java, we can develop virus-free systems. When it comes to security, Java is always the first choice.
Robust: Robust simply means strong. For example java uses strong memory management, in java there is automatic garbage collection which runs on the JVM to get rid of objects which are not being used by a Java application anymore. Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error checking and runtime checking.
Architecture-neutral: Java is architecture neutral because there are no implementation dependent features, for example, the size of primitive types is fixed. Compiler generates bytecodes, which have nothing to do with a
Portable: Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't require any implementation. Everything related to storage is predefined, example: size of primitive data types.
High-performance: Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native code. Java enables high performance with the use of just-in-time compiler. Java is an interpreted language, so it will never be as fast as a compiled language like C or C++.
Distributed: Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications.
Multi-threaded: A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. Benefit of multithreading is that it utilizes same memory and other resources to execute multiple threads at the same time.
Dynamic: Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.
Large Standard Library: One of the reasons why Java is widely used is because of the availability of huge standard library. The Java environment has hundreds of classes and methods under different packages to help software developers like us. For example java.lang package, java.util package, java.io package etc.
Install JDK(Java Development Kit)
Java Development Kit(JDK) allows you to code and run Java programs. It's possible that you install multiple JDK versions on the same PC. But Its recommended that you install only latest version.
Step 1: Click on download Download JDK to download the latest version of JDK.
Step 2: Once the download is complete, run the .exe file to install JDK.
Step 3: Once installation is complete click Close
Setting up path for windows:
Step 1: Right click on my computer and select properties.
Step 2: Go to the Advance System Settings tab.
Step 3: Click on Environment Variables button.
Step 4: Now alter the path variable so that it also contains the path to JDK installed directory. For ex: C:\Program Files\Java\jdk-11.0.1\bin
Step 5: Finally, we have to check if java is installed properly or not. We can do that by using java -version command using command prompt, if it display java version then java is successfully installed on your machine.
First Java Program
We have to follow some steps to Write, Compile and Run Java program.
Step 1: Open a text editor/Java IDE and write the Java code shown below.
Step 2: Save the file with ClassName.java in our example class name is HelloWorld so save it as HelloWorld.java
Step 3: Open command prompt and go to the directory where you saved your java program.
Step 4: Type javac ClassName.java to compile your code. This command will call the Java Compiler asking it to compile the specified file. If there are no errors in the code the command prompt will take you to the next line. In our example javac HelloWorld.java to compile the code.
Step 5: Now type java ClassName on command prompt to run your program. In this example java HelloWorld
Step 6: You will be able to see Hello world printed on your command prompt.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Code Explanation:
class : class keyword is used to declare classes in Java, each and every java program starts with creating one or more classes. A class can be defined as a template/blueprint that describes the behavior/state that the object of its type supports.
HelloWorld : This is the name of that class, here you can use any name to your class to identify. In our example we are using HelloWorld as class name and we have to store that file same as class name with extension .java, for example HelloWorld.java
public : It is an access specifier. Public means this method is visible to all. So the JVM can access the file to compile and run.
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. So it saves memory.
void : It is the return type, meaning this function will not return anything.
main : main() method is the most important and mandatory method in a Java program. This is the method which is executed by the JVM, hence all the logic must be inside the main() method. If a java class is not having a main() method, it causes compilation error. A method is basically a behavior. A class can contain many methods. main represents the starting point of the program.
String[] args : This represents an array whose type is String and name is args. This is used to store command line arguments, we will discuss about that later.
System.out.println : This is used to print anything on the console. Here, System is a class, out is the object of PrintStream class, println() is the method of PrintStream class.
Important Points to Remember about Syntax:
Case Sensitivity : Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
Class Names : For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case. Example: class HelloWorld
Method Names : All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case. Example: public void myMethod()
File Name : Name of the program file should exactly match the class name. When saving the file, you should save it using the class name and append '.java' to the end of the name. Example: Assume 'HelloWorld' is the class name. Then the file should be saved as 'HelloWorld.java'
public static void main(String args[]) : Java program processing starts from the main() method which is a mandatory part of every Java program.
Identifiers : Names used for classes, variables, and methods are called identifiers. All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). After the first character, identifiers can have any combination of characters. A key word cannot be used as an identifier.
Comments in Java
Java supports single-line and multi-line comments very similar to C and C++. All characters available inside any comment are ignored by Java compiler.
Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Single-line comments starts with two forward slashes (//). Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored by Java.
class CommentsDemo {
public static void main(String[] args) {
// This is a single line comment
System.out.println("Welcome to Java Programming");
}
}
class CommentsDemo {
public static void main(String[] args) {
/* This is the example for
Multiline Comments */
System.out.println("Welcome to my webpage");
}
}