import java.util.Random;
public class Hello {
private static final String[] GREETINGS = {"Hello!", "Hi!", "W'sup!"};
private String greeting;
private Hello() {
Random rand = new Random();
int greetingsIndex = rand.nextInt(GREETINGS.length);
greeting = GREETINGS[greetingsIndex];
}
public Hello(int anIndex) {
int greetingsIndex = anIndex % GREETINGS.length;
greeting = GREETINGS[greetingsIndex];
}
public String getGreeting() {return greeting;}
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.getGreeting());
}
}
import
StatementThe statement
import java.util.Random;
allows us to use the java.util.Random
class in our program. Without the import, the line
Random rand = new Random(1);
would prodiuce the following error:
Hello.java:11: error: cannot find symbol
Random rand = new Random(1);
^
symbol: class Random
location: class Hello
public Hello() {
Random rand = new Random(10);
int greetingsIndex = rand.nextInt(greetings.length);
greeting = greetings[greetingsIndex];
}
greeting
instance variablenew
Hello h = new Hello();
, h
holds the address of a Hello
object which has some randomly assigned greeting
instance variableh
is a reference to a Hello
objectWhat does Random rand = new Random(10);
do?
public Hello(int anIndex) {
int greetingsIndex = anIndex % greetings.length;
greeting = greetings[greetingsIndex];
}
new
, as in Hello h = new Hello(1);
new
call selects a particular constructor, in this case the constructor that takes one int
parameterConsider Doberman.java:
public class Doberman {
private static int dobieCount = 0;
private String name;
public Doberman(String name) {
this.name = name;
dobieCount++;
}
public String reportDobieCount() {
return name + " says there are " + dobieCount + " dobies.";
}
}
dobieCount
is shared between all instances of the Doberman
class.Doberman
has its own distinct copy of name
.Given DaringDobermans.java:
public class DaringDobermans {
public static void main(String[] args) {
Doberman fido = new Doberman("Fido");
Doberman prince = new Doberman("Prince");
Doberman chloe = new Doberman("Chloe");
System.out.println(fido.reportDobieCount());
System.out.println(prince.reportDobieCount());
System.out.println(chloe.reportDobieCount());
}
}
and our definition of Doberman
, what will java DaringDobermans
print?
$ java DaringDobermans
???
???
???
Given DaringDobermans.java:
public class DaringDobermans {
public static void main(String[] args) {
Doberman fido = new Doberman("Fido");
Doberman prince = new Doberman("Prince");
Doberman chloe = new Doberman("Chloe");
System.out.println(fido.reportDobieCount());
System.out.println(prince.reportDobieCount());
System.out.println(chloe.reportDobieCount());
}
}
and our definition of Doberman
, what will java DaringDobermans
print?
$ java DaringDobermans
Fido says there are 3 dobies.
Prince says there are 3 dobies.
Chloe says there are 3 dobies.
Now remove static
from the definition of dobieCount
:
private int dobieCount = 0;
Now when we run DaringDobermans
we get
$ java DaringDobermans
Fido says there are 1 dobies.
Prince says there are 1 dobies.
Chloe says there are 1 dobies.
The difference is that now each Doberman
instance has its own copy of dobieCount
, not a class-wide, or static
dobieCount
public class Doberman {
private static int dobieCount = 0;
private String name;
public Doberman(String name) {
this.name = name;
dobieCount++;
}
public String reportDobieCount() {
return name+" says there are "+dobieCount+" dobies.";
}
}
What does this code print?
Doberman fido = new Doberman("Fido");
System.out.println(fido.reportDobieCount());
Doberman prince = new Doberman("Prince");
System.out.println(prince.reportDobieCount());
Doberman chloe = new Doberman("Chloe");
System.out.println(chloe.reportDobieCount());
Given the definition of Doberman
from the previous slide,
Doberman fido = new Doberman("Fido");
System.out.println(fido.reportDobieCount());
Doberman prince = new Doberman("Prince");
System.out.println(prince.reportDobieCount());
Doberman chloe = new Doberman("Chloe");
System.out.println(chloe.reportDobieCount());
prints
Fido says there are 1 dobies.
Prince says there are 2 dobies.
Chloe says there are 3 dobies.
int n = 2.2;
legal?17 % 4
?n
after int n = (int) 2.2;
?n++;
, what's the value of n
?n += 2;
, what's the value of n
?String s = "Answer: " + n;
, what's the value of s
?int n = 2.2;
is not legal -- implicit narrowing conversion17 % 4
is 1
int n = (int) 2.2;
n
is 2n++;
, n
is 3n += 2;
, n
is 5String s = "Answer: " + n;
, s
is "Answer: 5"
Will this code compile?
String condition = "true";
if (condition) {
System.out.println("The true path.");
} else {
System.out.println("The false path.");
}
Will this code compile?
String condition = "true";
if (condition) {
System.out.println("The true path.");
} else {
System.out.println("The false path.");
}
No. condition
must be a boolean expression.
What will this code print?
boolean a = true;
boolean b = false;
if (a && b ) {
System.out.println("the true path");
} else {
System.out.println("the false path");
}
boolean a = true;
boolean b = false;
if (a && b ) {
System.out.println("the true path");
} else {
System.out.println("the false path");
}
prints
the false path
&&
is logical and.
What will this code print?
public class ShortCircuit {
private static int counter = 0;
private static boolean inc() {
counter++;
return true;
}
public static void main(String args[]) {
boolean a = false;
if (a || inc()) {
System.out.println("Meow");
}
System.out.println("counter: " + counter);
if (a && inc()) {
System.out.println("Woof");
}
System.out.println("counter: " + counter);
}
}
Substitute values, track counter
and output:
Code counter Output
boolean a = false; 0
if (a || inc()) { 1
System.out.println("Meow"); 1 Meow
} 1
System.out.println("counter: " + counter); 1 counter: 1
if (a && inc()) { 1
System.out.println("Woof"); 1
} 1
System.out.println("counter: " + counter); 1 counter: 1
Key points:
inc()
always returns true
inc()
not always evaluatedHow would you write this while
loop as a for
loop?
int n = 0;
while (n < 5) {
System.out.println("Hip, hip, hooray!");
n++;
}
Answer:
???
???
???
How would you write this while
loop as a for
loop?
int n = 0;
while (n < 5) {
System.out.println("Hip, hip, hooray!");
n++;
}
Answer:
for (int n = 0; n < 5; n++) {
System.out.println("Hip, hip, hooray!");
}
What will this code print?
public class Foo {
private String bar;
public Foo(String bar) {
this.bar = bar;
}
public boolean equals(Object other) {
return this.bar.equals(((Foo) other).bar);
}
public static void main(String[] args) {
Foo foo1 = new Foo("bar");
Foo foo2 = new Foo("bar");
Foo foo3 = foo1;
System.out.println("foo1.equals(foo2): " + foo1.equals(foo2));
System.out.println("foo1 == foo2: " + (foo1 == foo2));
System.out.println("foo1 == foo3: " + (foo1 == foo3));
}
}
Given the definition of Foo
on previous slide and:
Foo foo1 = new Foo("bar");
Foo foo2 = new Foo("bar");
Foo foo3 = foo1;
You have:
foo1.equals(foo2): true
foo1 == foo2: false
foo1 == foo3: true
public class Kitten {
private String name;
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
}
Assume the following statements have been executed:
Kitten maggie = new Kitten("Maggie");
Kitten fiona = new Kitten("Fiona");
Kitten fiona2 = new Kitten("Fiona");
What is the value of maggie
?
public class Kitten {
private String name;
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
}
Assume the following statements have been executed:
Kitten maggie = new Kitten("Maggie");
Kitten fiona = new Kitten("Fiona");
Kitten fiona2 = new Kitten("Fiona");
What is the value of maggie
?
Kitten
objectpublic class Kitten {
private String name;
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
}
Assume the following statements have been executed:
Kitten maggie = new Kitten("Maggie");
Kitten fiona = new Kitten("Fiona");
Kitten fiona2 = new Kitten("Fiona");
What does maggie.toString()
return?
public class Kitten {
private String name;
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
}
Assume the following statements have been executed:
Kitten maggie = new Kitten("Maggie");
Kitten fiona = new Kitten("Fiona");
Kitten fiona2 = new Kitten("Fiona");
What does maggie.toString()
return?
"Kitten: null"
public class Kitten {
private String name;
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
}
Assume the following statements have been executed:
Kitten maggie = new Kitten("Maggie");
Kitten fiona = new Kitten("Fiona");
Kitten fiona2 = new Kitten("Fiona");
What is the value of the expression fiona == fiona2
?
public class Kitten {
private String name;
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
}
Assume the following statements have been executed:
Kitten maggie = new Kitten("Maggie");
Kitten fiona = new Kitten("Fiona");
Kitten fiona2 = new Kitten("Fiona");
What is the value of the expression fiona == fiona2
?
false
public class Kitten {
private String name;
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
}
Assume the following statements have been executed:
Kitten maggie = new Kitten("Maggie");
Kitten[] kittens = new Kitten[5];
What is the value of kittens[0]
?
public class Kitten {
private String name;
public Kitten(String name) {
name = name;
}
public String toString() {
return "Kitten: " + name;
}
}
Assume the following statements have been executed:
Kitten maggie = new Kitten("Maggie");
Kitten[] kittens = new Kitten[5];
What is the value of kittens[0]
?
null