Currently studying for my SCJP exam, the material they provide you with to study is quite clearly rubbish as it covers no where near enough detail about the nuances of Java. For example
Spot the errors in the above code, a little test for you
But anyway that's not what I learnt today, given the following code
Produces the output:
bob1 height: 10
bob1 height: 20
What does the following code output
You lot probably already know this, but scroll down to see the answer
Hello
Hello
Despite the fact that the str2 reference variable refers to the same object, when you make a change to the reference variable the JVM refers it to a different string object so the other string isn't changed!
Code:
interface Jumpable {
int height = 5;
void jump();
}
class Jumper implements Jumpable {
public Jumper(int height) {
height = 10;
}
protected void jump() {}
}
Spot the errors in the above code, a little test for you
But anyway that's not what I learnt today, given the following code
Code:
Bob bob1 = new Bob();
bob1.setHeight(10);
System.out.println("bob1 height: " + bob1.getHeight());
Bob bob2 = bob1;
bob2.setHeight(20);
System.out.println("bob1 height: " + bob1.getHeight());
Produces the output:
bob1 height: 10
bob1 height: 20
What does the following code output
Code:
String str1 = "Hello";
System.out.println(str1);
String str2 = str1;
str2 += " World";
System.out.println(str1);
You lot probably already know this, but scroll down to see the answer
Hello
Hello
Despite the fact that the str2 reference variable refers to the same object, when you make a change to the reference variable the JVM refers it to a different string object so the other string isn't changed!
Last edited: