CS 46A - Lecture 2
Pre-class reading
- Section 1.4 - 1.7
- Did you take the quiz?
The Java Programming Language
- Simple
- Mostly—a few issues are vexing for beginners
- Safe
- Virtual machine diagnoses program errors instead of causing random
behavior
- Great for learning
- Platform-independent (
write once, run anywhere
)
- You can use Windows, Mac, Linux on your laptop
- Rich library
- Many built-in features
- Many third-party libraries
- Great tools
Our First Program
public class HelloPrinter
{
public static void main(String[] args)
{
// Display a greeting in the console window
System.out.println("Hello, World!");
}
}
Program Run:
The Structure of a Simple Program: Class Declaration
The Structure of a Simple Program: main Method
The Structure of a Simple Program: Comment
The Structure of a Simple Program: Statements
The Structure of a Simple Program: Method Call
Syntax 1.1 Method Call
The Structure of a Simple Program: Strings
- String: a sequence of characters enclosed in double
quotation marks:
"Hello, World!"
Lecture 2 Clicker Question 1
What does this program print?
public class HelloPrinter
{
public static void main(String[] args)
{
// Display Hello World
System.out.println("Hello");
System.out.println("World");
}
}
Hello World
Hello
World
Hello
World
Hello World
- Something else
Writing, Compiling and Running a Java Program
- Use an editor to enter and modify the program text
- The Java compiler translates source code into class
files that contain instructions for the Java virtual machine
- A class file has extension .class
- The compiler does not produce a class file if it has found errors in your
program
- The Java virtual machine loads instructions from the program's
class file, starts the program, and loads the necessary library files as
they are required
From Source Code to Running Program
Errors
- Compile-time error: A violation of the programming
language rules that is detected by the compiler
- Example:
System.ou.println("Hello, World!);
- Run-time error: Causes the program to take an action
that the programmer did not intend
- Examples:
System.out.println("Hello, Word!");
System.out.println(1/0);
Lecture 2 Clicker Question 2
What is true about this program:
public class HelloPrinter
{
public static void main(String[] args)
{
Display Hello World
System.out.println("Hello World);
}
}
- The program has no syntax errors and no run-time errors
- The program has no syntax errors and one run-time error
- The program has one syntax error
- The program has two syntax errors
Lecture 2 Clicker Question 3
What is wrong with this program?
public class HelloPrinter
{
public static void main(String[] args)
{
// Display Hello World!
System.out.println("Hello World!")
}
}
- There should have been a semicolon at the end of the first line
- There should have been a comma after Hello
- There should have been a semicolon at the end of the sixth line
- There is nothing wrong with the program
Lecture 2 Clicker Question 4
Which part of the program contains the first error that is reported
when you try to compile this program?
public class Hell0Printer
{
public static void main(String[] args)
{
// Display Hell0, W0rld!
System.0ut.println("Hell0, W0rld!");
}
}
public class Hell0Printer
// Display Hell0 W0rld!
System.0ut.println
"Hell0 W0rld!"
Lecture 2 Clicker Question 5
Which identifier is a cause of the first error that is reported
when you try to compile this program?
public Class HelloPrinter
{
public static void Main(string[] args)
{
// Display a greeting in the console window
system.out.println("hello, world!");
}
}
Class
Main
string
system
Tip: Download the Program Code
- It's on the book companion site (link on course page)
- Unzip somewhere so you can find it during homeworks/exams
- Copy/paste into BlueJ
- Don't type what you can copy/paste
- Not plagiarism since it's from the book
Tip: Understand the File System
- Folders (also called directories) contain files
- A file stores some data
- A Java program is a file (such as HelloWorld.java)
- It must be inside some folder
- You want to know where it is
- Windows hides folder structure to help naïve users
- Computer scientists are not naïve users
Tip: Know These Folders
- Home directory
c:\Users\Yourname
, /Users/yourname
,
/home/yourname
- Download directory
- Usually,
Downloads
inside the home directory
- You can choose it in your browser configuration
- Desktop
- Just kidding
- Experts don't use the desktop because it is completely covered with
the windows that we need for our work
- If your browser downloads to the desktop, reconfigure it
Tip: Make These Folders
- Course directory
- You need to make one
- Good choice:
sjsu/cs46a
inside your home directory
- That is, a folder
sjsu
inside your home directory, and
another folder cs46a
inside that
- Use lowercase letters and NO SPACES in directory
names
- Book code
- Put it in
sjsu/cs46a/book
- A separate directory for each assignment, lab, etc
sjsu/cs46a/lab1
sjsu/cs46a/hw1a
- In BlueJ
- Use Project -> New Project and give project name (such as
hw1a
). BlueJ will make the folder
- Use Project -> Open Non-BlueJ to open a book example
Lecture 2 Clicker Question 6
You want to start working on Homework 1. Into which folder should you put
it?
- On the desktop
Homework 1
inside My Documents
hw1
inside the home directory
hw1a
(or hw1b
or hw1c
) inside
cs46a