allizwell.jobsdoor360.in

Introduction to Java

Java Programming › Module 1: Introduction to Java
JAVA PROGRAMMING FOR PLACEMENTS

Module 1: Introduction to Java

Welcome to the world of Java programming! In this module, we will start from the basics and understand what programming is, what Java is, how Java works, and how to write our first Java program. No previous programming knowledge is required.

Beginner Level 6 Topics Practical Examples

What Will You Learn in This Module?

After completing this module, you will be able to:

  • Understand programming and the Java language.
  • Identify the features and applications of Java.
  • Understand the difference between JDK, JRE, and JVM.
  • Install Java and set up your programming environment.
  • Understand the basic structure of a Java program.
  • Write, compile, and execute your first Java program.
TOPIC 01

1. Introduction to Programming and Java

What is Programming?

Programming means giving instructions to a computer to perform a particular task. A computer cannot understand our normal language directly. Therefore, we write instructions using programming languages.

Real-Life Example

Imagine you want a computer to add two numbers, 10 and 20. You need to give it instructions to take the numbers, add them, and display the result.

The computer follows these instructions and displays 30. This is a simple example of programming.

What is a Programming Language?

A programming language is a language used to communicate instructions to a computer. It helps programmers tell computers what to do.

Some popular programming languages are:

  • Java
  • Python
  • C
  • C++
  • JavaScript

What is Java?

Java is a high-level, general-purpose programming language developed by James Gosling and his team at Sun Microsystems. It was officially released in 1995.

Java is widely used to develop desktop applications, web applications, enterprise software, backend systems, and many other types of software.

Remember: Java is designed to be portable. A Java program can run on different operating systems using a compatible Java Runtime Environment.

Why Should We Learn Java?

  • Java is widely used in the software industry.
  • It is useful for learning Object-Oriented Programming.
  • It is commonly used in technical placement interviews.
  • It provides many built-in libraries and tools.
  • It is a popular language for solving DSA problems.

Where is Java Used?

  • Web Development: Building the backend of websites and applications.
  • Android Development: Java is used in many existing Android applications.
  • Banking: Developing banking and financial software.
  • Enterprise Applications: Building large business applications.
  • Desktop Applications: Creating software that runs on computers.

Interview Question

Q. What is Java?

Answer: Java is a high-level, general-purpose, object-oriented programming language that is designed to be portable and platform-independent through the Java Virtual Machine.

TOPIC 02

2. Features and Applications of Java

Important Features of Java

Java has several features that make it popular among programmers and software companies.

1. Simple

Java has a relatively easy-to-understand syntax. Its structure is similar to C and C++, but it removes several complex features such as manual pointer arithmetic and explicit memory deallocation.

2. Object-Oriented

Java follows Object-Oriented Programming (OOP). It organizes programs using classes and objects. This helps programmers write reusable and organized code.

Real-Life Example

Think about a car. A car has properties such as color, model, and speed. It also performs actions such as starting, stopping, and accelerating. Java classes and objects help represent such things in a program.

3. Platform Independent

Java follows the principle of "Write Once, Run Anywhere". Java source code is compiled into bytecode. This bytecode can run on any system that has a compatible JVM.

4. Secure

Java provides security features such as bytecode verification, controlled memory access, and runtime security mechanisms.

5. Robust

Java provides exception handling, type checking, and automatic memory management. These features help reduce common programming errors.

6. Multithreaded

Java supports multithreading, which allows multiple tasks to make progress concurrently within a program.

7. Portable

Java bytecode can be transferred between compatible systems without needing to rewrite the original program.

8. High Performance

Java uses Just-In-Time (JIT) compilation to improve execution speed by compiling frequently executed bytecode into machine code.

Applications of Java

  • Banking and financial systems
  • Enterprise software
  • Web applications
  • Android applications
  • Cloud-based applications
  • Large-scale business systems

Interview Questions

Q1. Why is Java called platform independent?

Answer: Java source code is compiled into bytecode, which can run on different operating systems through a compatible JVM.

Q2. Who developed Java?

Answer: Java was developed by James Gosling and his team at Sun Microsystems.

TOPIC 03

3. Understanding JDK, JRE, and JVM

JDK, JRE, and JVM are three important terms that every Java beginner must understand. They work together to help us develop and run Java applications.

What is JVM?

JVM stands for Java Virtual Machine. It is a virtual machine that executes Java bytecode.

When we compile a Java program, the compiler generates bytecode. The JVM loads and executes this bytecode on the system.

Simple Example

Think of JVM as a translator between Java bytecode and the computer's operating system and hardware. Each platform has its own JVM implementation.

What is JRE?

JRE stands for Java Runtime Environment. It provides the environment required to run Java applications. It includes the JVM and supporting runtime libraries.

What is JDK?

JDK stands for Java Development Kit. It provides the tools required to develop, compile, debug, and run Java programs.

It includes the Java compiler, development tools, runtime components, and libraries.

Difference Between JDK, JRE, and JVM

JDK JRE JVM
Java Development Kit Java Runtime Environment Java Virtual Machine
Used to develop and run programs Provides runtime environment Executes bytecode
Includes development tools Includes JVM and runtime libraries Executes Java bytecode

How Do They Work Together?

  1. We write Java source code in a .java file.
  2. The Java compiler (javac) compiles the source code.
  3. The compiler generates bytecode in a .class file.
  4. The JVM loads and executes the bytecode.
  5. The program produces the required output.
Java Execution Flow
Java Source Code (.java)
          |
          v
    Java Compiler
       (javac)
          |
          v
   Bytecode (.class)
          |
          v
         JVM
          |
          v
    Program Output
Important: The JVM is platform-dependent in its implementation, but Java bytecode is designed to be platform-independent.

Important Interview Question

Q. What is the difference between JDK, JRE, and JVM?

Answer: JDK provides development tools to create Java programs, JRE provides the runtime environment required to run Java programs, and JVM executes Java bytecode.

TOPIC 04

4. Installing Java and Setting Up the Development Environment

What is a Development Environment?

A development environment is a collection of software tools that helps programmers write, compile, execute, and debug programs.

Step 1: Download the JDK

To start programming in Java, we first need to install the Java Development Kit (JDK).

You can download the JDK from:

Oracle Java Downloads

You can also use OpenJDK distributions such as: Eclipse Temurin .

Step 2: Install the JDK

  • Download the appropriate JDK installer for your operating system.
  • Open the downloaded installer.
  • Follow the installation instructions.
  • Complete the installation process.

Step 3: Verify Java Installation

Open Command Prompt or Terminal and type the following command:

Command Prompt
java -version

If Java is installed correctly, the system will display the installed Java version.

To check the Java compiler, execute:

Command Prompt
javac -version

Step 4: Choose a Code Editor or IDE

An IDE (Integrated Development Environment) provides tools to write, execute, and manage programs.

  • IntelliJ IDEA: A popular IDE for Java development.
  • Eclipse: A widely used Java development environment.
  • VS Code: A lightweight code editor with Java extensions.

Our Recommendation

Beginners can start with IntelliJ IDEA or VS Code. You can also use Command Prompt to understand how Java programs are compiled and executed.

Remember: Installing an IDE alone is not enough. You also need a suitable JDK to compile and run Java programs.
TOPIC 05

5. Structure of a Java Program

Before writing large Java applications, we must understand the basic structure of a Java program. Every beginner should become familiar with its important parts.

Our First Java Program

HelloWorld.java
public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}

Output

Hello, World!

Understanding Each Part

1. public class HelloWorld

This statement declares a class named HelloWorld. A class is a blueprint used to create objects. We will learn more about classes and objects in the OOP module.

2. public static void main(String[] args)

This is the main method. It acts as the entry point for launching a traditional Java application.

  • public: Makes the method accessible.
  • static: Allows the method to be called without creating an object of the class.
  • void: Means the method does not return a value.
  • main: The special method name recognized by the Java launcher.
  • String[] args: Receives command-line arguments.

3. System.out.println()

This statement displays information on the screen. In our example, it prints "Hello, World!".

4. Curly Braces { }

Curly braces define the beginning and ending of a class or method body.

5. Semicolon ( ; )

In Java, a semicolon is used to mark the end of most statements.

Comments in Java

Comments are notes written inside a program. They help programmers understand the code. Comments are ignored by the Java compiler.

Types of Comments
// Single-line comment

/*
   Multi-line comment
*/

/**
 * Documentation comment
 */

Print Statements in Java

Java provides three commonly used output methods:

Example
public class PrintExample {
    public static void main(String[] args) {

        System.out.print("Hello ");
        System.out.println("Java");
        System.out.printf("Number: %d", 100);

    }
}

Output

Hello Java
Number: 100
  • print(): Displays text without automatically moving to the next line.
  • println(): Displays text and moves to the next line.
  • printf(): Displays formatted output.

Interview Question

Q. Why is the main method static in Java?

Answer: The main method is static so that the Java launcher can invoke it without creating an object of the class.

TOPIC 06

6. Writing, Compiling, and Executing Your First Java Program

Now that we understand the structure of a Java program, let's learn how to write, compile, and execute it.

Step 1: Write the Java Program

Open a code editor or text editor and write the following program.

HelloWorld.java
public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Welcome to Java Programming!");

    }

}

Step 2: Save the File

Save the program using the filename:

HelloWorld.java

Remember that when a class is declared public, the source filename must match the public class name.

Step 3: Open Command Prompt or Terminal

Navigate to the folder where you saved your Java file.

Step 4: Compile the Program

Type the following command:

Compile Command
javac HelloWorld.java

The Java compiler checks the source code and generates a bytecode file named HelloWorld.class if compilation is successful.

Step 5: Execute the Program

Now run the compiled program using:

Run Command
java HelloWorld

Output

Welcome to Java Programming!

Difference Between javac and java

javac java
Compiles Java source code. Launches the compiled Java application.
Converts .java into .class bytecode. Executes the application using the JVM.
Example: javac HelloWorld.java Example: java HelloWorld
Important: When running the program using the java command, write the class name without the .class extension.

Module 1: Quick Revision

Let's revise the important concepts covered in this module.

  • Programming means giving instructions to a computer.
  • Java is a high-level, general-purpose programming language.
  • Java was released in 1995.
  • Java was developed by James Gosling and his team.
  • JDK is used for Java development.
  • JRE provides the runtime environment.
  • JVM executes Java bytecode.
  • The main method is the traditional entry point of a Java application.
  • javac compiles Java source code.
  • java launches a compiled Java application.

Practice Questions

Try solving these questions to check your understanding.

  1. What is programming?
  2. What is Java?
  3. Who developed Java?
  4. Why is Java called platform independent?
  5. What is the full form of JVM?
  6. What is the difference between JDK and JRE?
  7. What is the purpose of the main method?
  8. What is the difference between print() and println()?
  9. What is the purpose of the javac command?
  10. Write a Java program to print your name.
  11. Write a Java program to print your name, age, and city on separate lines.
  12. Write a Java program to display the message "I am learning Java".

Important Java Interview Questions

1. Is Java a purely object-oriented programming language?

No. Java is not purely object-oriented because it supports primitive data types such as int, char, boolean, and double.

2. Is Java compiled or interpreted?

Java uses both compilation and runtime execution. Source code is compiled into bytecode, which is executed by the JVM using interpretation and Just-In-Time compilation.

3. What is Java bytecode?

Bytecode is the intermediate code generated by the Java compiler. It is stored in .class files and executed by the JVM.

4. Can Java run on different operating systems?

Yes. Java bytecode can run on different operating systems when a compatible JVM is available.

5. What happens if we do not write the main method?

In a traditional Java application launched using the standard java launcher, a suitable entry point is required. Without one, the application cannot be launched in the usual way.

6. What is the extension of a Java source file?

A Java source file uses the .java extension. After compilation, bytecode is stored in a .class file.

7. What is the difference between JDK and JVM?

JDK provides tools to develop Java applications, whereas JVM is responsible for executing Java bytecode.

Congratulations!

You have completed Module 1: Introduction to Java. You now understand the basics of Java and know how to write and execute your first Java program.

Continue your learning journey with Java Fundamentals.

Scroll to Top