JAVA-intro - Notes By ShariqSP
Introduction to Java
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle Corporation) in the mid-1990s. It was designed with the principle of "Write Once, Run Anywhere" (WORA), meaning that Java programs can run on any device that has a Java Virtual Machine (JVM).
History of Java
Java was originally created by James Gosling, Patrick Naughton, and Mike Sheridan at Sun Microsystems in 1991. It was initially called Oak, but was later renamed Java in 1995. The first publicly available version of Java, Java 1.0, was released in 1996.
Why We Need Java
Java offers several advantages, making it a popular choice for software development:
- Platform Independence: Java programs can run on any platform that has a JVM, which makes it highly portable. This platform independence is achieved through the compilation of Java code into an intermediate representation called bytecode, which can be executed on any JVM.
- Object-Oriented: Java follows an object-oriented programming paradigm, which promotes code reusability, modularity, and easier maintenance. Object-oriented programming allows developers to model real-world entities as objects, encapsulating data and behavior together.
- Robustness: Java incorporates features like strong memory management, exception handling, and type checking, which make it a robust language. These features help developers catch and handle errors effectively, leading to more reliable software.
- Security: Java has built-in security features such as bytecode verification and sandboxing to protect against malicious activities. The Java Security Manager allows developers to define access controls and restrict the actions of untrusted code.
- Large Ecosystem: Java has a vast ecosystem of libraries, frameworks, and tools that support various types of development, from enterprise applications to mobile apps and web development. Popular Java frameworks include Spring, Hibernate, and Apache Struts.
Usage of Java in Industry
Java is extensively used in various industries and domains:
- Enterprise Software: Many large-scale enterprise applications, including banking systems, e-commerce platforms, and customer relationship management (CRM) software, are built using Java. Java's scalability, reliability, and performance make it a preferred choice for such applications.
- Android Development: Android, the world's most popular mobile operating system, uses Java as the primary language for app development. Java is used to develop Android applications, libraries, and frameworks. Android Studio, the official IDE for Android development, provides comprehensive tools for building Java-based apps.
- Web Development: Java is used for server-side programming in web development. Frameworks like Spring and Hibernate are widely used for building robust and scalable web applications. JavaServer Pages (JSP) and Servlets are also commonly used for generating dynamic web content.
- Big Data Technologies: Java is used in big data processing frameworks like Apache Hadoop and Apache Spark, which are essential for analyzing and processing large datasets. These frameworks leverage Java's scalability and performance to handle the massive volumes of data generated in today's digital world.
- Financial Services: Java is extensively used in the financial industry for building trading platforms, risk management systems, and high-frequency trading algorithms. Java's reliability, performance, and security features make it well-suited for handling sensitive financial transactions.
Java Versions
Java has evolved over the years with several versions and updates, each bringing new features, improvements, and bug fixes.
Java SE (Standard Edition)
Java SE is the core Java platform for developing desktop, web, and enterprise applications. The current version of Java SE is Java SE 17, which was released in September 2021. It introduces features such as pattern matching for switch statements, sealed classes, and a new macOS rendering pipeline.
Java EE (Enterprise Edition)
Java EE, now known as Jakarta EE, is a set of specifications for developing enterprise applications in Java. It provides APIs and runtime environments for building distributed, transactional, and scalable applications. The latest version of Jakarta EE is Jakarta EE 9, which focuses on modernizing the platform by removing outdated APIs and aligning with the latest Java SE versions.
Java ME (Micro Edition)
Java ME is a platform for developing applications for mobile and embedded devices. It is designed for resource-constrained devices with limited memory, processing power, and storage. However, Java ME has seen declining usage with the rise of other mobile platforms like Android and iOS.
Most Stable Version
As of now, Java SE 17 is considered the most stable version of Java. It is a long-term support (LTS) release, which means it will receive updates and support for an extended period, typically several years. LTS releases are recommended for production environments where stability and compatibility are crucial.
Installing Java 17 on Windows and Configuring Environment Variables
Step 1: Download Java Development Kit (JDK) 17
Visit the official Oracle website or the OpenJDK website to download the Java Development Kit (JDK) 17 installer for Windows.
Step 2: Run the Installer
Double-click on the downloaded installer file to run it. Follow the installation wizard instructions to complete the installation process.
Step 3: Set Up Environment Variables
- Find JDK Installation Directory: Once the installation is complete, locate the directory where JDK 17 is installed. By default, it is usually installed in "C:\Program Files\Java\" or "C:\Program Files (x86)\Java\".
- Set JAVA_HOME Variable: Right-click on "This PC" or "My Computer" and select "Properties". Click on "Advanced system settings" on the left-hand side. In the System Properties window, click on the "Environment Variables" button. Under the "System variables" section, click "New" to add a new variable. Set the variable name as "JAVA_HOME" and the variable value to the path of the JDK installation directory (e.g., "C:\Program Files\Java\jdk-17"). Click "OK" to save the changes.
- Update PATH Variable: Find the "Path" variable in the System variables section and select it. Click "Edit" to modify the variable value. Add "%JAVA_HOME%\bin" to the beginning of the variable value (without quotes). This will ensure that the Java executable files are accessible from any command prompt. Click "OK" to save the changes.
Step 4: Verify Installation
Open a command prompt and type the following command:
java -version
This command should display the installed Java version (Java 17) and other related information. Additionally, you can also check the JAVA_HOME variable by typing:
echo %JAVA_HOME%
This command should print the path to the JDK 17 installation directory.
Step 5: Additional Configuration (Optional)
Depending on your development environment and requirements, you may need to configure additional environment variables or settings. For example, if you're using an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA, you may need to specify the JDK path in the IDE settings.
Writing and Running Your First Java Program without an IDE
Step 1: Install Java Development Kit (JDK)
Before you can write and run Java programs, you need to have the Java Development Kit (JDK) installed on your system. Follow the instructions in the previous section to install JDK 17 on your Windows system.
Step 2: Write Your Java Program
Open a text editor such as Notepad or any other code editor of your choice. Write the following Java program:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This is a simple program that prints "Hello, World!" to the console.
Step 3: Save the Java File
Save the file with a ".java" extension. For example, you can name it "HelloWorld.java". Make sure to save it in a directory where you can easily locate it.
Step 4: Open Command Prompt
Open the Command Prompt by typing "cmd" in the Windows search bar and pressing Enter.
Step 5: Navigate to the Directory
Use the "cd" command to navigate to the directory where you saved your Java file. For example:
cd path/to/your/directory
Step 6: Compile Your Java Program
Compile your Java program using the "javac" command followed by the name of your Java file:
javac HelloWorld.java
This will generate a bytecode file named "HelloWorld.class".
Step 7: Run Your Java Program
Run your Java program using the "java" command followed by the name of the class (without the ".class" extension):
java HelloWorld
You should see the output "Hello, World!" printed in the Command Prompt.
Step 8: Congratulations!
You have successfully written and run your first Java program without using an Integrated Development Environment (IDE)!
Compilation and Execution of a Java Program
When you compile and run a Java program, several steps are involved in the process, which can be summarized as follows:
Step 1: Writing the Java Program
The first step is to write the Java program source code using a text editor or an Integrated Development Environment (IDE). The source code is written in plain text and typically saved with a ".java" extension.
Step 2: Compilation
When you compile a Java program, the Java compiler (javac) translates the human-readable Java source code into platform-independent bytecode. The bytecode is stored in files with a ".class" extension. During compilation, the compiler performs various tasks such as syntax checking, type checking, and generating bytecode.
Step 3: Class Loading
When you run a Java program, the Java Virtual Machine (JVM) loads the bytecode files generated during compilation. The process of loading classes involves locating the bytecode files on the file system and reading them into memory.
Step 4: Bytecode Verification
Before executing the bytecode, the JVM performs bytecode verification to ensure that it adheres to the rules of the Java programming language. Bytecode verification checks for various security vulnerabilities and ensures that the bytecode will not cause any harm to the system.
Step 5: Execution
Once the bytecode has been loaded and verified, the JVM begins executing the program. The JVM interprets the bytecode instructions and executes them on the underlying hardware platform. During execution, the program may interact with the system's resources, such as input/output devices, memory, and network connections.
Step 6: Output
As the program executes, it may produce output, which can be displayed in the console, written to files, or sent over the network. The output generated by the program depends on its logic and functionality.
Step 7: Termination
Once the program has finished executing, the JVM terminates the program and releases any resources that were used during execution. The termination of the program marks the end of its lifecycle.
Overall, the compilation and execution of a Java program involve translating the source code into bytecode, loading and verifying the bytecode, executing the bytecode on the JVM, producing output, and finally terminating the program.