Gaurav Chauhan

Greenhorn
+ Follow
since Apr 14, 2008
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 Gaurav Chauhan

Thanks a lot mate.

I understood it. TextArea lost it's focus so the highligted text was not shown.

I used the requestFocusInWindow() as per your suggestion and it's working :-)
15 years ago
Here is my original code. I am supposed to build a find and replace box. This is what i did.....



Kindly run the code and you will find the issue that i am facing..........
15 years ago
Hi ranchers,

I am stuck in a problem while using the select method of TextArea class. Here is my code:





The select(int , int ) method inside the MyText() constructor is perfectly working but when i am using the same inside the find() method it is not selecting the text............

Anyone please help me on this.
[ September 14, 2008: Message edited by: Gaurav Chauhan ]
15 years ago
Thanks for your inputs.

This was a class assignment in which i can't call any method of String class except charAt().

I can't use the length field because to do so i have to convert this String into an array which will surely use String Class method.

So the conversion into array is only possible by using charAt() method but i can't found the end of line character in java.

I tried another approach and the code for same is below:



The above method worked fine.
If i pass '\u0000' by taking input from keyboard at runtime, it work fine .

But when i pass "Hi \u0000 j" the output must be 6 but due to my logic i get 3. My folks never caught this problem but i know where this method won't work ?



Is there any other solution you guys see to this problem ?
15 years ago
Hi ranchers,

Today i was writing my own length() method whose functionality is exactly similar to String.length(). My code is below:


I kept thinking on it right from the morning and i tried various approaches like extracting each charecter from string and comparing it. But none of the approaches gave me perfect result.


Now i wanna know is this a right approach to do the same ? Or we should not use the exceptions in our programs and try something else.

Kindly reply.
15 years ago
Sorry i meant while(a>b) needs a return statement not 3>2.

But the thing that comes out here is that true is a constant and while evaluating with 3>2 we are dealing with constants again so the compiler knows at compile time that any code beyond this will be unreachable.

In case of a>b the value of a and b can be changed inside the while loop or somewhere in our code and then the result might be false so the compiler checks for return statement.


Any more inputs RANCHERS ?
15 years ago
I noticed that while(true) need not to have a return statement but while(3>2) should have one.

But the question is why is this happening ? Both give true value inside the while condition.

Kindly describe.
15 years ago
So it means the identityHashCode return a number which is used to refer to object in memory ?

The object location can be changed by GC but the identity hashcode remains same if once assigned.

Another thing identity HashCode doesn't gives us the memory location.

Thanks for clearing the doubt.
15 years ago
Well my teacher uses this term very frequently that's why i used it too ? But based on above justification by senior members i would better use "hashcode" in place of ref-id.

Thanks again from stopping me from misleading myself.

The above discussion prompted me to do write some code and this is what i did ?


The above code prints the hashcode for string object ...... (This was the main question i was asking). Thanks for telling me the identityHashCode() method.


One more thing that i proved here is that String objects are immutable. In above case first 2 values of "sob" are same so their hashcode is same too but third value is different so a different memory location has been assigned to it.

Thanks a lot Ranchers. I got my answer.
15 years ago

Originally posted by Ernest Friedman-Hill:
Many, many classes override hashCode(). Since you're already looking at the source for java.lang.String, I'm surprised you didn't notice that String does so.



Thanks for letting me aware of this i just checked out the String class and saw that hashcode() has been overridden.


Anyway, you're thinking much too hard here. toString() returns a String, right? And so String::toString() returns itself. Whenever you print an object, toString() is called to return a printable String. String::toString() just happens to have a handy one available already!



I agree that toString() return a String. I agree with "so String::toString() returns itself."

But it is still unclear "String::toString() just happens to have a handy one available already!"


@ Mark
Thanks for informing about the hashCode() Overridden implementation in different classes.


Now all discussion is going well but i again re-iterate my query.

Is there any way to print the reference-id of object of String class as we does with objects of other classes ?


First question, when has Java ever give you a "reference id" when you return an reference of String class? Second question, what the heck is a "reference id"?

Henry



Java has never given me the reference-id for String Class and this is my question.

Reference-id: It is a unique number assigned to objects. http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/
15 years ago
My dear friend you said that hashCode() method is overridden in most classes. Will you please tell me the name of any class that does it ?

Even in java.lang.Object what i got is this ?

for toString() method


When i tried to look at the definition of hashCode() method, i didn't got any it is native ?




Now i can't see any class overriding hashCode() however toString() method is overridden and that is executed whenever we print any object.



@ Mark Vedder

I do know that hashCode() method is executed to get the memory value and toString() method just returns the same in a String Format.

Your code works fine but my friend it doesn't serves my purpose.
15 years ago
Hi members,

We know that whenever we try to print the reference variable of any class , the toString() method from current class or it's parent class is executed.



It happens to the reference variable of all classes. But today when i created a reference of String class and tried printing the same i got the value as output not the Address. When i checked the String class source code i came to know that String class has overridden the toString() method which it inherited from Object class. The code is below:



So if i try the program given below i get Hello as output.


In this case when i use System.out.println(ob) then the toString() method from String Class is executed whose code is given above. The toString() method of String class just returns the reference of String Class so i must get the reference id as output. But i am not getting it, i get the value.


I hope you guys have got my question . I have been thinking on it from morning and my mind is stuck so i might not be able to define my problem in a proper way but try to get my perspective and reply me.
15 years ago
Congo Nice score dude
15 years ago