0% found this document useful (0 votes)
1 views54 pages

Week1 1 Java

Uploaded by

mitlucy1209
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views54 pages

Week1 1 Java

Uploaded by

mitlucy1209
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java

IntelliJ, packages, types, methods


Hello World
Java - Hello World
package week1;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• The simplest Java example


• .. it's still pretty complex
• Prints "Hello Java!" to the console
• Let's break this down
Java - Hello World
package week1;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello 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;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• 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;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• Filename matches the class name for that le


• This le will be named "[Link]"
• Combining the lename and directory structure, the full path for
this le is:
• "src/week1/[Link]"
fi
fi
fi
fi
Java - Hello World
package week1;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• 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;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• static: the method can be called without extra steps


• void: the method does not return a value
• main: The main method must be named exactly "main"
• String[]: The main method takes Strings as a parameter
Java - Hello World
package week1;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• public: This code can be used from anywhere


• Mix the ingredients of public, static, void, main,
String[] together and you have a main method in Java
• Confusing? This is why we don't use Java in CSE115
Java - Hello World
package week1;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• Or type main in IntelliJ and it creates a main


method for you
Java - Hello World
package week1;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• We're nally ready to write some code!


• [Link] is a method that prints to the
console
• Or type sout in IntelliJ
fi
Java - Hello World
package week1;

public class HelloWorld {

public static void main(String[] args) {


[Link]("Hello Java!");
}
}

• All Java statements must end with a semicolon


• Don't forget your semicolons
Variables and Methods
Java - Variables and Methods
package week1;

public class VariablesFunctions {


• This program de nes
several variables and
public static double multiplyByTwo(double input){
double x = input * 2;
return x;
methods in Java
}

public static void why(){ • We'll focus on the Java


}
[Link]("I return nothing");
speci c details
public static void main(String[] args) {
double num = 2.4;
num = multiplyByTwo(num);
• We'll step through the
[Link]("new num:" + num); code starting in the main
why();
} method
}
fi
fi
Java - Variables and Methods
package week1;

public class VariablesFunctions {


• Java is strongly typed!
public static double multiplyByTwo(double input){ • Whenever you declare a
double x = input * 2;
return x; variable, you must specify
}
the type of that variable
public static void why(){

}
[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;

public class VariablesFunctions {


• Declare a variable of type
double
public static double multiplyByTwo(double input){


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;

public class VariablesFunctions {


• Next, we call a method
named multiplyByTwo
public static double multiplyByTwo(double input){


double x = input * 2;

}
return x; Let's talk about method
public static void why(){
declaration in Java!
[Link]("I return nothing");
}

public static void main(String[] args) {


double num = 2.4;
num = multiplyByTwo(num);
[Link]("new num:" + num);
why();
}

}
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
}

public static void main(String[] args) {


• static: This method can be
double num = 2.4;
num = multiplyByTwo(num);
called without additional
[Link]("new num:" + num);
why();
setup

}
For the rst few weeks, all of
}
our methods will be public
static methods
fi
Java - Variables and Methods
package week1;
• Next, we must specify the
public class VariablesFunctions {
return type of the method
public static double multiplyByTwo(double input){
double x = input * 2;
return x;
• This method has a return type
} of double so it will return a
public static void why(){ double
[Link]("I return nothing");
}
• This is a contract you sign
public static void main(String[] args) {
double num = 2.4;
with Java saying you
num = multiplyByTwo(num); guarantee that you'll return a
[Link]("new num:" + num);
why(); value of type of double
}

} • If you break this contract,


Java will refuse to run your
code
Java - Variables and Methods
package week1;

public class VariablesFunctions {


• Since Java is strongly
typed, you must specify
public static double multiplyByTwo(double input){
double x = input * 2;
return x;
the type of every
}
parameter
public static void why(){

}
[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;

public class VariablesFunctions {


• All code between the
braces are part of the
public static double multiplyByTwo(double input){
double x = input * 2;
return x;
body of the method
}

public static void why(){ • Indentation does not


}
[Link]("I return nothing");
matter (As opposed to
public static void main(String[] args) { Python)
double num = 2.4;
num = multiplyByTwo(num);
[Link]("new num:" + num);
why();
• User "return" to return a
} value matching the return
} type of the method
Java - Variables and Methods
package week1;

public class VariablesFunctions {


• Now we call the method
we de ned
public static double multiplyByTwo(double input){


double x = input * 2;

}
return x; num is reassigned to the
public static void why(){
value 4.8
[Link]("I return nothing");
}

public static void main(String[] args) {


• print "new num: 4.8"
double num = 2.4;
num = multiplyByTwo(num);
[Link]("new num: " + num);
why();
}

}
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
}

} • This method has the side-


e ect of printing text
ff
ff
Java - Variables and Methods
package week1;

public class VariablesFunctions {


• When the end of the main
method is reached
public static double multiplyByTwo(double input){


double x = input * 2;

}
return x; The program ends
public static void why(){
[Link]("I return nothing");
}

public static void main(String[] args) {


double num = 2.4;
num = multiplyByTwo(num);
[Link]("new num:" + num);
why();
}

}
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
}

} • doubles cannot always be


represented exactly in a
computer (Foreshadow!)
fl
Java - Types
• String
package week1; • A sequence of characters
public class VariablesFunctions { (char)

public static void main(String[] args) {
double num1 = 2.4;
String str1 = "A string";
Note the capital S
int num2 = 6/4;
boolean bool = true;
}

}
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 {

public static void main(String[] args) {


Note the lowercase t and f
double num1 = 2.4;
String str1 = "A string";
int num2 = 6/4;
boolean bool = true;
}

}
Memory Diagram!
package week1;

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 an a num3: "+num3);

num2=multiplyByTwo(num2);
[Link]("new num2:"+num2);
why(2);
}
}

• Let's look at all the code together in one program


• We'll trace through this code in a memory diagram
• [The same way you will on lab quizzes]
package week1;

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);
}
}

• We'll trace through this code using a memory diagram

• We keep track of everything stored in the computer's memory while the


program runs
Stack
package week1;
Name Value
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);
}
}

• We setup space for the "stack" memory which stored variable

• 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);
}
}

• Heap Memory: A wonderful topic.. for another day


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; in/out
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);
}
}

• Add an in/out section wherever you have room

• This is where you put everything that's printed to the console


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; 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);
}
}

• Now we're ready to start tracing through the code and see how this program
works in memory

• First, we print "I can print!"


Stack
package week1;
Value
Heap
Name
public class VariablesFunctions {
public static double multiplyByTwo(double input){
double x = input * 2;
num 4
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; 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);
}
}

• 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);
}
}

• Add the next 2 variables to the stack


• Newer variables are added to the bottom of the stack
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!");
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);
}
}

• Don't forget about integer division


• When dividing 2 ints, the result will not have a decimal portion
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; 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);
}
}

• Declare another variable


• Print to the console again
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; 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);
}
}

• When a method is called, a "stack frame" is added to the stack


• A stack frame is an isolated part of the stack just for the method call
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; 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);
}
}

• We show stack frames as solid boxes


• You cannot break out of this part of the stack until the method returns
• Variables on the stack that are not inside the stack frame cannot be accessed
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
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);
}
}

• 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);
}
}

• New variables are declared 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 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);
}
}

• 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);
}
}

• Control returns to the main method


• Continue with the program
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);
why val 2 new num2: 4.8
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
why(2);
}
}

• We reach another method call


• This method returns void so there's no return arrow
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);
why val 2 new num2: 4.8
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
I return nothing
why(2);
}
}

• The method prints, then exits


• Cross out 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 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);
why val 2 new num2: 4.8
num2 = multiplyByTwo(num2);
[Link]("new num2: " + num2);
I return nothing
why(2);
}
}

• We reach the end of the program


• We now have a record of everything that happened in memory while the
program ran

You might also like