Week1 1 Java
Week1 1 Java
• Package declaration
• Matches the directory structure in the src (source)
directory
• This le is saved in the directory "src/week1"
fi
Java - Hello World
package week1;
• Create a class
• All Java code is de ned in classes
• We create a class named HelloWorld to contain our code
fi
Java - Hello World
package week1;
• Main method
• A method is a function de ned within a class
• The main method is a special method that controls the start of
your program
• Running a program is equivalent to calling the main method
fi
Java - Hello World
package week1;
}
[Link]("I return nothing");
• That variable can only store
public static void main(String[] args) { values of that type
double num = 2.4;
num = multiplyByTwo(num);
[Link]("new num:" + num);
why();
• The variable "num" can only
} store a double
•
}
num = "hello"; would
cause an error
Java - Variables and Methods
package week1;
•
double x = input * 2;
}
return x; Name the variable num
public static void why(){
}
[Link]("I return nothing"); • Assign num the value 2.4
public static void main(String[] args) {
double num = 2.4;
num = multiplyByTwo(num);
[Link]("new num:" + num);
why();
}
}
Java - Variables and Methods
package week1;
•
double x = input * 2;
}
return x; Let's talk about method
public static void why(){
declaration in Java!
[Link]("I return nothing");
}
}
Java - Variables and Methods
package week1;
• Just like the main method,
public class VariablesFunctions {
this method will be:
public static double multiplyByTwo(double input){
double x = input * 2;
return x; • public: This method can be
}
called from anywhere in the
public static void why(){
[Link]("I return nothing");
project
}
}
[Link]("I return nothing");
• This method takes an
public static void main(String[] args) { input of type double
double num = 2.4;
num = multiplyByTwo(num);
[Link]("new num:" + num);
why();
• It can only be called with
} arguments of type
} double
Java - Variables and Methods
package week1;
•
double x = input * 2;
}
return x; num is reassigned to the
public static void why(){
value 4.8
[Link]("I return nothing");
}
}
fi
Java - Variables and Methods
package week1;
• Some methods don't return
public class VariablesFunctions {
a value
public static double multiplyByTwo(double input){
}
double x = input * 2;
return x; • Give these methods a return
type of "void"
public static void why(){
}
[Link]("I return nothing");
• Methods that return void are
public static void main(String[] args) { called for their "side-e ects"
double num = 2.4;
num = multiplyByTwo(num); which is any functionality
[Link]("new num:" + num);
why(); aside from the return value
}
•
double x = input * 2;
}
return x; The program ends
public static void why(){
[Link]("I return nothing");
}
}
Types
Java - Types
• Java is strongly typed
package week1; • Let's talk about the common
public class VariablesFunctions { types we'll use to start the
public static void main(String[] args) {
double num1 = 2.4;
semester
String str1 = "A string";
int num2 = 6/4;
boolean bool = true;
}
}
Java - Types
• double
package week1; • A number that can have a
public class VariablesFunctions { decimal portion
•
public static void main(String[] args) {
double num1 = 2.4;
String str1 = "A string";
We call this a oating point
int num2 = 6/4;
boolean bool = true;
number
}
}
Java - Types
• int
package week1; • A whole number
public class VariablesFunctions {
• Can be negative
public static void main(String[] args) {
double num1 = 2.4;
String str1 = "A string"; • Dividing 2 ints will result in int!
int num2 = 6/4;
}
boolean bool = true;
• Called integer division
}
• 6/4 == 1
• Result is always rounded down
(Floor)
• 99/100 == 0
Java - Types
• boolean
package week1; • true or false
•
public class VariablesFunctions {
}
Memory Diagram!
package week1;
num2=multiplyByTwo(num2);
[Link]("new num2:"+num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
• We separate the stack into two column for the name and value of each
variable
Stack
package week1;
Value
Heap
Name
public class VariablesFunctions {
public static double multiplyByTwo(double input){
double x = input * 2;
return x;
}
public static void why(int val){
[Link]("I return nothing");
}
public static void main(String[] args) {
[Link]("I can print!");
int num = 4;
double num2 = 2.4;
String str1 = "A string";
int num3 = 6/4;
boolean bool = true;
[Link]("I am num3: " + num3);
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
• Now we're ready to start tracing through the code and see how this program
works in memory
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
• When variables are declared, add then to the stack with their value
• We don't add the variable type in our memory diagrams
Stack
package week1;
Value
Heap
Name
public class VariablesFunctions {
public static double multiplyByTwo(double input){
double x = input * 2;
num 4
}
return x; num2 2.4
public static void why(int val){ str1 "A string"
[Link]("I return nothing");
}
public static void main(String[] args) {
[Link]("I can print!");
int num = 4;
double num2 = 2.4; in/out
String str1 = "A string";
int num3 = 6/4; I can print!
boolean bool = true;
[Link]("I am num3: " + num3);
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
• We add the name of the method called next to the stack frame
• Add an arrow to denote where the return value will be assigned
Stack
package week1;
Value
Heap
Name
public class VariablesFunctions {
public static double multiplyByTwo(double input){
double x = input * 2;
num 4
}
return x; num2 2.4
public static void why(int val){ str1 "A string"
[Link]("I return nothing");
} num3 1
public static void main(String[] args) {
[Link]("I can print!"); bool true
int num = 4;
double num2 = 2.4; multiplyByTwo
input 2.4 in/out
String str1 = "A string";
int num3 = 6/4; I can print!
boolean bool = true; I am num3: 1
[Link]("I am num3: " + num3);
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
• Finally, we add each parameter (only 1 in this example) to the stack and assign them the
values of the arguments
• We're now ready to run the code of the method using only the variable inside the stack frame
Stack
package week1;
Value
Heap
Name
public class VariablesFunctions {
public static double multiplyByTwo(double input){
double x = input * 2;
num 4
}
return x; num2 2.4
public static void why(int val){ str1 "A string"
[Link]("I return nothing");
} num3 1
public static void main(String[] args) {
[Link]("I can print!"); bool true
int num = 4;
double num2 = 2.4; multiplyByTwo
input 2.4 in/out
String str1 = "A string";
int num3 = 6/4;
x 4.8 I can print!
boolean bool = true; I am num3: 1
[Link]("I am num3: " + num3);
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
• When a value is returned, assign it to the value following the return arrow
• To update a value, cross out the old value and write the new value
Stack
package week1;
Value
Heap
Name
public class VariablesFunctions {
public static double multiplyByTwo(double input){
double x = input * 2;
num 4
}
return x; num2 2.4 4.8
public static void why(int val){ str1 "A string"
[Link]("I return nothing");
} num3 1
public static void main(String[] args) {
[Link]("I can print!"); bool true
int num = 4;
double num2 = 2.4; multiplyByTwo
input 2.4 in/out
String str1 = "A string";
int num3 = 6/4;
x 4.8 I can print!
boolean bool = true; I am num3: 1
[Link]("I am num3: " + num3);
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}
• Cross out the entire stack frame when the method returns
• The variables in the stack frame are erased from memory
Stack
package week1;
Value
Heap
Name
public class VariablesFunctions {
public static double multiplyByTwo(double input){
double x = input * 2;
num 4
}
return x; num2 2.4 4.8
public static void why(int val){ str1 "A string"
[Link]("I return nothing");
} num3 1
public static void main(String[] args) {
[Link]("I can print!"); bool true
int num = 4;
double num2 = 2.4; multiplyByTwo
input 2.4 in/out
String str1 = "A string";
int num3 = 6/4;
x 4.8 I can print!
boolean bool = true; I am num3: 1
[Link]("I am num3: " + num3);
new num2: 4.8
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}