harsha balluru

Greenhorn
+ Follow
since Feb 06, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by harsha balluru

I correct something here:
Kiran,
By defining the methods readObject() and writeObject(), you actually "implement" the methods; You can NOT "override" them. These methods you are implementing here come from the "Serializable" interface implemented by the class whose object's state you are trying to save.
In fact the classes java.io.ObjectOutputStream and java.io.ObjectInputStream have declared the methods writeObject() and readObject() as final.
Hi Henry

Thanks for the reply. I too coincidentally came across this idea of using lookahead and lookbehind. That resolved the problem...

I compiled this regex

(?<!\\[)\\[(?!\\[)

This lookaround concept is so awesome that it alleviates the issue when the "[" is either first or last character in the string...

Thanks
Harsha
14 years ago
A small correction..The output I am seeing is "IWRIT\[AVA".......

Thanks
14 years ago
Hi Folks

I am stuck with a problem of regex. I am trying to find a pattern which is basically "[" and wanted this pattern to be replaced with "\[" (for example [java should become \[java). But at the same time I dont want my operation to affect any strings that have "[[" (i.e., I do not want [[java to become \[\[java )....

So to accomplish above I am doing this as part of my regex program:


String input = "IWRITE[JAVA"
Pattern p = Pattern.compile("[^\\[](\\[)[^\\[]");
Matcher m = p.matcher(input);
String replace = "\\" + m.group(1);
String converted = m.replaceAll(replace);
System.out.println(converted);



With the above part, the output is "IWRIT[AVA"..What is happening is instead of replacing "[" by "\[" ; the "E[J" is replaced by "\["...How to fix this?? Any ideas??

Thanks
Harsha
14 years ago
Sorry.... My mistake...I realized that I invoked an object of SuperClass type. But what if I change the statement like this:

import java.util.*;
public class MapTest extends HashMap {

public static void main(String args[]){
HashMap<String, Integer> mps = new MapTest<String, Integer>();
mps.put("Tom",new Integer(26));
mps.put("Steve",new Integer(33));
mps.put("Kelly",new Integer(27));
System.out.println(mps);
}

public String toString(){

return "Tom is 26, Steve is 33, Kelly is 27";

}

}
14 years ago
I am curious why the following code does not honor my override of toString() method. Any ideas???




//Class to test the override of toString method
import java.util.*;
public class MapTest extends HashMap {

public static void main(String args[]){
HashMap<String, Integer> mps = new HashMap<String, Integer>();
mps.put("Tom",new Integer(26));
mps.put("Steve",new Integer(33));
mps.put("Kelly",new Integer(27));
System.out.println(mps);
}

public String toString(){

return "Tom is 26, Steve is 33, Kelly is 27";

}

}
14 years ago
I see that ArrayList is mainly for faster iteration and fast random access while LinkedList for faster insertion and deletion. Both are ordered but not sorted. My question is that "does anyone disagree if I say LinkedList is ordered by 'insertion order'(or last accessed order)?"
14 years ago
Hi Madhu

Thanks for the clarification. As you quoted, the parantheses made the difference there. With that in place, K&B's explanation is correct...

Hi

I just observed a small error in K&B SCJP 6.0 in "Operators" chapter. The question 9 in the self test is incorrectly explained in the answer section. The question is as follows:

Given:

3. public class Spock{
4. public static void main(String args[]){

5. int mask=0;
6. int count=0;

7. if( (5<7) || (++count < 10) | mask++ < 10) mask = mask+1;
8. if( (6>8) ^ false ) mask = mask+10;
9. if( !(mask > 1) && ++count > 1) mask = mask + 100;

10. System.out.println(mask + " " + count);
11. }
12. }

Which two answers are true about the value of mask and value of count?
A. mask is 0
B mask is 1
C mask is 2
D mask is 10
E mask is greater than 10
F count is 0
G count is greater than 0

The correct answer is "mask is 1 and count is 1";

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In the answer's explanation, it is said that the non-short-circuit OR "|" allows the mask to be incremented which is conflicting. The short-circuit operator on that line makes the compiler not bother the right side of the equation. So effectively mask value is "1" after the line 7 is complete.
On line line 9, the left side of the && operator, !(mask>1) evaluates to true but right side value, ++count > 1 executes and evaluates to false and hence the "if" condition will not pass.
Thus finally the mask and count values are both "1".

Cheers
Harsha[color=darkblue]
[/color]
Thanks..I understand now...
15 years ago
It compiles.... Don't you think compiler should warn about this overriding where you will make the superclass' variable unusable.
15 years ago
Oh..Thanks Fred.. I could not think that deeper. I see what you are saying.

But again if the non abstract subclass has a static variable with the same name, then the superclass' non-static variable value will be overridden correct?
15 years ago
I tried declaring a non-static variable in an abstract class. The compiler did not complain. My concern is what can we do with a non-static variable inside an abstract class? You can not instantiate an abstract class anyways to access it. Alternatively think that you may use that variable in a non-abstract method in the same class. But again there is no way you can access that non-static non-abstract class because you can not instantiate an object. My question here is ... Do we see any logic in creating a non-static variable inside an abstract class?
15 years ago