0% found this document useful (0 votes)
20 views7 pages

Java Basics: Hello World & Addition Program

Uploaded by

deepalipatil0605
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Java Basics: Hello World & Addition Program

Uploaded by

deepalipatil0605
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BTIT307N Introduction to core JAVA

Experiment – 3

Title:- Write a program to print “Hello World Program” in java.

Objective:- To introduce the basic structure of a Java program and demonstrate how to write,
compile, and execute a simple "Hello World" program. This serves as the foundation for
understanding Java syntax and the execution process.

Theory:-
Introduction:- The "Hello World" program is traditionally the first program written when
learning a new programming language. It serves as a simple example to understand the basic
syntax, structure, and compilation process of the language. In Java, writing a "Hello World"
program introduces essential concepts such as classes, methods, and the main() method.

Purpose of the 'Hello World' Program:-

 To understand the structure of a basic Java program.


 To learn how to define a class and a method.
 To get familiar with Java’s output statement ( [Link]).
 To test the Java development environment setup (JDK installation, compiler, and
runtime).

Implementation:-

Program for “Hello World” -

Class Hello

Public static void main (String[] args)

[Link](“Hello World Program”);

2410DMTCSE17966 MANSI PATIL


BTIT307N Introduction to core JAVA

Output:-

Quiz:-

1. What is the correct syntax to print "Hello, World!" in Java?

A) [Link](“Hello World”);
B) echo(“Hello World”);
C) [Link](“Hello World”);
D) print(“Hello World”);

2. What is the entry point of a Java program?

A) start()
B) main()
C) run()
D) begin()

3. What does public static void main (String[] args) mean in Java?

A) It defines the method that prints text


B) It initializes the Java class
C) It is the main method where execution starts
D) It closes the Java program

4. In which file should the Hello World class be saved?

2410DMTCSE17966 MANSI PATIL


BTIT307N Introduction to core JAVA

A) [Link]
B) [Link]
C) [Link]
D) [Link]

5. What is the purpose of [Link]() in Java?

A) To read input from the user


B) To stop the program
C) To compile the program
D) To display output on the console

Viva Questions:-

1. What is the purpose of writing a "Hello World" program in Java?


2. Why is the main method necessary in every Java application?
3. What is the function of [Link]() in Java?
4. Why do we declare the main method as static in Java?
5. What is the significance of the file name in relation to the class name in Java?
6. What are the steps to compile and execute a Java program using the command line?

Lab Assignment:-

The objective of this lab is to introduce students to the basic structure of a Java program.
Students will write a simple program that prints "Hello, World!" using the
[Link]() statement. This helps in understanding the role of the class, the main
method, and Java syntax. The file should be saved as Hello [Link] , compiled using
javac, and executed with java. The program should display the message "Hello, World!" on
the console. This exercise ensures that the Java environment is properly set up and
functioning. It serves as the first step toward learning Java programming.

2410DMTCSE17966 MANSI PATIL


BTIT307N Introduction to core JAVA

Experiment – 4

Title:- Write a program to print “Addition of Two Numbers” in java.

Objective:- To develop a Java program that accepts two numbers as input and computes their
sum. The program aims to demonstrate basic input handling, arithmetic operation, and output
in Java.

Theory:- In Java, addition of two numbers involves taking two numeric inputs, performing
an arithmetic addition operation, and displaying the result. Java is a strongly typed, object-
oriented programming language that uses variables to store data values.

To add two numbers, you first declare variables to hold the values. These variables can be of
types like int (for integers) or float/double (for decimal numbers). After initializing or
accepting input for these variables, the + operator is used to add their values.

The program then outputs the result using [Link](), which prints the text or values
to the console. This simple program introduces fundamental concepts such as variable
declaration, input/output operations, and arithmetic expressions, which are essential in
learning Java programming.

Implementation:-

Program for “Addition of two numbers”-

class Addition

public static void main (String[] args){

int a= 30;

int b= 50;

int add;

add= a+b;

[Link](“Addition of a & b is : ” +add);

2410DMTCSE17966 MANSI PATIL


BTIT307N Introduction to core JAVA

Output:-

Quiz:-

1. Which operator is used in Java to add two numbers?

A) *
B) /
C) +
D) –

2. Which Java statement is used to display output on the screen?

A) echo()
B) [Link]()
C) [Link]()
D) [Link]()

3. What data type should be used to store whole numbers in Java?

A) String
B) int

2410DMTCSE17966 MANSI PATIL


BTIT307N Introduction to core JAVA

C) boolean
D) char

4. Which of the following is the correct way to declare two integers in Java?

A) int a, b;
B) integer a, b;
C) num a, b;
D) var int a, b;

5. How do you take input from the user in Java?

A) Scanner sc = new Scanner([Link]);


B) Input input = new Input();
C) [Link]();
D) [Link]();

Viva Questions:-

1. What is the purpose of your Java program?


2. Which data types can be used for storing numbers in Java?
3. What is the function of the + operator in Java?
4. How do you take input from the user in Java?
5. What is [Link]() used for?
6. Can you explain the flow of your program?
7. What happens if you use + with a number and a string?
8. What are the steps to compile and run a Java program?
9. Why do we use Scanner instead of BufferedReader in simple programs?
10. Can this program handle decimal numbers? How?

Lab Assignment:-

In this lab assignment, we are required to write a Java program that performs the addition of
two numbers. The program should take input from the user using the Scanner class, store the
values in appropriate variables, and use the + operator to compute the sum. Finally, the result
should be displayed using [Link](). This exercise helps us to understand basic
Java syntax, variable declaration, input/output operations, and arithmetic operators. It lays a
strong foundation for developing logic and working with user input in Java. Proper use of
data types and structure is expected in the solution.

2410DMTCSE17966 MANSI PATIL


BTIT307N Introduction to core JAVA

2410DMTCSE17966 MANSI PATIL

Common questions

Powered by AI

Testing the Java environment with a 'Hello World' program involves writing the simple code, compiling it with javac, and executing it with the java command to check for proper configuration and operation of the Java Development Kit (JDK). This process is significant because it verifies the installation and setup, ensuring the compiler is accessible and the JVM can execute Java applications. A successful run confirms that the environment supports subsequent development activities without basic configuration errors .

Java's strongly-typed system requires that all variables be declared with a specific data type, which defines what kind of data can be stored in them and how they can be used. This ensures type safety, meaning the operations performed on variables are compatible with their data types. In arithmetic expressions, this means that integer operations must involve int types, while operations requiring more precision use float or double types. The types also define the behavior of the operators, such as + for addition, ensuring that operations are semantically correct and helping to prevent common runtime errors .

Compiling and running a Java program through the command line involves several key steps: First, you write the program and save it as a .java file. Next, use the javac compiler command (e.g., javac HelloWorld.java) to compile the source code into bytecode, producing a .class file. Finally, execute the program using the java command (e.g., java HelloWorld) which runs the Java Virtual Machine to interpret the bytecode and execute the program. Each command — javac and java — plays a critical role in transforming code into an executable format and running it in a JVM environment .

In beginner programs, the Scanner class is often preferred over BufferedReader for input handling due to its simplicity and ease of use. Scanner provides methods to parse primitive types and strings using regular expressions, making it straightforward to capture input, which is beneficial when learning basic I/O operations in Java. BufferedReader, while faster and more efficient for reading lines, requires handling IOException and is generally more complex for beginners. Thus, Scanner is recommended for its simplicity in educational contexts .

In Java, using the + operator with a number and a string results in string concatenation rather than numerical addition. The operator converts the number to a string and appends it to the other string, leveraging Java's automatic type conversion from numbers to strings in mixed expressions. This behavior can lead to unexpected results if the programmer intends to perform arithmetic but accidentally concatenates, highlighting the need for careful type management in Java expressions .

In Java, the file name must match the class name, especially for public classes, because the Java compiler requires it for identifying the source file corresponding to the compiled class. This convention helps maintain code organization and prevents compilation errors, as the JVM looks for a file with the same name as the public class when executing the program. This rule ensures clarity and consistency across the Java project's directory structure .

The main method, denoted as public static void main(String[] args), is crucial because it serves as the entry point for any standalone Java application. It needs to be declared as static to allow the Java Virtual Machine (JVM) to invoke it without creating an instance of the class. This static context is essential for the JVM to initiate the execution process efficiently .

Setting up the Java development environment involves installing the Java Development Kit (JDK), setting up the PATH environment variable, and ensuring the compiler and runtime are functional. This setup is crucial for compiling and executing Java programs. It enables efficient translation of high-level code into bytecode, which the JVM can execute, thereby allowing developers to effectively test and run Java applications using 'Hello World' and more complex programs, ensuring the tools and libraries required for development are in place .

The 'Hello World' program is considered important as it marks the initiation into a new programming language, providing the simplest example to understand the basic syntax and structure of Java. It introduces fundamental concepts such as the definition of a class, the function of the main method as the entry point, and the use of System.out.println() to display output. Additionally, it helps test the Java development environment setup, ensuring the installation of JDK, the compiler, and runtime are properly configured .

In Java, adding two numbers involves declaring variables to store these values. The variables are assigned a data type, such as int for whole numbers or float/double for decimal numbers, which determines the nature of the values they can hold. The + operator is used to perform the addition and the result is displayed using System.out.println(). The choice of data type is critical as it ensures the variables can hold the necessary range and type of values for the operation .

You might also like