Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java I/O problemo

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java I/O problemo

    Please help me understand the code below works.
    hashcode() gives the same value everytime the program is run.
    class X
    {
    	public static void main(String args[])
    	{
    		int ans=results();
    		System.out.println(ans);	
    	}
    	static int results()
    	{
    	      	int h = 2;
    	      	int r = "APA".hashCode() % 3000;
    		System.out.println(r);	
          		int s = "UEJ".hashCode() % 3000;
    		System.out.println(s);	
    		for (int w = 0; w <= s; w++)
    		         h = (h ^ w) % r;
    	              return h;
     
    	}
     
    }
    Thank you


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Java I/O problemo

    Quote Originally Posted by skillet View Post
    Please help me understand
    What about it do you not understand?

    Quote Originally Posted by skillet View Post
    hashcode() gives the same value everytime the program is run.
    Have you read the API description for the String class?
    Java 7 String hashCode()


    Bottom line:
    The hashCode() method of the String class defines calculations with a fixed, documented algorithm that will give the same result for a given String object every time the program is run. Period.

    Post-bottom-line comment (if that's allowed):
    Note that the String hashCode() method overrides the Object class hashCode() method. The Object class hashCode() method is not guaranteed to give the same results for a given string for different runs of the program (unless it is overridden in a subclass).


    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java I/O problemo

    What I don't understand is the output value 1955.How is the same output value obtained everytime the program is executed?
    Thank you

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Java I/O problemo

    Quote Originally Posted by skillet View Post
    ....How is the same output value obtained everytime the program is executed?
    I don't know how to say it other than:

    The same calculations are done using the same finite sequence of well-defined operations on the same data values every time the program is run. Therefore, the result is the same every time the program is run. Period. Full stop.

    I mean, if the program performed a simpler calculation, say, 2 plus 2, wouldn't you expect the answer to be the same every time the program is run?

    If you want to know how that specific value is arrived at, then do the calculations "by hand." With pencil and paper and calculator.


    Cheers!

    Z

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java I/O problemo

    Thanks a ton for replying.
    Please tell me how to start calculating by hand.I really want to understand this program as this is the first time I have come across something like this.I never knew that the Object class had a method called hashcode.I know a little about hashing functions, hash tables and hash maps.Please guide me

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Java I/O problemo

    Quote Originally Posted by skillet View Post
    ...Please tell me how to start calculating by hand....
    Use the mathematical expression in the API reference to calculate the hashcode for "UEJ" as follows:

    The decimal integer value of the char 'U' is 85.
    The decimal integer value of the char 'E' is 69.
    The decimal integer value of the char 'J' is 74.

    The hashCode for this String is obtained by calculating the following:

    ((31 to the power 2) times 85) +
    ((31 to the power 1) times 69) +
    ((31 to the power 0) times 74)

    With my handy-dandy calculator:

    hashCode = 31*31*85 + 31*69 + 74 = 83898

    For a Java hashCode "manual" calculator, I would use Horner's nesting scheme to evaluate it.

    For example:


    //
    // Illustration of String hashCode calculations
    //
    // Zaphod_b
    //
     
    class Z
    {
        public static void main(String [] args)
        {
            // Try the following for another example
            //String testString = "This is a test string.";
     
            // This is the one that I did "by hand."
            String testString = "UEJ";
     
            System.out.println("testString = " + testString);
     
            int h1 = testString.hashCode();
            System.out.println("    testString.hashCode()      = " + h1);
     
            int h2 = manualCalcHash(testString);
            System.out.println("    manualCalcHash(testString) = " + h2);
     
        } // End main()
     
        static int manualCalcHash(String str) {
            int result = 0;
            for (int i = 0; i < str.length(); i++) {
                result = result*31 + (int)str.charAt(i);
            }
            return result;
        } // End manualCalcHash()
     
    } // End class definition

    Output:
    testString = UEJ
        testString.hashCode()      = 83898
        manualCalcHash(testString) = 83898



    Note that arithmetic in this program, as well as the arithmetic in the String hashCode() method, is done with 32-bit integers. If the partial sums in the evaluation are larger than the value of a 32-bit integer, the resulting overflow is silently ignored in Java.

    Try it again with testString set to something like "The quick brown fox jumps over the lazy dog." (I'm not going through that manually, but you can use my little example to show the results.)


    Quote Originally Posted by skillet View Post
    .I never knew that the Object class had a method called hashcode.
    It is entirely believable that one could spend a long, fruitful career writing Java code without ever encountering (much less having a need to know anything about) the String hashCode() method. But I recommend that when when you do "run across" such things, check with the Java API and see what it says. That's why I gave you the link in an earlier post.

    I know that seeing it is one thing but understanding what is involved may be a head-scratcher, and that's why I like to see folks ask questions about things that they don't "get" at first glance (or even second glance).



    Cheers!

    Z