Java language






History of Java:

- 1991: The Green Project starts, aiming to develop a programming language for the consumer electronics market. It was initially called Oak but later became Java.
- 1994: Release of the HotJava web browser, which demonstrated the potential of Java for web-based applications.
- 1995: Sun Microsystems officially announces Java.
- 1996: JDK 1.0, the first Java Development Kit, is released.
- 1997: JDK 1.1 is introduced, bringing features like RMI (Remote Method Invocation), AWT (Abstract Window Toolkit), and Servlets for web development.
- 1998: Java 1.2 is released, introducing Reflection, Swing, and Collections to the language.
- 2004: J2SE 1.5 (Java 5) is released, bringing Generics and enums to the language.
- 2014: Java SE 8 is released, introducing Lambdas and functional programming capabilities.
- 2017 onwards: Oracle continues to release new versions of Java, including Java SE 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, and 20.




Java Technology: 


● JDK: The Java Development Kit is a software development kit that provides the necessary tools, compilers, and libraries for developing Java applications. ● JRE: The Java Runtime Environment is a runtime environment that allows the execution of Java applications. It includes the necessary libraries and the Java Virtual Machine (JVM) to run Java programs. ● JVM: The Java Virtual Machine is the runtime environment in which Java bytecode is executed. It translates Java bytecode into machine-specific instructions and handles memory management and garbage collection.





● Object-oriented: Java is an object-oriented programming language, which means it emphasizes the use of objects and classes to structure and organize code. ● Interpreted: Java programs are interpreted by the Java Virtual Machine (JVM) at runtime, allowing for platform independence and flexibility. ● Portable: Java's "write once, run anywhere" principle enables programs to be developed on one platform and run on any platform with a compatible JVM. ● Secure and robust: Java incorporates built-in security features and exception handling, making it a reliable and secure language for developing applications. ● Scalable: Java applications can easily scale to accommodate larger workloads and handle increased demand, making them suitable for enterprise-level development. ● Multi-threaded: Java supports concurrent programming through its built-in support for multi-threading, allowing multiple threads of execution to run concurrently. ● Dynamic capabilities (reflection): Java provides reflection, allowing programs to examine and modify their own structure and behaviour at runtime. ● Distributed: Java includes libraries and features that facilitate the development of distributed applications, making it suitable for building networked and client-server systems.





Creating a "Hello World" Application in Java:


To create a "Hello World" application in Java, you can follow these steps:


  • Open a text editor and create a new file with a .java extension. For example, HelloWorld.java.

  • Inside the file, write the Java code to print "Hello, World!" to the console:


java:

public class HelloWorld {
    public static void main(String[] args) {
       System.out.println("Hello, World!");
    }
}


  • Save the file.

  • Open a command prompt or terminal and navigate to the directory where the HelloWorld.java file is saved.


  • Compile the Java source file by running the following command:

bash

javac HelloWorld.java

This command will generate a bytecode file named HelloWorld.class.



  • Run the compiled program by executing the following command:

bash

java HelloWorld

You should see the output Hello, World! displayed in the console.


Congratulations! You have successfully created and executed a "Hello World" application in Java.






Garbage Collection in Java:

- Garbage Collection is a memory management technique in Java.

- It automatically reclaims memory that is no longer needed by the program.

- It handles the deallocation of dynamically allocated memory.

- In languages like C and C++, programmers are responsible for manually deallocating memory.

- Manual deallocation can lead to memory leaks and program instability if not done properly.

- In Java, the responsibility of deallocating memory is shifted to the system through automatic garbage collection.

- The JVM has a built-in garbage collector that runs as a system-level thread.

- The garbage collector periodically checks allocated memory for unreferenced blocks.

- Unreferenced memory blocks are considered garbage and can be safely freed.

- The garbage collector uses algorithms like "mark-and-sweep" to identify and collect garbage.

- Garbage collection automates memory deallocation, relieving programmers from manual memory management.

- It reduces the risk of memory leaks and improves program reliability.

- Garbage collection ensures efficient and timely memory reclamation.





Points:

Portability refers to the ability of software or programming languages to run on different platforms or systems without requiring significant modifications or adaptations. In the context of compilers and interpreters, portability plays an important role. Let's discuss compilers and interpreters in relation to portability:


Compilers:

  • Compilers are software programs that translate the entire source code of a program into machine code or bytecode before execution.
  • Once the source code is compiled, the resulting executable file can be run directly on the target platform without the need for recompilation.
  • Compiled programs are often highly optimized and can offer better performance compared to interpreted programs.
  • However, for a program to be portable across multiple platforms, the source code must be written in a language that is supported by the compiler on each target platform.
  • If the target platforms have different instruction sets or hardware architectures, the source code may need to be modified or recompiled for each platform.


Interpreters:

  • Interpreters, on the other hand, execute the program source code directly without prior compilation.
  • The interpreter reads and interprets the source code line by line during runtime.
  • Interpreted programs are typically more portable because the interpreter itself can be implemented for different platforms.
  • As long as the interpreter is available for a specific platform, the same source code can be executed without modification.
  • However, interpreted programs may have slower execution speed compared to compiled programs due to the overhead of interpretation.

In summary, both compilers and interpreters have implications for the portability of software. Compilers require the source code to be compiled for each target platform but can offer better performance, while interpreters can execute the same source code across platforms but may have slower execution speeds. The choice between compilers and interpreters depends on factors such as performance requirements, target platforms, and the need for portability.






No comments

darkmode