Introduction to
Programming with Java
– Lecture 4
German Eichberger, MSc
Principal Cloud Software Engineer @ HPE
Instructor @ UCSD Extension
Adjunct Professor @ Mesa College
Functions
Remember from Math
parameter
Return value
function
Remember from Math
Another function
How Objects Behave
The size effects the bark
Instance variables
(state) Dog
size
name knows
methods
(behavior)
bark() does
Send things to a method
d.bark(3); //bark three times
A caller passes arguments
void bark (int numOfBarks)
A method uses parameters
How does it work?
1. Call the bark method on the dog reference,
and pass the value 3 (as an argument)
2. The bits representing 3 are delivered to the
bark method
3. The bits land in the numOfBarks parameter
4. Use of numOfBarks parameter as a variable in
the method code
More than one argument
void go() {
TestStuff t = new TestStuff();
int foo = 34;
t.takeTwo(12, foo);
}
void takeTwo(int x, int y) {
int z = x + y;
System.out.println(“Total is” + z);
}
Java is pass-by-value
• int x = 7; x
z
• void go (int z) { }
copy
• foo.go(x); x z
• void go(int z) {
z = 0; x z
}
x an z aren’t
connected
Get things back
• int theSecret = life.giveSecret();
These types
must match
• int giveSecret() {
return 42;
}
Bullet Points
• Classed define what an object knows and what
an object does
• Things an object knows are its instance
variables (state, attributes)
• Things an object does are methods (behavior)
• Methods can use instance variables so that
objects of the same type behave differently.
Bullet Points
• A method can have parameter, which means
you can pass one or more values in to the
method.
• The number and type of values you pass in
must match the order and type of the
parameters declared by the method
• Values passed in and out of methods can be
implicitly promoted to a larger type or
explicitly cast to a smaller type.
Bullet Points
• A method must return a return type. A void
return type means the method doesn’t
return anything.
• If a method declares a non-void return
type, it must return a value compatible with
the declared return type.
Fancy Functions
Function
Function
-x Conditional
x
UnaryOperator<Double> fx = x->x<0
? -x
: x;
Fibonacci Numbers
n 0 1 2 3 4 5 6 7 8
1 1 2 3 5 8
Fibonacci Numbers
UnaryOperator<Integer> fib = n -> n<=1
?1
: this.fib.apply(n-1) +
this.fib.apply(n-2);
Fancy Functions
• Called Lambda Expressions
• BinaryOperator<Integer> add = (x,y)->x+y;
– Allows to write things in a more
mathematical way
– Less boilerplate
– More functional
Lazy Evaluation
• lazy evaluation is an evaluation strategy
which delays the evaluation of an
expression until its value is needed (non-
strict evaluation)
• It also avoids repeated evaluations
(sharing) - sharing can reduce the running
time of certain functions by an exponential
factor over other non-strict evaluation
strategies
Benefits of Lazy Evaluation
• Performance increases by avoiding
needless calculations, and error conditions
in evaluating compound expressions
• The ability to construct potentially infinite
data structures
• The ability to define control flow
(structures) as abstractions instead of
primitives
Thank You!
1. Download VirtualBox and install it (see
https://www.virtualbox.org)
2. Download vagrant and install it (see
https://www.vagrantup.com/docs/installation/)
3. Download Vagrant file for the course from
Vagrant, etc.
Introduction
Hello World
public class HelloWorld {
public static void main(String args[]) {
System.out.println(“Hello World”);
}
}
What is code
• The projects will be due as assigned
• One hour final examination (multiple choice)
http://www.bloomberg.com/graphics/2015-paul-ford-what-is-code/