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
Ad

More Related Content

Similar to Java Primitive Type from Long Nguyen's lecture (20)

ch01-basic-java-programs.ppt
ch01-basic-java-programs.pptch01-basic-java-programs.ppt
ch01-basic-java-programs.ppt
Mahyuddin8
 
Java Notes
Java Notes Java Notes
Java Notes
Sreedhar Chowdam
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
Sreedhar Chowdam
 
Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manual
nahalomar
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1
Mohamed Essam
 
Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
mrinalbhutani
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
Zeeshan MIrza
 
Programming in Java: Getting Started
Programming in Java: Getting StartedProgramming in Java: Getting Started
Programming in Java: Getting Started
Martin Chapman
 
Ch01 basic-java-programs
Ch01 basic-java-programsCh01 basic-java-programs
Ch01 basic-java-programs
James Brotsos
 
OOP-Chap2.docx
OOP-Chap2.docxOOP-Chap2.docx
OOP-Chap2.docx
NaorinHalim
 
Topic2IntroductionToJavaProgramming with concepts
Topic2IntroductionToJavaProgramming with conceptsTopic2IntroductionToJavaProgramming with concepts
Topic2IntroductionToJavaProgramming with concepts
rajipe1
 
CORE JAVA
CORE JAVACORE JAVA
CORE JAVA
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Topic2IntroductionToJavaProgramming.ppt
Topic2IntroductionToJavaProgramming.pptTopic2IntroductionToJavaProgramming.ppt
Topic2IntroductionToJavaProgramming.ppt
ralph581247
 
Compiling and understanding first program in java
Compiling and understanding first program in javaCompiling and understanding first program in java
Compiling and understanding first program in java
Jamsher bhanbhro
 
OOPM Introduction.ppt
OOPM Introduction.pptOOPM Introduction.ppt
OOPM Introduction.ppt
vijay251387
 
Unit2 java
Unit2 javaUnit2 java
Unit2 java
mrecedu
 
INTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATIONINTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATION
Ajit Yadav
 
Java lab-manual
Java lab-manualJava lab-manual
Java lab-manual
Khurshid Asghar
 
Introduction to java programming part 1
Introduction to java programming   part 1Introduction to java programming   part 1
Introduction to java programming part 1
university of education,Lahore
 
Core java introduction
Core java introduction Core java introduction
Core java introduction
Som Prakash Rai
 
ch01-basic-java-programs.ppt
ch01-basic-java-programs.pptch01-basic-java-programs.ppt
ch01-basic-java-programs.ppt
Mahyuddin8
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
Sreedhar Chowdam
 
Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manual
nahalomar
 
Intro to programing with java-lecture 1
Intro to programing with java-lecture 1Intro to programing with java-lecture 1
Intro to programing with java-lecture 1
Mohamed Essam
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
Zeeshan MIrza
 
Programming in Java: Getting Started
Programming in Java: Getting StartedProgramming in Java: Getting Started
Programming in Java: Getting Started
Martin Chapman
 
Ch01 basic-java-programs
Ch01 basic-java-programsCh01 basic-java-programs
Ch01 basic-java-programs
James Brotsos
 
OOP-Chap2.docx
OOP-Chap2.docxOOP-Chap2.docx
OOP-Chap2.docx
NaorinHalim
 
Topic2IntroductionToJavaProgramming with concepts
Topic2IntroductionToJavaProgramming with conceptsTopic2IntroductionToJavaProgramming with concepts
Topic2IntroductionToJavaProgramming with concepts
rajipe1
 
Topic2IntroductionToJavaProgramming.ppt
Topic2IntroductionToJavaProgramming.pptTopic2IntroductionToJavaProgramming.ppt
Topic2IntroductionToJavaProgramming.ppt
ralph581247
 
Compiling and understanding first program in java
Compiling and understanding first program in javaCompiling and understanding first program in java
Compiling and understanding first program in java
Jamsher bhanbhro
 
OOPM Introduction.ppt
OOPM Introduction.pptOOPM Introduction.ppt
OOPM Introduction.ppt
vijay251387
 
Unit2 java
Unit2 javaUnit2 java
Unit2 java
mrecedu
 
INTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATIONINTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATION
Ajit Yadav
 
Core java introduction
Core java introduction Core java introduction
Core java introduction
Som Prakash Rai
 

Recently uploaded (20)

spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM & Mia eStudios
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM & Mia eStudios
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Ad

Java Primitive Type from Long Nguyen's lecture

  • 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