Why Programming?
Intro
This is an introduction to Java and CS Awesome’s curriculum. It includes a video discussing the basics of Java & some activities CS Awesome uses to teach.
- Java is used in many places
- Used widely in the development of Android apps
- AppInventor translates it’s code into Java
- The language used to write Minecraft
- Used by Netflix
- Used widely in the development of Android apps
// All programs start with "public class" and a class name
public class MyClass {
// Class body
// Classes usually have a "main" method; always looks like that ↓
public static void main(String[] args) {
// Write your code here (this will be executed automatically)
System.out.print("No newline here!"); // Prints out string literal given
System.out.println("This one has it though :)"); // Same as print, but this time it ends with a line break
System.out.println("Welcome to sloshyyy.github.io!");
}
}
- All keywords are in lower case
public
,static
,void
, etc.
- Classes are upper case
MyClass
,System
,String
- Java is case sensitive!!!
System
!=system
- If you use the wrong one, your code will crash!
- Spacing doesn’t matter
- By convention people indent inside of a curly bracket block
- Helps to keep track of which code is where
- By convention people indent inside of a curly bracket block
- All statements must be ended with a semicolon!
- All open curly brackets need a closing curly bracket
- Same with quotes and parenthesis
1.2.1: My first Java program
- Every Java program is written as a “class”
- Java is Object Oriented
- Learn about that in Unit 2
- Everything within a program is inside that class
- Java is Object Oriented
- Can be a “main method”
- Starts the program
- This is what is executed when you ask Java to run a class
public class MyClass {
public static void main(String[] args) {
// Put your code here!
}
}
A template for a simple Java program with a main method
1.2.2: Print commands
- Java has two different print commands
System.out.println(val)
- Prints the value followed by a new line
System.out.print(val)
- Prints the value; no new line
System.out.println("Hi there!");
- prints characters between first and last quote
"Hi there!"
is a string literal- Can have any number of characters
- Most Java keywords are lowercase
- Class names are uppercase
System
orString
- Class names are uppercase
- Statements are terminated with a semicolon
- Used like a period in english
1.2.3: Syntax Errors and Debugging
- We need to compile (translate) Java source code into class files which the computer can run
- Syntax errors are reported if your code isn’t properly written
- A missing semicolon/curly bracket/quote could cause syntax errors
- Compiler errors will tell you the line number of the error and the type of error
- Not always easy to understand
- Sometimes the compiler is wrong about the line number!
How to debug
- Describe your problem
- What did you want your code to do, and what is it actually doing?
- Hunt for bugs
- What code is causing the bug?
- Try out solutions
- Document your learning
- Try explaining the bug out loud
- Can be to a real person
- Can be to no one
- Can also be to a rubber duck!
- (ducks are kind of fun)
My debugging ducks :)
1.2.4: Comments
- In Java,
//
is used to mark the beginning of a comment /*
is used to begin a multi line comment- ended with
*/
- ended with
- the compiler will skip over comments
- They are still useful to document code and make notes about what something is doing
- I make heavy use of
// TODO
to keep track of things I noticed but am not going to fix/improve right now
/* MyClass.java
Programmer: Nathan Jankowski
Date: 10/2/2021
*/
public class MyClass {
int max = 10; // keeps track of max score
}
An example of commenting
1.2.5: Debugging Challenge
There isn’t much to note with this section, just debug the code to get an expected output.
1.2.6: Summary
public class MyClass {
public static void main(String[] args) {
System.out.println("Hi there!");
}
}
A basic Java program
- A Java program starts with
public class NameOfClass {}
- Each class has it’s own file
- Class name needs to match file name
NameOfClass.java
- Each class has it’s own file
- the main method is invoked when a Java class is run
- will always be
public static void main(String[] args)
- will always be
System.out.println()
andSystem.out.print()
print to stdoutSystem.out.println
also adds a line break to the end of the output
- A string literal is enclosed in double quotes
- Java statements are ended with a semicolon
- Curly brackets enclose blocks of code
//
and/* */
are used for comments- A compiler translates source code into a class file which can be run.
- If the source code is improperly written, the compiler will report the errors as syntax errors
- Check for curly brackets, parenthesis, quotation marks, and semicolons
1.2.7: AP Practice
Nothing really to note here, just complete the activity.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.