Skip to main content

ProwessApps

Learn · Practice · Excel

Java Program to display "Hello World!" (enclosed in double quotes).

Java Code Example — OOP Programs

ADVERTISEMENT

Java Program to display "Hello World!" (enclosed in double quotes).

Objective

Write a Java program to display "Hello World!" by creating an object of a class and calling its method.

Algorithm / Approach

  1. Create a class named Test.
  2. Inside the class, define a method named display().
  3. Inside display(), use System.out.print() to print the literal string "Hello World!" including double quotes.
  4. Create the main method.
  5. Inside main, instantiate an object of the Test class: Test t = new Test();.
  6. Call the display method using the object: t.display();.
Test.java
class Test {
 void display() {
  System.out.print("\"Hello World!\"");
 }
 public static void main(String[] a) 
 {
  Test t = new Test();
  t.display();
 }
}

Expected Output

"Hello World!"

Explanation of the Program

  • This program introduces the most basic concept of Object-Oriented Programming (OOP) in Java.
  • Instead of writing logic directly inside the main method, we define a behavior (the display method) inside a blueprint (the Test class).
  • To use that behavior, we must first create an instance of the class (an object) using the new keyword.
  • To print literal double quotes inside a string, we escape them using a backslash \".

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT