Programming 1
Lecture 2
Introduction to Java
Programming
1
A Java Program:
Output:
2
A Java Program:
• Package:
Group of classes.
• Class:
1. Data items
2. Methods ( or functions or operations)
• The main method is the entry point to the program.
• Java is case sensitive language.
• Console window:
Text box into which the program's output is printed.
3
Structure of a Java program
class: a program
public class name {
public static void main(String[] args) {
Body of
statement;
statement;
main method: a named group
method ...
statement; of statements
}
}
statement: a command to be executed
• Every executable Java program consists of a class,
• that contains a method named main,
• that contains the statements (commands) to be
executed.
[Link] Statement:
• A statement that prints a line of output on the console.
• Two ways to use [Link] :
• [Link]("text");
Prints the given message as output.
• [Link]();
Prints a blank line of output.
5
Example:
public class Hello {
public static void main(String[] args) {
[Link]("Hello, world!");
[Link]();
[Link]("This program produces");
[Link]("four lines of output");
}
}
Output:
Hello, world!
This program produces
four lines of output
6
Compile/run a program
1. Write it.
• code or source code: The set of instructions in a program.
2. Compile it.
• javac: translates the program from Java to bytecode
• bytecode: runs on many computer types (any computer
with JVM)
3. Run (execute) it.
• output: whatever the programmer instructs the program
to do
source code byte code output
compile run Hello, World!
javac [Link] java Hello
Identifier:
A name given to an item in your program.
• Must start with a letter or _ or $
• Do not use space or any special character.
• Do not use any reserved name like (void, class, int)
• Do not start with a number.
• legal:
_myName TheCure ANSWER_IS_42 $bling$
• illegal:
me+u 49ers side-swipe Ph.D’s
Example:
public class MyClass {
8
Syntax:
• Syntax: The set of legal structures and commands that can
be used in a particular language.
• Every basic Java statement ends with a semicolon ;
• The contents of a class or method occur between { and }
9
Programming Errors:
1- Syntax Errors:
• Detected by the compiler.
2- Runtime Errors:
• Causes the program to abort.
10
Programming Errors:
3- Logic Errors:
• Produces incorrect results.
11
Strings:
• string: A sequence of characters to be printed.
• Starts and ends with a " quote " character.
• The quotes do not appear in the output.
• Examples:
"hello"
"This is a string. It's very long!"
Restrictions:
• May not span multiple lines.
"This is not
a legal String."
• May not contain a " character.
"This is not a "legal" String either."12
Escape sequence:
• Escape Sequence: A special sequence of characters used
to represent certain special characters in a string.
\t tab character
\n new line character
\" quotation mark character
\\ backslash character
• Example:
[Link]("\\hello\nhow\tare \"you\"?\\\\");
• Output:
\hello
how are "you"?\\
13
Questions
• What is the output of the following println statements?
[Link]("\ta\tb\tc");
[Link]("\\\\");
[Link]("'");
[Link]("\"\"\"");
[Link]("C:\nin\the downward
spiral");
Answer:
a b c
\\
'
"""
C:
in he downward spiral
Questions
• Write a println statement to produce this output:
/ \ // \\ /// \\\
Answer:
[Link]("/ \\ // \\\\ /// \\\\\\");
Comments:
• Comment: A note written in source code by the
programmer to describe or clarify the code.
• Comments are not executed when your program runs.
• Syntax:
// comment text, on one line
or,
/* comment text; may span multiple lines */
• Examples:
// This is a one-line comment.
/* This is a very long
multi-line comment. */
Using comments
• Where to place comments:
• at the top of each file (a "comment header")
• at the start of every method (seen later)
• to explain complex pieces of code
• Comments are useful for:
• Understanding larger, more complex programs.
• Multiple programmers working together, who
must understand each other's code.
Comments Example
/* Suzy Student, Fall 2022
This program prints lyrics about ... something.
*/
public class BaWitDaBa {
public static void main(String[] args) {
// first verse
[Link]("Bawitdaba");
[Link]("da bang a dang diggy
diggy");
[Link]();
// second verse
[Link]("diggy said the
boogy");
[Link]("said up jump the
boogy");
}
}
print vs println:
println
print