Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995 (now owned by Oracle). It’s designed to be platform-independent, meaning Java code can run on any device that supports the Java Virtual Machine (JVM), following the principle “write once, run anywhere” (WORA).
Contents
Example of Java Code
import java.util.Scanner; // Importing Scanner class to take input
// Class definition
public class CircleArea {
// Method to calculate the area of a circle
public static double calculateArea(double radius) {
return Math.PI * radius * radius; // Formula: π * r²
}
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Prompt user for input
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble(); // Take radius as input
// Call the method to calculate the area
double area = calculateArea(radius);
// Print the result
System.out.println("The area of the circle with radius " + radius + " is: " + area);
// Close the scanner
scanner.close();
}
}
Output:
Enter the radius of the circle: 5
The area of the circle with radius 5.0 is: 78.53981633974483
Key Features of Java
- Java supports the principles of OOP (Object-Oriented Programming), such as inheritance, polymorphism, encapsulation, and abstraction.
- Java programs are compiled into bytecode, which can run on any system that has the JVM installed.
- Java has a simple syntax, making it easier for beginners to pick up compared to languages like C++.
- Java includes built-in security features, such as bytecode verification and a secure execution environment.
- Java has strong memory management features (automatic garbage collection), which helps prevent memory leaks and crashes.
- Java supports multithreaded programming, allowing tasks to run concurrently.
- While Java is an interpreted language, the JVM optimizes code execution using Just-In-Time (JIT) compilation.
Advantages of Java
- The WORA principle ensures Java programs can be run on different operating systems without modification.
- Java has one of the largest developer communities, which ensures extensive resources, tutorials, and third-party libraries are available.
- Java offers a wide range of APIs for networking, input/output, database connections, and more.
- Java is highly scalable, making it suitable for both small applications and large enterprise-level systems.
- Java’s reliability and robustness have made it the language of choice for enterprise applications.
Uses of Java
- Java is used in back-end web development (e.g., Spring Framework) for building scalable web applications.
- Java is the core language for Android app development using Android Studio.
- Java is commonly used to build large-scale business applications for organizations.
- Java’s high performance and security make it ideal for scientific and research-based applications.
- Though not as popular as some other languages in gaming, Java has been used for developing some popular mobile and indie games.
- Java is often used to build cloud-based applications due to its portability and scalability features.