Page 1 of 2 12 LastLast
Results 1 to 20 of 29
Like Tree1Likes

Thread: What will be the output?

  1. #1
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Question What will be the output?

    class test //line 1
    { //line 2
    public static void main(String args[]) //line 3
    { //line 4
    double a=10, b=20; //line 5
    b=a---b; //line 6
    System.out.println(a); //line 7
    System.out.println(b); //line 8

    }

    }

    what will line 6 will out? it is very confusing...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: What will be the output?

    Quote Originally Posted by rahil.khan View Post
    Java Code:
    class test                                                                     //line 1
    {                                                                                //line 2
    	public static void main(String args[])                    //line 3
    	{                                                                       //line 4
    		double a=10, b=20;                                    //line 5
    		b=a---b;                                                    //line 6
    		System.out.println(a);                                 //line 7
    		System.out.println(b);                                //line 8
    		
    	}
    
    }
    what will line 6 will out? it is very confusing...
    If you had some proper spaces in there then you would have:
    Java Code:
    b = a-- - b;
    Does that help?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    yeah its helped but my question is why the jvm is taking it as "a--" why not "--b"?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: What will be the output?

    It's not the JVM, it's the compiler, and I suspect it's the parsing.
    Exactly what in the parsing I would be guessing, though. Either how it reads a line (left to right) or however it builds up the tokens for a statement.
    Jos does compiler stuff, not me...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    thnx Tolls... is Jos online??? will he reply to this thread???

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: What will be the output?

    Depends if he's started on the Grolsch yet or not.
    ;)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    ok... but whats grolsch??

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What will be the output?

    I checked the JLS and it says it parses left to right with a single look-a-head token (but doesn't cite this specific example).
    The compiler also employs tricks to compensate for special cases. Bottom line is that for something like a---b, someone had
    to decide how it would be interpreted and they did.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: What will be the output?

    Quote Originally Posted by jim829 View Post
    I checked the JLS and it says it parses left to right with a single look-a-head token (but doesn't cite this specific example).
    The compiler also employs tricks to compensate for special cases. Bottom line is that for something like a---b, someone had
    to decide how it would be interpreted and they did.

    Regards,
    Jim
    I had a quick skim of it, but couldn't find that.
    Thanks for being more persistent than me!

    So single character look ahead gives us:
    find '-'.
    lookahead finds another '-', which equates to a '--', so we'll apply that to the 'a'...and so on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    i very new to this technology, but finds it awsm... really... till its been interesting one... i want to knw what is JLS and one more thing what if it becomes..... "d= a---b = f" where d and f are integer.... how the compiler will takes that...

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What will be the output?

    The JLS (v 8). The second italicized paragraph under section 15.8. I presume that except for special cases and tricks
    that parsing is left to right. And more special tricks are mentioned in 15.15 Unary operators but again, not for the
    cited issue.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    i still dont get it?? what is JLS?

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What will be the output?

    The Java Language specification tells you at a very anal level, what one should know about the syntax of the Java
    Language. The Java Virtual Machine specification tells you about the byte codes and other information on how to implement
    the JVM. Then all you need is the documentation for the target platforms native libraries, a very big brain, and a masochistic
    personality and you could write your own java compiler and JRE. :)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    sry for the trouble... and thank you so much.. it was a great help for me....

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: What will be the output?

    The tokenizer is 'eager', i.e. it reads as many characters as it can to deliver a next token; so a---b is tokenized as 'a', '--', '-', 'b' instead of 'a', '-', '--', 'b' or 'a, '-', '-', '-', 'b'.

    kind regards,

    Jos

    ps. @OP: Grolsch is the best Dutch beer.
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    class test
    {
    public static void main(String args[])
    {
    double a=10, b=20;
    b=a*b++; // line 6
    System.out.println("A = "+a);
    System.out.println("B = "+b);

    }

    }

    Output:

    A = 10.0
    B = 200.0

    why the increment operator is neglected by the comiler on line 6?

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: What will be the output?

    It isn't neglected and most certainly not by the compiler (the poor thing is completely innocent); what happens in the a*b++ expression is this:

    1) take the value of a (10)
    2) take the value of b (20)
    3) increment b and assign it to b again (21)
    4) multiply the values in 1) and 2) and assign it to b (200)

    As you might have seen, the value 21 is lost because of step 4).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  18. #18
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    you mean 200 is assigned to b before it gets incremented.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: What will be the output?

    Quote Originally Posted by rahil.khan View Post
    you mean 200 is assigned to b before it gets incremented.
    No, I mean that b is incremented and assigned the new value in step 3) (see above).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  20. #20
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: What will be the output?

    is it possible to print that value of b I mean 21.

Page 1 of 2 12 LastLast

Similar Threads

  1. What is this Output !
    By Nazneen Ali in forum New To Java
    Replies: 11
    Last Post: 10-28-2013, 10:40 AM
  2. Replies: 2
    Last Post: 05-03-2012, 11:39 AM
  3. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  5. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 10:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •