1. Unit 1: Primitive Types Basic
Java Syntax
Adapted from:
1) Building Java Programs: A Back to Basics Approach
by Stuart Reges and Marty Stepp
2) Runestone CSAwesome Curriculum
https://longbaonguyen.github.io
This work is licensed under the
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
2. 2
Java
What do Minecraft, Android phones, and Netflix have in
common? They’re all programmed in Java!
Many of the apps you use in an Android phone or tablet are
also written in Java. Netflix uses Java for some of its software
too. Java is used worldwide to create software that we all use.
3. 3
Java
Java is a programming language, which means that we can
use Java to tell a computer what to do.
Computers don’t actually speak Java so we have
to compile (translate) Java source files (they end in .java) into
class files (they end in .class).
The source file is something humans can read and edit, and the
class file is code that a computer can understand and can run.
4. 4
Java Terminology
All Java code are organized into units called classes.
class:
(a) A module or program that can contain executable
code.
(b) A description of a type of objects. (Animal class,
Human class, Employee class, Car class)
statement: An executable piece of code that represents a
complete command to the computer.
– every basic Java statement ends with a semicolon ;
method: A named sequence of statements that can be executed
together to perform a particular action or computation.
5. 5
Structure of a Java program
public class name {
public static void main(String[] args) {
statement;
;
statement;
;
...
...
statement;
;
}
}
• Every executable Java program consists of a class, called
the driver class,
– that contains a method named main,
• that contains the statements (commands) to be executed.
class: a program
statement: a command to be executed
method: a named group
of statements
6. 6
Program Template
Here's a simple program that prints out a message on the screen. Code
highlighted in red should be in every program!
public class Main{
public static void main(String[] args){
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
7. 7
First Program: repl.it
We will use repl.it, an online integrated development
environment(IDE), for the first part of this course to write all
of our code.
console: Text box into which
the program's output is printed.
Click on “run” to compile and
run your code!
8. 8
File naming
The name of the class has to match up with the name of the
file.
For example, the class below is called Main therefore the
name of the file is Main.java. On replit, the main class must
be called Main.java.(in other IDEs, you can pick any name.)
9. 9
Printing
Two ways to print a line of output on the console:
System.out.println() and System.out.print().
System.out.println() is just the way that you ask Java to
print out the value of something followed by a new line (ln).
System.out.print() without the ln will print out
something without advancing to the next new line.
10. 10
System.out.println
public class Welcome{
public static void main(String[] args){
System.out.println("Hi there!");
System.out.println(”Welcome to APCS A!");
}
}
Output:
Hi There!
Welcome to APCS A!
The “System” in System.out.println() must be capitalized. And
the command line must end with a semicolon (;).
11. 11
System.out.print
public class SecondClass{
public static void main(String[] args){
System.out.print("Hi there!");
System.out.println(”Welcome to APCS A!");
System.out.print(”We will learn Java!");
}
}
Output:
Hi There!Welcome to APCS A!
We will learn Java!
Do you see why there are two lines of output as above?
12. 12
Find the errors.
pooblic class Errors
public static void main(String args){
System.out.print("Good morning! ")
system.out.print("Good afternoon!);
System.Print "And good evening!";
}
See next slide for all of the corrections.
13. 13
Corrected!
public class Errors {
public static void main(String[] args){
System.out.print("Good morning! ");
System.out.print("Good afternoon!”);
System.out.print("And good evening!”);
}
}
14. 14
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."
A string enclosed in quotes
is called a string literal.
15. 15
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. */
16. 16
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.
17. 17
Comments example
/* Suzy Student, CS 101, Fall 2019
This program prints lyrics about ... something. */
public class BaWitDaBa {
public static void main(String[] args) {
// first verse
System.out.println("Bawitdaba");
System.out.println("da bang a dang diggy diggy");
System.out.println();
// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}
18. 18
Indent Nicely!
public class Welcome{ public static void main(String[]
args){ System.out.println("Hi there!”
);System.out.println(”Welcome to APCS A!");}}
The code above will compile and run correctly. Java ignore whitespaces.
But it is very hard to read, please make an effort to indent nicely!
public class Welcome{
public static void main(String[] args){
System.out.println("Hi there!");
System.out.println(”Welcome to APCS A!");
}
}
19. 19
Lab 1
Create a new repl on your repl.it account and write a
program that has the following outputs:
You must use exactly 5 different print statements.(println
and/or print).
Output:
I am Sam. Sam I am. I do not like them, Sam-I-am.
I do not like green eggs and ham.
20. 20
References
For more tutorials/lecture notes in Java, Python, game
programming, artificial intelligence with neural networks:
https://longbaonguyen.github.io
1) Building Java Programs: A Back to Basics Approach by Stuart Reges and
Marty Stepp
2) Runestone CSAwesome Curriculum:
https://runestone.academy/runestone/books/published/csawesome/index.html