aspose file tools*
The moose likes Beginning Java and the fly likes Questions about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Questions about "?:" and "shift"" Watch "Questions about "?:" and "shift"" New topic
Author

Questions about "?:" and "shift"

YingShiuan Chang
Greenhorn

Joined: Feb 18, 2004
Posts: 6
I have two questions:
1.This is a part of my program.

int i ;
i<=5?1:0;
System.out.println(i);

When it is compiled, the screen prints "not a statement".
i<=5?1:0;
^
What does it mean? Or where is the error?

2.I am confused with the usage of "shift".
If I shift a negative number, what's the meaning?
for example:
int x>>-4;
How to calculate it?

Thanks.
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
1) You need to do something with the expression such as assign it to create a statement. For instance
i = i<=5?1:0;
2) I believe shifting by a negative distance has no meaning
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

int i ;
i<=5?1:0;
System.out.println(i);
When it is compiled, the screen prints "not a statement".
i<=5?1:0;
^
What does it mean? Or where is the error?

First, the local variable 'i' is not initialized; you must explicitly initialize it
int i=0;
Secondly, the ternary operator returns a value and that value must be assigned to a variable
int i = 0;
int result = 0;
result = i<=5?1:0;
or you can print the result directly
System.out.println(i<=5?1:0);


byte i=125;
i<<-4

This shift expression is equivalent to i<<124. To learn more about negative shifting you can check out MM post here


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions about "?:" and "shift"