• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Operator

 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic