Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By ShadowWalker
  • 1 Post By DarrylBurke

Thread: Java Code

  1. #1
    Rubel Rana is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Java Code

    Java Code:
    class Hell{
          [color=blue]public[/color] static void main(String args[]){
          [color=blue]int[/color] i = -1;
    		System.out.println((i<0)?-i:i);
    	}
    }
    why this output is 1?
    Last edited by Rubel Rana; 08-03-2013 at 12:45 PM.

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: Java Code

    because this statement (i<0 ? -i : i) means :
    if i < 0 then do -i
    else do i so in your situation i = -1 so
    i < 0 = true then do -i = -(-1) = 1 ;)

    it's like this :

    Java Code:
    if(i < 0){
    	System.out.println(-i);
    }else{
    	System.out.println(i);
    }
    Last edited by ShadowWalker; 08-02-2013 at 05:53 PM.
    Rubel Rana likes this.

  3. #3
    Rubel Rana is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Java Code

    Oh thx brother :)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Java Code

    Please go through the Forum Rules, particularly the third paragraph.

    Also go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and learn how to post code here.

    db
    Rubel Rana likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Translate Vb.net code into Java code
    By Radu in forum New To Java
    Replies: 5
    Last Post: 04-12-2011, 09:27 AM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM

Tags for this Thread

Posting Permissions

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