Patrick Hoey

+ Follow
since Oct 11, 2001
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Patrick Hoey

public void test4()
int i=0;
int m=0;
System.out.println(i); //prints 0
System.out.println(m=i++); //prints 0

System.out.println(m); //prints 0
System.out.println(i); //prints 1
This shows the sublety here in this example. Before I stated that the postfix increment happens after the assignment. (expressions are evaluated from left to right).
Found this in the online tutorial at:

" op++ : Increments op by 1; evaluates to the value of op before it was incremented

++op : Increments op by 1; evaluates to the value of op after it was incremented "
So in the expression i=i++;, i does increment, but gets evaluated to the value it was before it was incremented, which is 0.
0 gets assigned to the variable i (i=i++), which overwrites the original value of i which was 1. This is very tricky, but basically in how it gets evaluated, the value 1 gets overwritten with the value 0, due to how postfix notation evaluates the expression.
Hope this helps.
//Take a look at this code
//This should help you understand the subtlety
public class Test
public static void main(String[] args)
Test testVal = new Test();
public void test1()
int i=0;
System.out.println(i); //prints out 0 (assignment happens
//before increment
public void test2()
int i=0;
System.out.println(i); //prints out 1 (increments)

public void test3()
int i=0;
System.out.println(i);//prints out 1 (assignment happens
//after increment

The issue here is how expressions are evaluated
In the Java Language Specification 2.0
Section 15 deals with Expressions
Specifically 15.14.1 and 15.15.1 are the sections
you want to read.
For this expression: ++i;
(Prefix Increment Operator)
"Value of prefix increment expression
is the value of the variable _AFTER_
the new value is stored"
From this, I would gather that the expression
"i=++i" would equal "(++(= i i))" where i is
incremented before the assignment.
For this expression: i++;
(Postfix Increment Operator)
"Value of the postfix increment expression
is the value of the variable _BEFORE_
the new value is stored"
From this, I would gather that the expression
"i=i++" would equal "((= i i)++)" where i is
incremented after the assignment already
took place. So i gets initialized as 0, and
in the expression, i=i++; i is assigned 0
before i is incremented.

I am not going to write the code for you because the only way to truly know how to program is to practice by doing. I will give you some tips to point you in the right direction:
1) You need to store your elements so that you can easily access them. The easiest data structure for this implementation would be an array.
2) Create a method like 'int psuedoRandom()' which will return a random int value. There is a reference in the API for a Random class. You should start there:
*note: Make sure the value is within the number of elements that you have. Try to mod the value with the number of elements in your array, with % modulus operator.
3) In the method that prints out the phrase, you will want to print out the phrase by accessing it in the array with the random number you got back from the pseudoRandom() method as the index, like:
System.out.println("The phrase is :" + array[i]);

Hope this helps.
Java Language Specification.
Get it here:
You can download it in html, or pdf format (about 4.5 megs).
My question is quite simple. I currently have the "Complete Java 2 Certification Study Guide" by RHE (FIRST edition). I looked on the Sybex website, found the errata and also the fact the second edition has 936 pages, while the first edition has 912 pages.
Is there enough difference between the two editions, other then 24 pages, to spend $50 on the second edition?
Since I will be getting "A programmer's guide to Java Certification" by Mughal and "Exam Cram" second edition, I don't want to waste time or money with another copy of RHE if it is not worth it.
I understand lastest is greatest, but for sanity, I would need responses from the community about both editions.
I would appreciate responses from people that have used both editions. Thank you in advance.