Program 1: Hello World in Java
A simple Java program that prints “Hello, World!” to the console. This program serves as an introduction to Java programming and demonstrates the basic structure of a Java application.
Algorithm
Step 1: Start
Step 2: Define a public class named HelloWorld.
Step 3: Within the class, define the main method with the signature public
static void main(String[] args).
Step 4: Inside the main method, use System.out.println to print the string
"Hello, World!" to the console.
Step 5: Exit
Program in Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Step-by-Step Explanation:
Class Declaration:
public class HelloWorld {
- public: An access modifier that means the class is accessible by any other class.
- class: This keyword is used to declare a class.
- HelloWorld: The name of the class. It must match the filename (HelloWorld.java).
Main Method Declaration:
public static void main(String[] args) {
- public: The method is accessible from outside the class.
- static: This means that the method belongs to the class, not instances of the class. It can be called without creating an object of the class.
- void: The method does not return any value.
- main: The name of the method. It is the entry point for any Java program.
- String[ ] args: The parameter args is an array of String objects, which allows command-line arguments to be passed to the program.
Print Statement:
System.out.println("Hello, World!");
- System: A built-in class in the java.lang package.
- out: A static member of the System class, which is an instance of PrintStream.
- println: A method of PrintStream that prints the argument passed to it, followed by a new line. In this case, it prints “Hello, World!”.
Summary:
The HelloWorld program is a simple Java application that consists of a single class, HelloWorld. Within this class, there is a main method, which is the entry point of the program.
When the program runs, it executes the main method, which contains a single statement that prints “Hello, World!” to the console. This basic program demonstrates the structure of a Java application and how to output text to the console.
Program 2: Hello World in JavaScript
A simple JavaScript program that prints “Hello, World!” to the web page or console. This program introduces basic JavaScript syntax and the use of console.log.
Algorithm
Step 1: Start
Step 2: Use the console.log function to print the string "Hello, World!" to
the console.
Step 3: Exit
Program in Java
// Printing Hello, World! to the console
console.log("Hello, World!");
Explanation:
- console: This is an object provided by the JavaScript runtime (such as browsers and Node.js) that provides access to the browser’s debugging console.
- log: This is a method of the console object that outputs a message to the console.
- “Hello, World!”: This is a string literal, which is the message that will be printed to the console.;: This semicolon denotes the end of the statement. In JavaScript, semicolons are used to terminate statements, though they are optional in many cases.
Summary:
The provided JavaScript code is a simple script that prints “Hello, World!” to the console. It consists of a single-line comment that describes the purpose of the code, followed by a console.log statement that outputs the string “Hello, World!” to the console. This example illustrates the basic syntax of JavaScript for logging messages and using comments.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.