jon ruane

Greenhorn
+ Follow
since Mar 08, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by jon ruane

i know how to use the wait() and sleep() methods, but one thing about them is puzzling me:

under what circumstances would you call these methods on an object, as oppose to just calling them on their own?

for example iv seen code like this:

object.wait();
thread.sleep();

but also:

wait();
sleep();

i know that wait() must be done within a sync block, so is wait() actually looking at what the block is sync'd on, and thats the object it sets to waiting?

many thanks to anyone who can clear this little query up.

john.
17 years ago
i think these guys explain it better:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html

15.13.2 Examples: Array Access Evaluation Order

In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated. For example, in the (admittedly monstrous) expression a[(a=b)[3]], the expression a is fully evaluated before the expression (a=b)[3]; this means that the original value of a is fetched and remembered while the expression (a=b)[3] is evaluated. This array referenced by the original value of a is then subscripted by a value that is element 3 of another array (possibly the same array) that was referenced by b and is now also referenced by a.
Thus, the example:


class Test {
public static void main(String[] args) {
int[] a = { 11, 12, 13, 14 };
int[] b = { 0, 1, 2, 3 };
System.out.println(a[(a=b)[3]]);
}
}

prints:

14

because the monstrous expression's value is equivalent to a[b[3]] or a[3] or 14.
in response to Tina's question:

the reason that this code compiles:

int[] a={11,12,13,14};
int[] b={0,1,2,3};
System.out.println(a[(a=b)[3]]);


and this code does not:

int[] a={11,12,13,14};
int[] b={1,2,3,4};
System.out.println(a[(a=b)[3]]);

is to do with the assignment of a=b. when you assign a value to a variable, you are returning the value that is being assigned. i.e int x=1 will return the value of 1.

in the above code, when we say: (a=b)[3], this can be read as return the value of b[3]. a=b returns the value b, and then we are looking up index [3] in b's array.

the value of b[3] is different in the two pieces of code. b[3]=3 in the code that compiles, and b[3]=4 in the code that does not. that is why you are getting the ArrayOutOfBounds exception; there is no index 4 in array 'a'. System.out.println(a[4]).

took me a while to figure this out. hope iv explained it as simple as....
cheers guys. just thought id post the way i eventually did it:

SimpleDateFormat format = new SimpleDateFormat("dd MMM, yyyy");

try {
Date parsed = format.parse(str);
GregorianCalendar cal =
new GregorianCalendar(parsed.getDay(),parsed.getMonth (),parsed.getYear());
int day = (parsed.getDate());
int month = (parsed.getMonth()+1);
int year = (parsed.getYear()+1900);
System.out.println(day +" " + month + " "+ year);
} catch (ParseException e) {
e.printStackTrace();
}

i realise that the get() methods from Date are in fact depreciated, but i've been told that they will function fine in an application thats not going to be run on a machine running a different time zone.
17 years ago
does anyone know of any classes that i could use to turn a String date into its number equivalent?

i need to split a string "20 Apr, 2007" into day month year, so this would return:

day = 20
month = 4,
year = 2007

iv used a mixture of split() and parseInt() to get the day and year, but im left with String "apr" which i need to turn into 4. any simple way without using a massive IF statement.
17 years ago
im currently studying for my programmers certification and i've come across this question twice, but with opposite answers.

can anyone clear it up just in case it pops up on the real exam.

TRUE or FALSE:

"is-a relationships always require at least two class types"



(i heard that a 'type' can be 'of its own' type i.e Object is-a Object. one class, therefore the is-a relationship statement above is false)
can someone please explain why the System.out statement at line //1 didnt throw an 'unreachable code error'. i felt for sure that it would, as the call above it redirects it to another method.

are 'unreachable code' errors usually found in loops?

thank you.



public class MyProgram {
public static void throwIt() {
throw new RuntimeException();
}

public static void main(String[] args) {
try {
System.out.println("hello world");
throwIt();
System.out.println("done with block"); //1
}
catch (RuntimeException e) {
System.out.println("catch");
}
finally {
System.out.println("finally");
}
}
}
17 years ago
Thanks for all the useful advice.

why i have questioned the information in the SCJP book, is that i have seen questions in mock exams that ask: where/when should you not use assertions, and my answers have been the 3 places im told:

1. in public methods
2. the validate command line arguments
3. anywhere that can cause a side effects

then i see that there is a twist to the public method rule. i dont like uncertainty, and if im asked the above question in an exam, then i'll will be hesitating in my answer.
17 years ago
ok, what i've learnt (from studying Kathy and Berts SCJP study guide for java 5), is that one of the places you shouldnt use assertions is in public methods.

this makes sense as your public methods are the 'way-in' to your application from the outside world, and thus must have some kind of protection that guarantees certain contraints on the arguments of the public methods will be inforced.

as assertions arent guaranteed to be enabled, this enforcement might not happen. so dont rely on them.

my questions is: based on the above, why are we then advised to use assertions, even in public methods, to check for cases that you know are never ever supposed to happen?

for example a case statement:

switch(x) {
case 2: ......
case 3: ......
case 4: ......
default: assert false;
}

in the above, we are assuming that x will be either 2,3 or 4. anything else and we want to alert the user of a problem via an assertion.

following on from my 1st point, if assertions are disabled then in the above case statement a value other than 2,3 or 4 will slip through the net, and we will be unaware of this.
17 years ago
i came across this question. now the answer is that the code in B.java fails to compile as we are trying to access a's integer 'i', through a 'b'object.

now i could try memorise this rule, but if someone could explain why this is so, and why if you change the access modifier of 'a' to public, the code compiles, then i think this rule will stick in my head.


Consider following two classes:
//in file A.java

package p1;
public class A{
protected int i = 10;

public int getI() {
return i;
}
}

//in file B.java

package p2;
import p1.*;

public class B extends p1.A {
public void process(A a) {
a.i = a.i*2;
}

public static void main(String[] args) {
A a = new B();
B b = new B();
b.process(a);
System.out.println(a.getI());
}
}
17 years ago
oooooooooooooooooooooooh!

thanks very much!
17 years ago
As i understand: Conditional operators have higher presidence over Assignment operators, as the following example code shows:

public class Q10
{
public static void main(String[] args)
{
int i = 10;
int j = 10;
boolean b = false;

if( b = i == j)
System.out.println("True");
else
System.out.println("False");
}
}

(Prints out "True")


What i cant understand is why a compliation error is not thrown at the fact a boolean variable is having an int assigned to it in the IF statement. It doesnt even look like a legal IF statemnt, but it is!

If you place the code:

b = i anywhere else, the compiler picks up this illegal assignment.

Is the assignment being ignored because the conditional side of the IF statement is the only part the compiler deals with?
17 years ago