aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator" Watch "Operator" New topic
Author

Operator

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
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

Joined: Oct 15, 2001
Posts: 21
You'd better check out the operator associativity preference in JLS. If you are only preparing for scjp, there won't such tricky questions.


----SCJP Java 2----
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
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
}
}
 
Don't get me started about those stupid light bulbs.
 
subject: Operator