Help coderanch get a
new server
by contributing to the fundraiser

Chris Allen

Ranch Hand
+ Follow
since Feb 01, 2003
Merit badge: grant badges
For More
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 Chris Allen

I too had this question and the response I got from this forum was that when you are using the 'greedy' quantifier, it reads from right to left. Thus, it consumes the 3 and 4 at the same time and skips the 3rd position (i.e. the 4 in the string to searched). I too am still somewhat confused as to why an additional index position shows up in the result (i.e. 6).
I am a little confused on why the m.find() function always interrogates one position beyond the length of the string passed in. Is it because of the 0 to many identifier (the asterisk)? When I try it with \d+ instead, it does what I expect (produces 234 ). I would have expected the method to only go for as many positions that are available (i.e. only produce 0123445).
Can someone post a link where I can download the source for the base java classes (i.e. Integer class). I previously downloaded the entire 1.4 source but can't seem to find a 1.5 equivalent.
18 years ago

Originally posted by Vejaykrishna:

In K&B book p:112 there is an example program which explains reference variable casting. can anybody explain me line: 3 and 4.

1. class CastTest2{
2. public static void main(String [] ar){
3. Animal[] a={new Animal(), new Dog(), new Animal()};
4. for(Animal animal : a){
5. animal.makeNoise();
6. if(animal instanceof Dog){
7. animal.playDead();
8. }}}}

Line 3: just creates an array of objects, all of which can be casted to an Animal object (since the array is of type Animal). The new Dog() object is a subclass of Animal and hence, can be upcasted to an Animal object and the other two elements are Animal objects.

Line 4: an example of the new enhanced for-loop in 1.5. It is initializing an object named animal which is of type Animal which will be assigned to each of the elements in the array created in line 3.

In the first iteration it animal will point to the first element in the a array (the new Animal object); in the second iteration, a will point to the second element in the array (the new Dog object). In the final iteration, it will be assigned to the second new Animal object created on line 3.

Does this answer your question?
It is because of your for loop condition. You are initializing i=10 and then checking to see if i<0. None of the code inside the for loop is executed which leaves the value of x=0.
You don't need to know this for the exam but just for the record, the name came from an Oak tree outside of Gosling's window, I believe when he created the language.
Putting a slight twist on the question. Is it common practice to include multiple classes (obviously not public) within one source file? It likely makes sense if the classes are related or you want to have all of your classes in a given package inside one file but other than that, I don't see any advantage. Is there something I am missing?

You stated the following:

If a unchecked exception is thrown in try block, that will be printed after executing the finally block.

All I wanted to indicate was that a Runtime exception will NOT ALWAYS be printed (as it was caught in a try block). You are correct that it will display if not caught by a catch block.

Perhaps we could agree that if you modified the statement to be "If an unchecked exception is thrown in a try block that is not caught by an exception clause, the runtime exception will be printed after executing the finally block".
Thanks for the correction on String. I knew there were 8 in total so the other three I missed were short, boolean, and byte. Good question on the protected modifier. Perhaps they only want these methods to be called by objects and not the primitives.
Not really sure if there is a specified "contract" on what a hashcode method must do (other than return an int). The only thing to keep in mind is the following:

1. If two objects point to the same reference (i.e. a == b), then the hashcode for a and b MUST return the same value.

2. If the hashcode for a and b are equal, the objects MAY point to the same reference, but not necessarily.

If you are going to write your own custom hashcode method, make sure the above two items still hold true. Normally, the equals method is overriden if the hashcode method is, but not always (it depends on the implementation).
The only classes which do not extend Object are the primitives (i.e. int, float, double, char, long, String).

Originally posted by Naren Chivukula:
Not always this happen. If a unchecked exception is thrown in try block, that will be printed after executing the finally block.

Hmmm, I don't think this is true. Consider the following code below:

The output is "Peace" (without the quotes) and the RunTimeException error is not displayed.
In the first example, the variable i is defined as final and the compiler can determine at compile time that the value 120 will fit into x. Without the final modifier, it can only be done at runtime. Since it is a narrowing conversion, it will require an explicit cast such as:

Unless the two objects point to the same reference in memory, the expression will always be false and hence, never assign the value. I am guessing you want to use the .equals() method instead of == to test whether the objects are equivalent (data-wise) rather than pointing to the same reference in memory.
18 years ago
Oops , let me correct one statement:

Only once A's constructor is completed will the value of A's instance variable i be initialized to 7.

This should be b's constructor is completed will the value of A's instance variable i be initialized to 7.

Here is the modified code I used which should demonstrate this: