• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a question about "?:"

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>public class Test{
public static void main(String[] args){
for(int i = 0; i < args.length ; i++ )
System.out.println( i==0 ? args[i] : " " + args[i] );
}
}</pre>
after input 'java Test a b c ' at commandline,why the output is:
a
b
c
i think it should have output:
aa
b
c
please tell me

(Marilyn corrected spacing so for loop shows properly)

[This message has been edited by Marilyn deQueiroz (edited November 18, 2001).]
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Why do you think the output should look in your way?
The single effect of the
i==0?args[i]:" "+args[i] instead of args[i] is the fact that "a" will be moved with on char to the right.

..Cristian
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The colon ( is not an "and", it is an "or"

if ( i==0 is true ), println( whatever is before the colon )
else, println( whatever is after the colon )

You will never get what is before and what is after the colon.
[This message has been edited by Marilyn deQueiroz (edited November 19, 2001).]
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can this quetion come in exam? I dont think so!
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have never seen a for loop like this.Please could anyone tell What is the for loop syntax here.
Thanx
NEha
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(int i = 0; i < args.length ; i++ )
without the space after the < comes out like
for(int i = 0; i <args.length ; i++ )>

If you click on the pencil icon, you will see that the two lines are identical except for the space after the < on the second line.

The UBB software interprets the < whatever (with no space) as an html tag, so it becomes invisible.

[This message has been edited by Marilyn deQueiroz (edited November 18, 2001).]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i contradict because loop works fine but in println statement there is a condition whatever the result is depending upon the variable i but args[i] after that condition should be printed at any cost.
Correct me if i am Wrong?
------------------
Regards
Farrukh Mahmud
[This message has been edited by Marilyn deQueiroz (edited November 19, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

java Test It works

output:
It
! works

The first time args[i] = args[0] and i == 0, so args[0]("it") gets printed.
The second time args[i] = args[1] and i != 0, so "! " + " "+args[1] ("! works") gets printed.

[This message has been edited by Marilyn deQueiroz (edited November 19, 2001).]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,all
i think this problem is mainly concerned with Evaluation Order,The order, from high precedence to low precedence, is as follows:
1.++ postfix, --postfix
2.++ prefix, -- prefix, + unary, - unary, ~, !
3.(type) casting
4.*, /, %
5.=, + (binary), - (binary)
6.<<, >>, >>>
7.<, >, <=, >=, instanceof
8.==, !=
9.&
10.^
11.|
12.&&
13.| |
14.?: (ternary operator)
15.=, +=, -=, *=, /=, %=, >>=, <<=, >>>=, &=, ^=, |=
it's clear that '+' has a higher precedence than ':'
,so the output maybe ......
------------------
Tony Sam
One want to be a SCJP...
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic