Java is a high-level, object-oriented programming language. This language is very easy to learn and widely used. It is known for its platform independence, reliability, and security. It follows one principle, that is, the "Write Once, Run Anywhere" principle. It supports various features like portability, robustness, simplicity, multithreading, and high performance, which makes it a popular choice for beginners as well as for developers.
- Simple Syntax
Java syntax is very straightforward and very easy to learn. Java removes complex features like pointers and multiple inheritance, which makes it a good choice for beginners.
Example: Basic Java Program
import java.io.*;
class Geeks {
public static void main(String[] args)
{
System.out.println("GeeksForGeeks!");
}
}
2.2. Object-Oriented
Java is a pure object-oriented language. It supports core OOP concepts like,
Class
Objects
Inheritance
Encapsulation
Abstraction
Polymorphism
Example: The below Java program demonstrates the basic concepts of OOPs.
// Java program to demonstrate the basic concepts of oops
// like class, object, constructor, and method
import java.io.*;
class Student {
int age;
String name;
public Student(int age, String name)
{
this.age = age;
this.name = name;
}
// This method display the details of the student
void display()
{
System.out.println("Name is: " + name);
System.out.println("Age is: " + age);
}
}
class Geeks {
public static void main(String[] args)
{
Student student = new Student(22, "GFG");
student.display();
}
}
- Platform Independent
Java is platform-independent because of the Java Virtual Machine (JVM).
When we write Java code, it is first compiled by the compiler and then converted into bytecode (which is platform-independent).
This byte code can run on any platform that has JVM installed.
- Interpreted
Java code is not directly executed by the computer. It is first compiled into bytecode. This byte code is then understood by the JVM. This enables Java to run on any platform without rewriting code.
- Scalable
Java can handle both small- and large-scale applications. Java provides features like multithreading and distributed computing that allow developers to manage loads more easily.
- Portable
When we write a Java program, the code first gets converted into bytecode, and this bytecode does not depend on any operating system or any specific computer. We can simply execute this bytecode on any platform with the help of JVM. Since JVMs are available on most devices,we can run the same Java program on different platforms.
- Secured and Robust
Java is a reliable programming language because it can catch mistakes early while writing the code and also keeps checking for errors when the program is running. It also has a feature called exception handling that helps deal with unexpected problems smoothly.
- Memory Management
Memory management in Java is automatically handled by the Java Virtual Machine (JVM).
The Java garbage collector reclaims memory from objects that are no longer needed.
Memory for objects are allocated in the heap
Method calls and local variables are stored in the stack.
- High Performance
Java is faster than old interpreted languages. A Java program is first converted into bytecode, which is faster than interpreted code. It is slower than fully compiled languages like C or C++ because of the interpretation and JIT compilation process. Java performance is improved with the help of Just-In-Time (JIT) compilation, which makes it faster than many interpreted languages but not as fast as fully compiled languages.
- Multithreading
Multithreading in Java allows multiple threads to run at the same time.
It improves CPU utilization and enhances performance in applications that require concurrent task execution.
Multithreading is especially important for interactive and high-performance applications, such as games and real-time systems.
Java provides built-in support for managing multiple threads. A thread is known as the smallest unit of execution within a process.
Example: Basic Multithreading in Java
// Java program to demonstrate multithreading
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
public class Geeks {
public static void main(String[] args) {
MyThread thread = new MyThread();
// Starts the thread
thread.start();
}
}
These are some of the features of Java that I have learned in my class today. Thank you for reading my blog. See you all with my next topic!!
Tidak ada komentar:
Posting Komentar