Monday, 23 April 2012

difference between two most toString() methods in java !

hello every one java is based on strings , means every thing which you see is presented by strings , and every thing is presented with the help of  "toString()" method . now see why this thing arose in my mind !



at the time of coding , i implement this easily but at the time of  run-time i found something which does not suits in my eyes so i tried to found the reason behind that output , but before this see the output !


see there are 4 lines and each one in presenting their output according themselves , but why , what is the problem with "System.out.println(xxx.toString());" , but the point is that there are few things which are noticeable in seeing , that ::
1st line is output of reference.toString() ;
2nd line is output of only class variable ;
3rd and 4rth are different from 1st one , 3rd and 4rth are output of  String.toString() ;

so , definitely the problem or solution is situated in the toString() method of reference classes , means toString() of String and toString() of now_my_own_rules class , now look at the two different methods of both classes .

first look at the toString() method of String class .


see the lined area , and see the line which is put inside the two black boxes , means when ever you enter any string in any String object , it will return the string itself .


now also see the , the lines added inside the boxes , here you will find the String representation of object , but here i override the toString() method thats why it is printing something else , otherwise it will print the class_name@hexadecial values ..


thanks ,
tushar ..

Friday, 20 April 2012

what is the problem with equals and == !

see in java , great thing about string is that , string is primary concept in java , means every thing in java is presented by string , you want to print something , you want to show someone that they have created that thing at that time you also need strings , every api is added with the help of strings { means you use text }.

now see two great points about strings . " equals " .......  " == " ! but i am here to show or placing the problems introduced by "equals" and "==" but before this understand why we use them , 

Equals ::  equals always deals with the content present in the object , now look at this , 


see if you use equals it will deal with 
::: pillion seat 
::: light 
::: tyre 
::: glass

" == " ::  now we have "==" in java , it will deal with who is holding , means if EARN is having that car of DUKE have that car ! "==" compares the reference , look at this .


Problem associated with "EQUALS" and "==" :: now the main points comes in , see we have two human "duke" and "earl"  now a car is standing in parking and earl takes duke's car and went to market for buying something now duke comes in parking and search his car , and did not find his car and after sometime he saw that earl is using his car , after it they fight with each other , now think how they will solve their problem by "equals" and "=="  



now "DUKE" and "EARL" use 

 ::: "equals" for solution and find that and find that they use same content glass , pillion seat , tyre .

 ::: "==" for solution and finds that they use same car , and drive same car 

so in the end , they have no solution and still they are fighting , so we have to override our "equals()" in java .


 

Sunday, 8 April 2012

in java , memory for strings allocation is performed inside fixed location !


hello every one , after a long time i am writing post but i am not a good writer but please understand this topic , i am interested in writing blogs but before my interest , it is necessary to explore the concept behind memory allocation for strings , so why i am saying this , this is the point !
i am working with string and i just find a hashCode() method , and i just find the hashcode() of "zzzz" and "zzza" and between this i found relation , that the difference , between their hashcode() depends upon their ascii code , and after it i recompile it and try to find out , the dynamic allocation of memory for "aaaa" and "zzzz" , so the hashcode() generated for these two strings is always same . 
so every string which have length == 4 lies between ... 1477632 to 387884 { entered from keyboard }
now the question in your mind will arise that how i am finding this range see the ascii table from wikipedia "http://en.wikipedia.org/wiki/ASCII"  so from this we get the characters entered from the keyboard !
now how this thing knocks in my mind , i am performing this thing which is displayed in this picture 

so jvm uses dynamicness but it stored fixed position in storing things !