SECTION 1.1
Why does java matter?
one of the main reason that java is such an important language in the coding world is because it is a object-oriented-programming (OOP) language.
it offers a structure that is easy to solve big problems. but i think that we all know that so lets skip to the real deal.
Basics of Java
-
Block Comments- The compiler will ignore any text between /* and */
-
line comments- Same as above but // ignores one line
-
Class Declaration - identifies the name, the start and the end of the class. the class name must match the file name. (case sensitive)
-
main Method- controls all of the action in the program.
-
system.out- objects that generate output to the console
-
system.out.print- prints what ever you out inside the ()
-
system.out.println- prints whatever is one the screen and moves to the next line to print out the next action. basically hits enter after it prints out.
tip: add “<classname>.main(null)
” at the end of your code to see the output in your jupyter notebook.
Example:
/* this is a
code block */
// code: printing out hello world.
public class greeting {
public static void main (String [] args) {
System.out.println("Hello, World!");
System.out.print("Hello,");
System.out.print(" World!");
}
}
greeting.main(null)
Hello, World!
Hello, World!
What is a string literal?
- any sequence of letters, numbers, or symbols that is put between quotes.
- java will put out anything in the quotes, no restrictions.
Examples:
public class stingLiterals {
public static void main (String [] args) {
System.out.println("This is a string literal.");
System.out.println("and so are these");
System.out.println("1234567890");
System.out.println("&^&*%^$&%$#^%W#*^$%&(*^)");
}
}
stingLiterals.main(null)
This is a string literal.
and so are these
1234567890
&^&*%^$&%$#^%W#*^$%&(*^)
ERRORS!!!!!!
Syntax/compiler error:
- messed up syntax
- compiler is not happy >:(
public class syntaxError {
public static void main (String [] args) {
system.out.println("This is a syntax error.")
//uncapitalized s
//missing semicolon
}
}
syntaxError.main(null)
| system.out.println("This is a syntax error.")
';' expected
Logic Error
- compiler is happy
- messed up in the string literals
- code works perfectly
public class logicError {
public static void main (String [] args) {
System.out.println("This is a leogic error.");
}
}
logicError.main(null)
This is a leogic error.
exception error:
public class exceptionError {
public static void main(String[] args) {
try {
int result = 2 / 0;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
}
exceptionError.main(null)
java.lang.ArithmeticException: / by zero
at REPL.$JShell$42C$exceptionError.main($JShell$42C.java:19)
at REPL.$JShell$45.do_it$($JShell$45.java:16)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at io.github.spencerpark.ijava.execution.IJavaExecutionControl.lambda$execute$1(IJavaExecutionControl.java:95)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1623)
SECTION 1.2
Variable and Data Types.
Primitive Data
- determines the size and type of data can we can worth with in a java program.
- focus on three different types that can we can represent data.
Smallest to biggest:
Boolean, takes up 1 bit.
- true or false
Int, take up 32 bit
- whole number values.
- add, subtract, multiply, etc.
Doubles, AKA Floating point numbers. 64 bit
- same as integers
String
- “Hello World!”
Reference Purposes
(the collegeboard person used bows as reference so will i.)
- there can be small bow
- a medium bow
- a red bow
- a large red bow
What is the difference?
- primitive data are already in java, you don’t have to make it. Except for string, which is created by the programmer.
- non-primitive data can be use methods to perform actions, but primitive data cannot.
Primitive Activity
MIX&MATCH
Choices:
1. int
2. double
3. boolean
4. String
__ False
__ "ryan gosling"
__ 1738
__ 4.26
Questions:
what type of data should be used to store
-
someones bank number and cvc?
-
someones mother’s maiden name and their name?
-
the first 16 digits of pi
-
if you want to transact one million $.
Variables
A name given to a memory location that is holding a specified type of value.
how to name a variable (omg this is so hard !)
-
may consists of letters, digits, or an underscore (case sensitive)
- may not start with a digit
- space are a big no no
- may not use other characters as &,@, or #
- may not used java reserved words
Tip!
use camel casing when naming a variables.
example:
thisIsCamelCasing
Declare variables:
The three primitiva data types in Java:
- integers (whole #):
int
- Decimal numbers (floating-point values):
double
- Boolean values (true/false):
boolean
Format:
dataType varibleName;
Example
int total;
boolean outcome;
double firstFifteenPi;
what if you don’t want to change the variable’s value after given?
add final in front of the declaration:
final double PI;
final boolean WORKOUT_DECISION;
for final variables, use all caps and underscore when naming them.
Practice
Find the odd one out.
int value;
double 4eva;
boolean public;
integer sum;
double wow!;
boolean foundIt;
int numberOfPigs;
double chanceOfRain;
boolean #apDaily;
int count;
bool isFriday;
final int DOZEN;