Introduction to Java

Introduction to Java

Introduction to Java basics

Written by: saeed1907057

Sun, 17 Nov 2024

Why java?

  • Platform Independence: Write once, run anywhere (JVM-based).
  • Object-Oriented: Encourages reusable and maintainable code.
  • Rich API: Extensive libraries for development.
  • Multithreading: Built-in support for concurrent programming.
  • Security: Strong security features for safe execution.
  • Scalability: Ideal for building large, scalable systems.
  • Community Support: Large, active community and resources.
  • Performance: Optimized with Just-In-Time (JIT) compilation.
  • Enterprise Adoption: Widely used in large-scale enterprise applications.

Setting up java

  • Download and install jdk. Jdk contains

  • Install the jdk by following the installation instruction.

  • Set Up Environment Variables

    • To use javac and java from any command prompt, we need to configure environment variables.
      • Open Environment Variables:
        • Right-click on This PC > Properties > Advanced system settings > Environment Variables.
      • Add JAVA_HOME(Not mandatory):
        • Under System Variables, click New.
        • Name: JAVA_HOME
        • Value: Path to your JDK installation (e.g., C:\Program Files\Java\jdk-XX).
      • Update Path Variable:
        • In System Variables, find and edit Path.
        • Add a new entry: %JAVA_HOME%\bin.
        • You can directly add the bin without the JAVA_HOME also.
      • Apply Changes:

        next image

        - Click ok and save the changes.
  • Verify Installation

    • Open Command Prompt and run the following commands:
      • Check java (Java Compiler):
        java -version
        
      • Check javac (Java Compiler):
        javac -version
        

Program execution

  • Let's write a simple java program first:

    public class FirstProgram {
    
        public static void main(String... args){
            System.out.println("Hello world");
        }
    }
    
    • Make sure to save it with .java extension with file name same as class name. FirstProgram.java.
  • Compile Java

    • Now we need to compile the file.
    • For compiling via cmd, we can use
      javac FirstProgram.java
      
    • If there are no error, it will create a file named FirstProgram.class. See the .class extension.
    • This generated file contains bytecode which is machine independent.
    • Content of generated file:
      Êþº¾   = 
            java/lang/Object <init> ()V            
       
       java/lang/System out Ljava/io/PrintStream;  
      Hello world
            java/io/PrintStream  println (Ljava/lang/String;)V   FirstProgram Code LineNumberTable main ([Ljava/lang/String;)V 
      SourceFile FirstProgram.java !                    *· ±            ‰       %         ²  
      ¶ ±       
                 
      
  • Executing the code

    • For executing the .class file, we can use the following command
      java FirstProgram
      
      • See we are not using extension here.
    • Output of the code:
      Hello world
      
User12024-11-02

This is a comment 1.

User12024-11-02

This is a comment 2.