SlideShare a Scribd company logo
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

PPT
Ch01 basic-java-programs
PPT
ch01-basic-java-programs.ppt
PPT
01-ch01-1-println.ppt java introduction one
PPT
Java
DOCX
Unit of competency
PPT
JAVA Programming notes.ppt
PDF
Java Basics.pdf
PPTX
Intro to programing with java-lecture 1
Ch01 basic-java-programs
ch01-basic-java-programs.ppt
01-ch01-1-println.ppt java introduction one
Java
Unit of competency
JAVA Programming notes.ppt
Java Basics.pdf
Intro to programing with java-lecture 1

Similar to Unit 1: Primitive Types - Basic Java Syntax (20)

PDF
Java Programming
PPT
pptTopic2IntroductionToJavaProgramming.ppt
PPT
Topic2IntroductionToJavaProgramming with concepts
PPT
Java PPt.ppt
PPT
00_Introduction to Java.ppt
PPTX
Chapter 2.4
PPT
IntroductionToJavaProgrammingxcvxcvx.ppt
PPT
java01.ppt
PDF
computer science cousre related to python
PPT
CSL101_Ch1.ppt Computer Science
PPT
Programming with Java by Faizan Ahmed & Team
PPT
Introduction to java programming with Fundamentals
PPTX
CSE 116 OOP Educational Materials of United International University
PDF
java 1 new.pdf
PPTX
Multi Threading- in Java WPS Office.pptx
DOCX
Java cheat sheet
PPT
Java Concepts with object oriented programming
PPT
java programming for engineering students_Ch1.ppt
PPT
Mobile computing for Bsc Computer Science
PPT
Programming with Java - Essentials to program
Java Programming
pptTopic2IntroductionToJavaProgramming.ppt
Topic2IntroductionToJavaProgramming with concepts
Java PPt.ppt
00_Introduction to Java.ppt
Chapter 2.4
IntroductionToJavaProgrammingxcvxcvx.ppt
java01.ppt
computer science cousre related to python
CSL101_Ch1.ppt Computer Science
Programming with Java by Faizan Ahmed & Team
Introduction to java programming with Fundamentals
CSE 116 OOP Educational Materials of United International University
java 1 new.pdf
Multi Threading- in Java WPS Office.pptx
Java cheat sheet
Java Concepts with object oriented programming
java programming for engineering students_Ch1.ppt
Mobile computing for Bsc Computer Science
Programming with Java - Essentials to program
Ad

Recently uploaded (20)

PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PDF
Pre independence Education in Inndia.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Insiders guide to clinical Medicine.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Week 4 Term 3 Study Techniques revisited.pptx
Cell Types and Its function , kingdom of life
Renaissance Architecture: A Journey from Faith to Humanism
Pharmacology of Heart Failure /Pharmacotherapy of CHF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
Pre independence Education in Inndia.pdf
Supply Chain Operations Speaking Notes -ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Insiders guide to clinical Medicine.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPH.pptx obstetrics and gynecology in nursing
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
O7-L3 Supply Chain Operations - ICLT Program
Ad

Unit 1: Primitive Types - Basic Java Syntax

  • 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