harsha karanam

Greenhorn
+ Follow
since Nov 14, 2016
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
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by harsha karanam

Question 1:

Do we need to override the clone method if we implement the Cloneable interface, if there is no specific functionality defined.

If we have to override , what is the benefit of such override.

Question 2:

The below code is from other site (Source:http://javaconceptoftheday.com/difference-between-shallow-copy-vs-deep-copy-in-java/) ,

In the deep copy example


In the deep copy example the clone method definition is modified , even if we don’t override it and have another course object, add it to the student object and use the set method for changing the field , its going to behave the same.

What is the benefit of such override?

Example similar to the above:

Course course=new Course(“subj1″,”subj2”,"subj3");
Student std=new Student(123,“student1”,course);
Student std2=(Student ) std.clone();
Course course2=new Course(“subj1″,”subj2”,"subj3");
std2.setCourse(course2);

is this definition and the overrided clone method definition are the same?
7 years ago

Junilu Lacar wrote:

sreeharsha Karanam wrote:then in real time scenario when is the nextIndex method useful?


To paraphrase Iñigo Montoya: "You keep using that phrase; it doesn't actually mean what you think it means." -- again, you probably mean "real-world" not "real time".



thanks for the correction
7 years ago
Yes i have looked in to the documentation , during the iteration of the list using the method next it returns the element and moves to next position but in the case of nextIndex it returns the index position but it does not move the position
7 years ago
now i get it that its not necessary to product distinct integer results if objects are not equal but if it produces it enhances performance, but am curious to know whether we can make such sample code where objects are not equal using equals method but hashcode produces same integer results for the objects.
7 years ago
Correction in the topic heading it has to be previousIndex

[moderator edit: correction to subject text made]
7 years ago

When we use the next method of the ListIterator interface it returns the element and moves to the next position

in the case of the nextIndex or previousIndex method it returns the element position but not moving the next position , then in real time scenario when is the nextIndex method useful?
7 years ago
Hello Junilu ,

The case where the objects are unequal using equals method , my understanding is if objects are unequal in that case the hashcodes of the objects must be unequal, consider the below example.

Country.Java:


Test.java:



Output:
Both countries are not equal
Country 1 hashcode:70793526
Country 2 hashcode:-1514818128

this case i was able to get different hashcodes , i believe this is the point you were trying to highlight in the post.

But in the other case  i was not able to have an example that s how i started this thread ;)
7 years ago
Thanks Henry,

that was a nice example where we run in to the scenario of having same hashcodes for two objects but the objects are different
7 years ago
I would like to know any real time scenario example where hashcodes are equal for two objects but the objects are not equal
7 years ago