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: simple java calculation

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simple java calculation

    Hello guys,

    I am currently learning Java. Excuse me for my stupid question. I have come across an example in my book which I don't quite understand. Can anyone explain me please?

    K has the value 10 and j == 5
    It says that the following expression should equal: k==0; j==4;. The expression is:

    k/= 25 * j--

    I think though that k==10 and j==4 because - we do the following:

    k/= 25 * (j=j-1)

    k/= 25 * 4

    k/= 100

    k= k/100

    k= 10

    As a result k==10 and j==4. Can anyone clarify please?

    Thank you


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,090
    My Mood
    Mellow
    Thanks
    244
    Thanked 752 Times in 738 Posts

    Default Re: simple java calculation

    I assume k and j are ints.

    What is the result of any int divided by a larger int?

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple java calculation

    int / int = int

    100:10 = 10 and not 0. This is the way I get it. Could you please clarify what do you exactly mean?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,090
    My Mood
    Mellow
    Thanks
    244
    Thanked 752 Times in 738 Posts

    Default Re: simple java calculation

    An int divided by a larger int, as in:

    int k = 10 / 100;

    What will k be?

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple java calculation

    ohhh..right..now I got it. How stupid..it will be 0. It will be 10: 100 and not 100:10.

    How about expressions like

    m = j++ + k. Can you tell me what will be the precedence order and the result given that m = 10, j=3 and k=2

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,090
    My Mood
    Mellow
    Thanks
    244
    Thanked 752 Times in 738 Posts

    Default Re: simple java calculation

    So that you know how to find these things yourself, try searching for 'java precedence' with your favorite search engine. The top 2 results I got from Google are both excellent.

Similar Threads

  1. simple calculation code
    By dokunbam in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2013, 06:55 AM
  2. Java calculation application
    By Shaw2380 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 28th, 2013, 08:14 PM
  3. Java array calculation
    By maple1100 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2013, 01:05 PM
  4. Java Calculation Incorrect
    By cow23 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 08:09 AM
  5. Calculation error...very New to Java..please be kind.
    By medicinaluser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2011, 12:16 AM