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

Hello World

The document serves as an introduction to programming, specifically focusing on Java, covering essential topics such as elements of programming, functions, object-oriented programming (OOP), data structures, and the theory of computing. It provides step-by-step instructions for creating, compiling, and executing a simple Java program, along with common error types and exercises for practice. Additionally, it includes web resources and related courses to further enhance learning in programming and computer science.

Uploaded by

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

Hello World

The document serves as an introduction to programming, specifically focusing on Java, covering essential topics such as elements of programming, functions, object-oriented programming (OOP), data structures, and the theory of computing. It provides step-by-step instructions for creating, compiling, and executing a simple Java program, along with common error types and exercises for practice. Additionally, it includes web resources and related courses to further enhance learning in programming and computer science.

Uploaded by

jatindahiya077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Intro to Programming

1. Elements of Programming
1.1 Your First Program
1.2 Built-in Types of Data
1.3 Conditionals and Loops
1.4 Arrays
1.5 Input and Output
1.6 Case Study: PageRank
2. Functions
2.1 Static Methods
2.2 Libraries and Clients
2.3 Recursion
2.4 Case Study: Percolation
3. OOP
3.1 Using Data Types
3.2 Creating Data Types
3.3 Designing Data Types
3.4 Case Study: N-Body
4. Data Structures
4.1 Performance
4.2 Sorting and Searching
4.3 Stacks and Queues
4.4 Symbol Tables
4.5 Case Study: Small World

Computer Science
5. Theory of Computing
5.1 Formal Languages
5.2 Turing Machines
5.3 Universality
5.4 Computability

https://introcs.cs.princeton.edu/java/11hello/ 24/01/25, 9 38 PM
Page 1 of 7
:
5.5 Intractability
9.9 Cryptography
6. A Computing Machine
6.1 Representing Info
6.2 TOY Machine
6.3 TOY Programming
6.4 TOY Virtual Machine
7. Building a Computer
7.1 Boolean Logic
7.2 Basic Circuit Model
7.3 Combinational Circuits
7.4 Sequential Circuits
7.5 Digital Devices

Beyond
8. Systems
8.1 Library Programming
8.2 Compilers
8.3 Operating Systems
8.4 Networking
8.5 Applications Systems
9. Scientific Computation
9.1 Floating Point
9.2 Symbolic Methods
9.3 Numerical Integration
9.4 Differential Equations
9.5 Linear Algebra
9.6 Optimization
9.7 Data Analysis
9.8 Simulation

Related Booksites

Web Resources
FAQ
Data
Code
Errata
Lectures
Appendices

https://introcs.cs.princeton.edu/java/11hello/ 24/01/25, 9 38 PM
Page 2 of 7
:
A. Operator Precedence
B. Writing Clear Code
C. Glossary
D. TOY Cheatsheet
E. Matlab
Coursera MOOCs
Programming with a Purpose
Algorithms, Theory, Machines
Certificate Courses
Programming with a Purpose
Algorithms, Theory, Machines
Java Cheatsheet
Programming Assignments

1.1 Your First Java Program: Hello World

In this section, our plan is to lead you into the world of Java programming by taking you through
the three basic steps required to get a simple program running. As with any application, you need
to be sure that Java is properly installed on your computer. You also need an editor and a terminal
application. Here are system specific instructions for three popular home operating systems. [
Mac OS X · Windows · Linux ]

Programming in Java.
We break the process of programming in Java into three steps:

1. Create the program by typing it into a text editor and saving it to a file named, say,
MyProgram.java.
2. Compile it by typing "javac MyProgram.java" in the terminal window.
3. Execute (or run) it by typing "java MyProgram" in the terminal window.

The first step creates the program; the second translates it into a language more suitable for
machine execution (and puts the result in a file named MyProgram.class); the third actually runs
the program.

https://introcs.cs.princeton.edu/java/11hello/ 24/01/25, 9 38 PM
Page 3 of 7
:
Creating a Java program. A program is nothing more than a sequence of characters, like a
sentence, a paragraph, or a poem. To create one, we need only define that sequence
characters using a text editor in the same way as we do for email. HelloWorld.java is an
example program. Type these character into your text editor and save it into a file named
HelloWorld.java.

public class HelloWorld {


public static void main(String[] args) {
// Prints "Hello, World" in the terminal window.
System.out.println("Hello, World");
}
}

Compiling a Java program. A compiler is an application that translates programs from the
Java language to a language more suitable for executing on the computer. It takes a text file
with the .java extension as input (your program) and produces a file with a .class
extension (the computer-language version). To compile HelloWorld.java type the
boldfaced text below at the terminal. (We use the % symbol to denote the command prompt,
but it may appear different depending on your system.)

% javac HelloWorld.java

If you typed in the program correctly, you should see no error messages. Otherwise, go
back and make sure you typed in the program exactly as it appears above.
Executing (or running) a Java program. Once you compile your program, you can execute
it. This is the exciting part, where the computer follows your instructions. To run the
HelloWorld program, type the following in the terminal window:

% java HelloWorld

If all goes well, you should see the following response

Hello, World

Understanding a Java program. The key line with System.out.println() prints the text
"Hello, World" in the terminal window. When we begin to write more complicated
programs, we will discuss the meaning of public, class, main, String[], args,
System.out, and so on.
Creating your own Java program. For the time being, all of our programs will be just like
HelloWorld.java, except with a different sequence of statements in main(). The easiest

https://introcs.cs.princeton.edu/java/11hello/ 24/01/25, 9 38 PM
Page 4 of 7
:
way to write such a program is to:
Copy HelloWorld.java into a new file whose name is the program name followed
by .java.
Replace HelloWorld with the program name everywhere.
Replace the print statement by a sequence of statements.

Errors.
Most errors are easily fixed by carefully examining the program as we create it, in just the same
way as we fix spelling and grammatical errors when we type an e-mail message.

Compile-time errors. These errors are caught by the system when we compile the program,
because they prevent the compiler from doing the translation (so it issues an error message
that tries to explain why).
Run-time errors. These errors are caught by the system when we execute the program,
because the program tries to perform an invalid operation (e.g., division by zero).
Logical errors. These errors are (hopefully) caught by the programmer when we execute
the program and it produces the wrong answer. Bugs are the bane of a programmer's
existence. They can be subtle and very hard to find.

One of the very first skills that you will learn is to identify errors; one of the next will be to be
sufficiently careful when coding to avoid many of them.

Input and output.


Typically, we want to provide input to our programs: data that they can process to produce a
result. The simplest way to provide input data is illustrated in UseArgument.java. Whenever this
program is executed, it reads the command-line argument that you type after the program name
and prints it back out to the terminal as part of the message.

% javac UseArgument.java
% java UseArgument Alice
Hi, Alice. How are you?
% java UseArgument Bob
Hi, Bob. How are you?

Exercises

1. Write a program TenHelloWorlds.java that prints "Hello, World" ten times.


5. Modify UseArgument.java to make a program UseThree.java that takes three names and
prints out a proper sentence with the names in the reverse of the order given, so that for
example, "java UseThree Alice Bob Carol" gives "Hi Carol, Bob, and Alice.".

https://introcs.cs.princeton.edu/java/11hello/ 24/01/25, 9 38 PM
Page 5 of 7
:
Web Exercises

1. Write a program HelloWorldMultilingual.java that prints "Hello World!" in as many human


languages as you can.
2. Write a program Initials.java that prints your initials using nine rows of asterisks like the
one below.

** *** ********** ** * **
** *** ** ** ** *** **
** *** ** ** ** ** ** **
** *** ** ** ** ** ** **
***** ** ** ** ** ** **
** *** ** ** ** ** ** **
** *** ** ** ** ** ** **
** *** ** ** *** ***
** *** ********** * *

3. Describe what happens if, in HelloWorld.java, you omit


1. main
2. String
3. HelloWorld
4. System.out
5. println
4. Describe what happens if, in HelloWorld.java, you omit
1. the ;
2. the first "
3. the second "
4. the first {
5. the second {
6. the first }
7. the second }
5. Describe what happens if, in HelloWorld.java, you misspell (by, say, omitting the second
letter)
1. main
2. String
3. HelloWorld
4. System.out
5. println
6. I typed in the following program. It compiles fine, but when I execute it, I get the error
java.lang.NoSuchMethodError: main. What am I doing wrong?

public class Hello {


public static void main() {

https://introcs.cs.princeton.edu/java/11hello/ 24/01/25, 9 38 PM
Page 6 of 7
:
System.out.println("Doesn't execute");
}
}

Answer: you forgot the String[] args. It is required.

Last modified on June 10, 2022.

Copyright © 2000–2025 Robert Sedgewick and Kevin Wayne. All rights reserved.

https://introcs.cs.princeton.edu/java/11hello/ 24/01/25, 9 38 PM
Page 7 of 7
:

You might also like