COMP 202 Lecture Notes - Lecture 2: Bracket, Semicolon, Computer Programming

58 views8 pages
COMP 202: Foundations of Programming
Lecture 2: Intro Pt. II and Variables + Data Types
First Java Program:
public class HelloWorld
{
public class void main (String [] args)
{
System.out.println(“Hello World!”);
}
}
Curly Brackets:
Curly brackets denote “blocks” of code, which helps us keep track of which parts of code are related to each
other. Each opening { is paired with a closing }
Class: one of the key units of organization in Java. In the HelloWorld example, the class is public, and defined
by the outermost {}
→ public class HelloWorld
Methods: another key unit of organization, and is a chunk of code that performs a specific task. A main
method is a very particular type of method.
→ public class void main (String [] args)
When running a Java program, you are actually running a Java class. Execution of the program will start at the
beginning of the method called “main” inside the class that you run.
Statements: inside a method, you can put many statements or commands.
- All statements in Java end with a semi-colon
- “” indicates a String of text
- In this example, the statement prints the content in between the “” to the screen
→ System.out.println(“Hello World!”);
println (print line) is a method that was written by someone else, and put in a package for others to use.
A package is a collection of code that someone else wrote that they allow other people to use – Java has many
packages.
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
COMP 202: Foundations of Programming
System.out.print() prints whatever is in the parenthese
System.out.println() prints whatever is in the parentheses, and skips to the next line after printing
There are three different types of coding “lines” we can write:
1. Code that defines where a block starts and ends – this line either ends with an open curly bracket, or the
whole line is a single closed curly bracket
→ public class HelloWorld {
→ }
2. A line of code that does something. These are statements and must end with a semi-colon.
→ System.out.println(“Hello World!”);
3. A comment – can be either a single line comment or a multi-line comment. A single line comment starts
with // and a multi-line comment starts with /* and ends with */
→ // Comment
→ /* Comment Line 1
Comment Line 2 */
A method in Java can use as many statements as you want, and the computer will execute this from top to
bottom.
Code Structure:
Rules about structure exist to make code more readable, and these rules include when to have new lines and
how to indent things.
New Line (or, Press Enter):
1. When an open curly bracket ({)
2. When a close curly bracket (})
3. Statement ending with a semi-colon (;)
Indent:
1. When you start a new block ({), all subsequent lines should be indented (tabbed)
2. When ending a block (}), all subsequent lines should be un-indented (untabbed)
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
COMP 202: Foundations of Programming
Escape Characters:
Within a String, we can use escape sequences for punctuation or spacing.
\n New Line
\t Tab
\” To have double quotes within your string (i.e. when printing a quote)
\\ To “escape” the escape character, if you ever have to print the symbols for an escape character.
Comments: Annotations that can be included in your code to explain what it does and make it more
understandable.
Variables in Java:
A variable is a place in computer memory where you can store data and access it later on.
- Must have a name, so you can identify to the computer which memory “place” you are talking about
- It must also have a type, so that the computer can know how much memory to allocate to this particular
variable
- Could be said that a variable is a “typed label for a space of memory”
Why Variables in Java?
- Code readability
- Code generalizability
- Temporary storage for partial results
The Stages of a Variable:
1. Declaration
2. Initialization
3. Manipulation
Variable Declaration
When you declare a
variable, you give it a
name and a type.
→ int
myNumber;
Here, int is the type of
variable, and myNumber
is the name. int is
what is known as a
keyword (a reserved word in Java), and is short for integer.
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
hwpark22 and 40159 others unlocked
COMP 202 Full Course Notes
100
COMP 202 Full Course Notes
Verified Note
100 documents

Document Summary

Ii and variables + data types public class void main (string [] args) First java program: public class helloworld public class helloworld. Curly brackets denote blocks of code, which helps us keep track of which parts of code are related to each other. Each opening { is paired with a closing } Class: one of the key units of organization in java. In the helloworld example, the class is public, and defined by the outermost {} Methods: another key unit of organization, and is a chunk of code that performs a specific task. A main method is a very particular type of method. Public class void main (string [] args) When running a java program, you are actually running a java class. Execution of the program will start at the beginning of the method called main inside the class that you run. Statements: inside a method, you can put many statements or commands.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions