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

Operator

 
Fisher Daniel
Ranch Hand
Posts: 582
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can you help me to explain about my code
public class A
{
public static void main(String[] args)
{
int x = 5;
System.out.println(x-x); // print : 0
System.out.println(x - x); // print : 0
System.out.println(x---x); // print : 1
System.out.println(x-- -x); // print : 1
System.out.println(x- --x); // print : 1
System.out.println(x- - -x); // print : 0 System.out.println(x-----x); // compile error
System.out.println(x-- - --x); // print : 2
System.out.println(x--- --x); // print : 2
System.out.println(x-- ---x); // compile error
System.out.println(x - - - - - x);// print : 0
}
}
thanks
daniel
 
Alan Mar
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd better check out the operator associativity preference in JLS. If you are only preparing for scjp, there won't such tricky questions.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to check JLS 3.2. It explains how the source code is tokenized.
From JLS

The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would. Thus the input
characters a--b are tokenized (�3.5) as a, --, b, which is not part of any grammatically correct program, even though the tokenization a, -, -, b could be part of a
grammatically correct program.

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fisher,
The first two is obvious that are printing 0 and for java
these two are equivalent
System.out.println(x---x); // print : 1
System.out.println(x-- -x); // print : 1)
{x is decremented first time but the negating value is 5 but the next time the x is consdired as 4}
(x- --x)==> (5-4)
(x- - -x) ==> (5-(-(-4))) ==> (5-4) ==>1
(x-- - --x); // print : 2
here it is (5-3)x-- yields x = 4, but for the first time the negating value is 5 and --x yield x=4-1 which is 3.
Others are self explanatory.
Hope this helps.
Nisheeth.

public class A
{
public static void main(String[] args)
{
int x = 5;
System.out.println(x-x); // print : 0
System.out.println(x - x); // print : 0
System.out.println(x---x); // print : 1
System.out.println(x-- -x); // print : 1
System.out.println(x- --x); // print : 1
System.out.println(x- - -x); // print : 0 System.out.println(x-----x); // compile error
System.out.println(x-- - --x); // print : 2
System.out.println(x--- --x); // print : 2
System.out.println(x-- ---x); // compile error
System.out.println(x - - - - - x);// print : 0
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic