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

Thread: on unary operators

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default on unary operators

    DIFFERENCE BETWEEN THESE TWO PROGRAMS PLEASE EXPLAIN ME IN DETAIL HOW IT WORKS



    public class a
    {
    public static void main(String[] args)
    {
    int i=0;
    int j=i++ +i++ + i + i;
    System.out.println(i);
    System.out.println(j);
    }
    }



    AND


    public class B
    {
    public static void main(String[] args)
    {
    int i=0;
    int j=i++ + i + i + i++;
    System.out.println(i);
    System.out.println(j);
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: on unary operators

    Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers..

    These two lines:

    int j=i++ +i++ + i + i;

    int j=i++ + i + i + i++;

    are different. What about the differences don't you understand or about the results caused by them?

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: on unary operators

    am getting different outputs and i ve doubt how i value is passing pls clear my doubt

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: on unary operators

    Write down:

    (first line without unary operator)

    j = i + i + i + i

    substitute 0 for i, calculate j. Shat does j equal?

    write down (or modify the line in the program) to:

    j = i++ + i + i + i

    substitute 0 for i. what does that equal?

    write down (or modify the line in the program) to:

    j = i++ + i++ + i + i

    Keep going until YOU figure it out. You don't need anyone's help with adding together numbers less than 5.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: on unary operators

    Another way to see what each expression evaluates to and when it returns its value is to use println statements:
          int i = 1;                        //  set initial value
          System.out.println("i++="+i++);    // show value returned by i++
          System.out.println("i="+i);         //  show value AFTER i++
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Operators
    By Skybear in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 11th, 2012, 01:53 PM
  2. Help with logical operators
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:35 AM
  3. Evaluation of operators in expressions.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: September 23rd, 2011, 07:23 PM
  4. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 06:26 PM
  5. Need Help with Operators/ logic
    By codekiller in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 09:25 AM