File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions about "?:" and "shift"

 
YingShiuan Chang
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic