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 2 of 2

Thread: Quick question (intergers and accuracy)

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Quick question (intergers and accuracy)

    Hey, why doesn't this work. The rest of the program doesn't really effect how this works, so I'm only going to past the bit of code that Im curious about.

    	public static double Average(int total)
    	{
    		double average= (double)(total/10);
    		return average;
    	}

    When I leave the code as it is, it displays the answer as an integer, when the answer should have a decimal point.

    I can fix it by removing the brackets around the total/10 and it then produces the answer with the decimal point. I was just wondering, why do I have to remove the brackets? Surely they are the same thing :O


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Quick question (intergers and accuracy)

    It's the order of operations.

    (double) (total/10) evaluates as such:

    1. take the integer total and divide by the integer 10. This is an integer number.
    2. Cast that number to a double. An integer casted to a double looks like an integer.

    Now if you have the other way around,

    (double)total / 10:

    1. cast total to double.
    2. Divide the double value of total by the integer 10. 10 is implicitly casted to a double before the division, the result is a decimal number.

    This second form is equivalent to ((double)total)/10

Similar Threads

  1. scope - quick question
    By bbr201 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2010, 08:30 AM
  2. quick question - new keyword
    By bbr201 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 18th, 2010, 09:43 PM
  3. Quick Question about Mergesort
    By Shadow703793 in forum Algorithms & Recursion
    Replies: 4
    Last Post: March 4th, 2010, 05:48 PM
  4. Quick JComboBox Event Question
    By -tr in forum AWT / Java Swing
    Replies: 2
    Last Post: December 12th, 2009, 05:35 PM
  5. Quick binary tree question.
    By Sinensis in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 09:28 PM