alias

Discussion in 'Java' started by NickName, Dec 18, 2006.

  1. NickName

    NickName Guest

    Hi,

    I may be jumping guns here. I mean I'm totally new to java and yet, I
    feel the need to do something like this for typing much less,

    o = System.out.println; // problem, what data type for var of o here?
    undefined.
    or
    alias o = System.out.println; // alias is supposed to be a special
    command or the sort?

    then, I do something like this

    for (int i=0; i < 5; i++) {
    switch(i)
    case 0:
    case 2:
    case 4:
    o(i + " is an even number"); // instead of System.out.println(i +
    " is an even number");
    break;
    case 1:
    case 3:
    case 5:
    o(i + " is an odd number"); // instead of System.out.println(i + "
    is an odd number");
    break;
    default:
    o(i + " is neither an odd nor even number");
    // instead of System.out.println(i + " is neither an odd nor even
    number");
    }

    Doable? How? TIA.
    NickName, Dec 18, 2006
    #1
    1. Advertising

  2. And thus, NickName spoke...

    > I may be jumping guns here. I mean I'm totally new to java and yet, I
    > feel the need to do something like this for typing much less,


    If you want to make your code unreadable, there are more easy ways to go...

    > o = System.out.println; // problem, what data type for var of o here?
    > undefined.
    > or
    > alias o = System.out.println; // alias is supposed to be a special
    > command or the sort?


    Doesn't exist - afaik. You could, of course, simply write...

    public void o(String message) {
    System.out.println(message);
    }

    > then, I do something like this
    >
    > for (int i=0; i < 5; i++) {
    > switch(i)


    Strange way to ask for...

    i % 2 == 0

    or

    i & 1 == 0

    Flo
    Flo 'Irian' Schaetz, Dec 18, 2006
    #2
    1. Advertising

  3. NickName

    NickName Guest

    Flo 'Irian' Schaetz wrote:
    > And thus, NickName spoke...
    >
    > > I may be jumping guns here. I mean I'm totally new to java and yet, I
    > > feel the need to do something like this for typing much less,

    >
    > If you want to make your code unreadable, there are more easy ways to go...
    >
    > > o = System.out.println; // problem, what data type for var of o here?
    > > undefined.
    > > or
    > > alias o = System.out.println; // alias is supposed to be a special
    > > command or the sort?

    >
    > Doesn't exist - afaik. You could, of course, simply write...
    >
    > public void o(String message) {
    > System.out.println(message);
    > }


    Thank you. That's exactly what I'm looking for. And with a little
    twist, I changed it to

    // alias, creating short hand for some commonly used command, DL
    public static void o(String msg) {
    System.out.println(msg);
    }

    // added static because the caller uses static

    Now, since we are at it and talking about readibility (pls note, I add
    comments for short hands), a new one regarding a file's date time
    stamp, the method of lastModified() seem to be the aggregation of
    million seconds something as in

    File myfile = new File(thisFile.txt);
    long fileDate = myfile.lastModified();

    What method to display date/time like mm/dd/yyyy or mm--dd--yyyy?
    Sorry I did not go the trouble of digging it via language reference doc

    TIA.

    >
    >[...]


    > Flo
    NickName, Dec 18, 2006
    #3
  4. NickName <> wrote:

    > I may be jumping guns here. I mean I'm totally new to java and yet, I
    > feel the need to do something like this for typing much less,


    > o = System.out.println; // problem, what data type for var of o here?


    In other languages, o might be a function pointer, but in Java you're
    out of luck. The best you could do, saving yourself a little bit of
    typing, would be something like

    final PrintStream o = System.out;
    o.println( "Hello, world!" );

    although it really isn't worth the effort. The alternative, using
    reflection to get the println Method from System.out, will save you
    neither typing nor brain CPU cycles.

    > alias o = System.out.println; // alias is supposed to be a special
    > command or the sort?


    You might be thinking of shells and shell scripts; no such thing
    exists in Java.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 18, 2006
    #4
  5. Christopher Benson-Manica wrote:
    > NickName <> wrote:
    >
    >
    >>I may be jumping guns here. I mean I'm totally new to java and yet, I
    >>feel the need to do something like this for typing much less,

    >
    >
    >>o = System.out.println; // problem, what data type for var of o here?

    >
    >
    > In other languages, o might be a function pointer, but in Java you're
    > out of luck. The best you could do, saving yourself a little bit of
    > typing, would be something like
    >
    > final PrintStream o = System.out;
    > o.println( "Hello, world!" );
    >
    > although it really isn't worth the effort. The alternative, using
    > reflection to get the println Method from System.out, will save you
    > neither typing nor brain CPU cycles.


    Something similar does sometimes come up where you want to "wrap"
    something like System.out.println. For example, you want to log certain
    events, but the exact nature of the logging should be irrelevant to
    whatever does the logging and should be changeable in a single place.
    Then you do this:

    public interface Logger {
    public void log (String message);
    }

    //Somewhere else
    public class StdoutLogger implements Logger {
    public void log (String message) {
    System.out.println(message);
    }
    }

    //Somewhere else again
    public static void main (String[] args) {
    Logger logger = new StdoutLogger();
    ...
    someobject.doSomething(logger, other_args);
    ...
    }

    //Somewhere else again
    public void doSomething (Logger logger, other_args) {
    ...
    logger.log("Foo");
    ...
    catch (IOException e) {
    logger.log("Oops! I/O error");
    logger.log(e.toString());
    <some sort of recovery>
    }
    ...
    }

    Later on you might want to use some other Logger. You can make an
    AggregatingLogger: (assumes Tiger)

    public class AggregatingLogger implements Logger {
    private List<Logger> members;
    public AggregatingLogger () {
    members = new LinkedList<Logger>();
    }
    public void add (Logger logger) {
    members.add(logger);
    }
    public void log (String message) {
    for (Logger logger : members) {
    logger.log(message);
    }
    }
    }

    Just be careful not to add an aggregating logger to itself, OK? :)
    John Ersatznom, Dec 18, 2006
    #5
  6. NickName

    Daniel Pitts Guest

    NickName wrote:
    > Hi,
    >
    > I may be jumping guns here. I mean I'm totally new to java and yet, I
    > feel the need to do something like this for typing much less,
    >
    > o = System.out.println; // problem, what data type for var of o here?
    > undefined.
    > or
    > alias o = System.out.println; // alias is supposed to be a special
    > command or the sort?
    >
    > then, I do something like this
    >
    > for (int i=0; i < 5; i++) {
    > switch(i)
    > case 0:
    > case 2:
    > case 4:
    > o(i + " is an even number"); // instead of System.out.println(i +
    > " is an even number");
    > break;
    > case 1:
    > case 3:
    > case 5:
    > o(i + " is an odd number"); // instead of System.out.println(i + "
    > is an odd number");
    > break;
    > default:
    > o(i + " is neither an odd nor even number");
    > // instead of System.out.println(i + " is neither an odd nor even
    > number");
    > }
    >
    > Doable? How? TIA.


    import static java.lang.System.*;
    public class OddsAndEvens {
    public static final int MAX_COUNT = 5;
    public static boolean isEven(int number) {
    return (number & 1) == 0;
    }
    public static void main(String[] args) {
    for (int i = 0; i < MAX_COUNT; ++i) {
    out.println(i + " is an " + (isEven(i) ? "even" : "odd") + "
    number");
    }
    }
    }
    Probably the shortest way to write this and still have it readable.

    the "import static.java.lang.System.*" line tells the compiler to
    borrow all of the static declarations in the java.lang.System class
    (including the "out" object).

    It is always a good idea to find meaningful names for your classes, and
    for any constant (other than "obvious" values, such as 1, or 0.)

    It's also not a bad idea to break out short methods (such as the
    isEven) that describe the intent of even the simplest piece of "logic".

    Now, having said all that, System.out.println is common enough that if
    you need to use it a lot, there isn't much wrong with copy-paste.
    Also, if you are using a good IDE (and you should try to), you will
    have a lot of auto-complete tools. For example, in IntelliJ IDEA, I
    would type "sout" press *ctrl-J* and press *enter*, and I would get
    System.out.println(""), with my cursor between the quotes.

    There are a lot of other typing helpers within all sorts of IDE's.

    In short, don't worry about typing too much. Usually the problem is
    with people typing too little, and making the code unreadble, and
    therefore impossible to maintain.
    Daniel Pitts, Dec 18, 2006
    #6
  7. NickName schreef:
    > Flo 'Irian' Schaetz wrote:
    >> And thus, NickName spoke...
    >>
    >>> I may be jumping guns here. I mean I'm totally new to java and yet, I
    >>> feel the need to do something like this for typing much less,

    >> If you want to make your code unreadable, there are more easy ways to go...
    >>
    >>> o = System.out.println; // problem, what data type for var of o here?
    >>> undefined.
    >>> or
    >>> alias o = System.out.println; // alias is supposed to be a special
    >>> command or the sort?

    >> Doesn't exist - afaik. You could, of course, simply write...
    >>
    >> public void o(String message) {
    >> System.out.println(message);
    >> }

    >
    > Thank you. That's exactly what I'm looking for. And with a little
    > twist, I changed it to
    >
    > // alias, creating short hand for some commonly used command, DL
    > public static void o(String msg) {
    > System.out.println(msg);
    > }
    >
    > // added static because the caller uses static


    An even easier way, still keeping things readable, is to use a proper
    IDE, such as Eclipse, then just type syso + Ctrl + Space and you get the
    whole System.out.println(|);, with the cursor at the |.

    > Now, since we are at it and talking about readibility (pls note, I add
    > comments for short hands), a new one regarding a file's date time
    > stamp, the method of lastModified() seem to be the aggregation of
    > million seconds something as in
    >
    > File myfile = new File(thisFile.txt);
    > long fileDate = myfile.lastModified();
    >
    > What method to display date/time like mm/dd/yyyy or mm--dd--yyyy?
    > Sorry I did not go the trouble of digging it via language reference doc


    Have a look at Formatter:
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html

    There are also convenience methods in the outputstreams:
    System.out.format(...)

    H.
    --
    Hendrik Maryns
    http://tcl.sfs.uni-tuebingen.de/~hendrik/
    ==================
    http://aouw.org
    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Hendrik Maryns, Dec 19, 2006
    #7
  8. NickName

    Ian Wilson Guest

    NickName wrote:

    <wants to write o(text) instead of System.out.println(text)>

    > for (int i=0; i < 5; i++) {
    > switch(i)
    > case 0:
    > case 2:
    > case 4:
    > o(i + " is an even number");
    > break;
    > case 1:
    > case 3:
    > case 5:
    > o(i + " is an odd number");
    > break;
    > default:
    > o(i + " is neither an odd nor even number");
    > }
    >


    1) Separate the message from the IO function.

    String m;
    for (int i=0; i < 5; i++) {
    switch(i) {
    case 0:
    case 2:
    case 4: m = " is an even number"; break;
    case 1:
    case 3:
    case 5: m = " is an odd number"; break;
    default: m = " is neither an odd nor even number";
    }
    System.out.println(i+m)
    }


    2) Use a less clumsy test and fix the out-by-one error.

    System.out.println("whatever you want to say about 0");
    for (int i=1; i < 6; i++) {
    if (i%2 == 0) {
    System.out.println(i+" is even");
    } else {
    System.out.println(i+" is odd");
    }
    }
    Ian Wilson, Dec 19, 2006
    #8
  9. NickName

    NickName Guest

    John Ersatznom wrote:
    > Christopher Benson-Manica wrote:
    > > NickName <> wrote:
    > >
    > >
    > >>I may be jumping guns here. I mean I'm totally new to java and yet, I
    > >>feel the need to do something like this for typing much less,

    > >
    > >
    > >>o = System.out.println; // problem, what data type for var of o here?

    > >
    > >
    > > In other languages, o might be a function pointer, but in Java you're
    > > out of luck. The best you could do, saving yourself a little bit of
    > > typing, would be something like
    > >
    > > final PrintStream o = System.out;
    > > o.println( "Hello, world!" );
    > >
    > > although it really isn't worth the effort. The alternative, using
    > > reflection to get the println Method from System.out, will save you
    > > neither typing nor brain CPU cycles.

    >
    > Something similar does sometimes come up where you want to "wrap"
    > something like System.out.println. For example, you want to log certain
    > events, but the exact nature of the logging should be irrelevant to
    > whatever does the logging and should be changeable in a single place.
    > Then you do this:
    >
    > public interface Logger {
    > public void log (String message);
    > }
    >
    > //Somewhere else
    > public class StdoutLogger implements Logger {
    > public void log (String message) {
    > System.out.println(message);
    > }
    > }
    >
    > //Somewhere else again
    > public static void main (String[] args) {
    > Logger logger = new StdoutLogger();
    > ...
    > someobject.doSomething(logger, other_args);
    > ...
    > }
    >
    > //Somewhere else again
    > public void doSomething (Logger logger, other_args) {
    > ...
    > logger.log("Foo");
    > ...
    > catch (IOException e) {
    > logger.log("Oops! I/O error");
    > logger.log(e.toString());
    > <some sort of recovery>
    > }
    > ...
    > }
    >
    > Later on you might want to use some other Logger. You can make an
    > AggregatingLogger: (assumes Tiger)
    >
    > public class AggregatingLogger implements Logger {
    > private List<Logger> members;
    > public AggregatingLogger () {
    > members = new LinkedList<Logger>();
    > }
    > public void add (Logger logger) {
    > members.add(logger);
    > }
    > public void log (String message) {
    > for (Logger logger : members) {
    > logger.log(message);
    > }
    > }
    > }
    >
    > Just be careful not to add an aggregating logger to itself, OK? :)


    Interesting, thanks.
    NickName, Dec 19, 2006
    #9
  10. NickName

    NickName Guest

    Daniel Pitts wrote:
    > NickName wrote:
    > OP [...]


    > import static java.lang.System.*;
    > public class OddsAndEvens {
    > public static final int MAX_COUNT = 5;
    > public static boolean isEven(int number) {
    > return (number & 1) == 0;
    > }
    > public static void main(String[] args) {
    > for (int i = 0; i < MAX_COUNT; ++i) {
    > out.println(i + " is an " + (isEven(i) ? "even" : "odd") + "
    > number");
    > }
    > }
    > }
    > Probably the shortest way to write this and still have it readable.


    Very nice and thanks for introducing the System package here. More
    questions,
    For the LINE of
    public static final int MAX_COUNT = 5;
    why not simply int MAX_COUNT = 5;
    ? // since it's it's already at the top level of the OddsAndEvens
    class.

    Please elaborate on the "(number & 1) == 0", though the & symbol is
    supposed to mean something like Evaluation AND (binary). TIA.

    >
    > the "import static.java.lang.System.*" line tells the compiler to
    > borrow all of the static declarations in the java.lang.System class
    > (including the "out" object).
    >
    > It is always a good idea to find meaningful names for your classes, and
    > for any constant (other than "obvious" values, such as 1, or 0.)
    >
    > It's also not a bad idea to break out short methods (such as the
    > isEven) that describe the intent of even the simplest piece of "logic".
    >
    > Now, having said all that, System.out.println is common enough that if
    > you need to use it a lot, there isn't much wrong with copy-paste.
    > Also, if you are using a good IDE (and you should try to), you will
    > have a lot of auto-complete tools. For example, in IntelliJ IDEA, I
    > would type "sout" press *ctrl-J* and press *enter*, and I would get
    > System.out.println(""), with my cursor between the quotes.
    >
    > There are a lot of other typing helpers within all sorts of IDE's.
    >
    > In short, don't worry about typing too much. Usually the problem is
    > with people typing too little, and making the code unreadble, and
    > therefore impossible to maintain.
    NickName, Dec 19, 2006
    #10
  11. NickName

    NickName Guest

    Hendrik Maryns wrote:

    >> [ ... ]
    > >
    > > What method to display date/time like mm/dd/yyyy or mm--dd--yyyy?
    > > Sorry I did not go the trouble of digging it via language reference doc

    >
    > Have a look at Formatter:
    > http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html
    >
    > There are also convenience methods in the outputstreams:
    > System.out.format(...)
    >
    > H.
    > --
    > Hendrik Maryns
    > http://tcl.sfs.uni-tuebingen.de/~hendrik/
    > ==================
    > http://aouw.org
    > Ask smart questions, get good answers:
    > http://www.catb.org/~esr/faqs/smart-questions.html


    Thanks, got it.
    NickName, Dec 19, 2006
    #11
  12. NickName

    NickName Guest

    Ian Wilson wrote:
    > NickName wrote:


    [ ...]

    > 1) Separate the message from the IO function.
    >
    > String m;
    > for (int i=0; i < 5; i++) {
    > switch(i) {
    > case 0:
    > case 2:
    > case 4: m = " is an even number"; break;
    > case 1:
    > case 3:
    > case 5: m = " is an odd number"; break;
    > default: m = " is neither an odd nor even number";
    > }
    > System.out.println(i+m)
    > }


    Beautiful and glad to learn about the "String m" usage, thanks.

    >
    > 2) Use a less clumsy test and fix the out-by-one error.
    >
    > System.out.println("whatever you want to say about 0");
    > for (int i=1; i < 6; i++) {
    > if (i%2 == 0) {
    > System.out.println(i+" is even");
    > } else {
    > System.out.println(i+" is odd");
    > }
    > }


    Ok, so, the i%2 == 0 is a formula to see if i is divisable by 2?
    Similar ones? TIA.
    NickName, Dec 19, 2006
    #12
  13. NickName

    Daniel Pitts Guest

    NickName wrote:
    > Daniel Pitts wrote:
    > > NickName wrote:
    > > OP [...]

    >
    > > import static java.lang.System.*;
    > > public class OddsAndEvens {
    > > public static final int MAX_COUNT = 5;
    > > public static boolean isEven(int number) {
    > > return (number & 1) == 0;
    > > }
    > > public static void main(String[] args) {
    > > for (int i = 0; i < MAX_COUNT; ++i) {
    > > out.println(i + " is an " + (isEven(i) ? "even" : "odd") + "
    > > number");
    > > }
    > > }
    > > }
    > > Probably the shortest way to write this and still have it readable.

    >
    > Very nice and thanks for introducing the System package here. More
    > questions,
    > For the LINE of
    > public static final int MAX_COUNT = 5;
    > why not simply int MAX_COUNT = 5;
    > ? // since it's it's already at the top level of the OddsAndEvens
    > class.


    int MAX_COUNT = 5; woudl create a new integer for every object create
    in OddsAndEvens. In this particular case, that doesn't matter much,
    but if you have a constant value that is the same accross 10000
    objects, it can start to add up.

    the "static" keyword tells the compiler that the memory and value is
    associated with the class, not individual instances of the class. The
    "final" keyword tells the compiler to not let anyone accidently change
    the value of this constant. It also allows the compiler to optimize.

    >
    > Please elaborate on the "(number & 1) == 0", though the & symbol is
    > supposed to mean something like Evaluation AND (binary). TIA.
    >


    In binary, if a number is a multiple of two, then its lowest
    signifigant bit is 0, otherwise the bit is one.
    We can use that knowledge to help us determine the "evenness" of a
    number. Since an even number is a number which contains two as a
    factor, we can test the lowest bit to tell us where a number is odd or
    even.
    1 is the bitmask for the lowest bit. n & 1 will return the value of
    the lowest bit.
    for example:
    n | BIN |n&1|
    0 | 0000 | 0 | even
    1 | 0001 | 1 | odd
    2 | 0010 | 0 | even
    3 | 0011 | 1 | odd
    4 | 0100 | 0 | even

    Hope this helps.
    - Daniel.
    Daniel Pitts, Dec 20, 2006
    #13
  14. NickName

    Ian Wilson Guest

    NickName wrote:
    > Ian Wilson wrote:
    >
    >> NickName wrote:
    >>
    >> 2) Use a less clumsy test and fix the out-by-one error.
    >>
    >> System.out.println("whatever you want to say about 0");
    >> for (int i=1; i < 6; i++) {


    Most people[1] regard 0 as even. If you are one of them, you could
    replace the above two lines with:
    for (int i=0; i < 6; i++) {

    >> if (i%2 == 0) {
    >> System.out.println(i+" is even");
    >> } else {
    >> System.out.println(i+" is odd");
    >> }
    >> }

    >
    >
    > Ok, so, the i%2 == 0 is a formula to see if i is divisable by 2?


    Yes.

    The value of `a%b` is the remainder after integer division of a by b. I
    recall Patricia Shanahan saying that the Java remainder operator (`%`)
    is almost the same as the usual modulo operator but there is some subtle
    distinction which I forget. (P.S. my recollection may be inaccurate, it
    often is :)

    http://en.wikipedia.org/wiki/Modulo_operation

    > Similar ones?


    I'm too new to Java to have a list of "interesting" operators handy :)
    I find O'Reilly's "Learning Java" helpful.

    Just reading this newsgroup regularly is an excellent way of discovering
    better ways to do things in Java.


    [1] Roulette table operators don't.
    Ian Wilson, Dec 20, 2006
    #14
  15. Ian Wilson wrote:
    ....
    > The value of `a%b` is the remainder after integer division of a by b. I
    > recall Patricia Shanahan saying that the Java remainder operator (`%`)
    > is almost the same as the usual modulo operator but there is some subtle
    > distinction which I forget. (P.S. my recollection may be inaccurate, it
    > often is :)

    ....

    The difference is in the extension to negative numbers.

    One of the examples in the JLS is

    (-5)%3 produces -2

    This is correct for remainder, because it maintains consistency with
    division. I would expect (N mod 3) to be represented by an integer in
    the range 0 through 2 for any integer N.

    Patricia
    Patricia Shanahan, Dec 20, 2006
    #15
  16. NickName

    NickName Guest

    Daniel Pitts wrote:
    > NickName wrote:
    > > Daniel Pitts wrote:
    > > > NickName wrote:
    > > > OP [...]
    > > > }

    > >
    > > Very nice and thanks for introducing the System package here. More
    > > questions,
    > > For the LINE of
    > > public static final int MAX_COUNT = 5;
    > > why not simply int MAX_COUNT = 5;
    > > ? // since it's it's already at the top level of the OddsAndEvens
    > > class.

    >
    > int MAX_COUNT = 5; woudl create a new integer for every object create
    > in OddsAndEvens. In this particular case, that doesn't matter much,
    > but if you have a constant value that is the same accross 10000
    > objects, it can start to add up.
    >
    > the "static" keyword tells the compiler that the memory and value is
    > associated with the class, not individual instances of the class. The
    > "final" keyword tells the compiler to not let anyone accidently change
    > the value of this constant. It also allows the compiler to optimize.
    >
    > >
    > > Please elaborate on the "(number & 1) == 0", though the & symbol is
    > > supposed to mean something like Evaluation AND (binary). TIA.
    > >

    >
    > In binary, if a number is a multiple of two, then its lowest
    > signifigant bit is 0, otherwise the bit is one.
    > We can use that knowledge to help us determine the "evenness" of a
    > number. Since an even number is a number which contains two as a
    > factor, we can test the lowest bit to tell us where a number is odd or
    > even.
    > 1 is the bitmask for the lowest bit. n & 1 will return the value of
    > the lowest bit.
    > for example:
    > n | BIN |n&1|
    > 0 | 0000 | 0 | even
    > 1 | 0001 | 1 | odd
    > 2 | 0010 | 0 | even
    > 3 | 0011 | 1 | odd
    > 4 | 0100 | 0 | even
    >
    > Hope this helps.
    > - Daniel.


    Your explanation is perfect. Thank you.
    NickName, Dec 20, 2006
    #16
  17. NickName

    NickName Guest

    Ian Wilson wrote:
    > NickName wrote:
    > > Ian Wilson wrote:
    > >
    > >> NickName wrote:
    > >>
    > >> 2) Use a less clumsy test and fix the out-by-one error.
    > >>
    > >> System.out.println("whatever you want to say about 0");
    > >> for (int i=1; i < 6; i++) {

    >
    > Most people[1] regard 0 as even. If you are one of them, you could
    > replace the above two lines with:
    > for (int i=0; i < 6; i++) {
    >
    > >> if (i%2 == 0) {
    > >> System.out.println(i+" is even");
    > >> } else {
    > >> System.out.println(i+" is odd");
    > >> }
    > >> }

    > >
    > >
    > > Ok, so, the i%2 == 0 is a formula to see if i is divisable by 2?

    >
    > Yes.
    >
    > The value of `a%b` is the remainder after integer division of a by b. I
    > recall Patricia Shanahan saying that the Java remainder operator (`%`)
    > is almost the same as the usual modulo operator but there is some subtle
    > distinction which I forget. (P.S. my recollection may be inaccurate, it
    > often is :)
    >
    > http://en.wikipedia.org/wiki/Modulo_operation
    >
    > > Similar ones?

    >
    > I'm too new to Java to have a list of "interesting" operators handy :)
    > I find O'Reilly's "Learning Java" helpful.
    >
    > Just reading this newsgroup regularly is an excellent way of discovering
    > better ways to do things in Java.
    >
    >
    > [1] Roulette table operators don't.


    Great. Thank you.
    NickName, Dec 20, 2006
    #17
  18. NickName

    NickName Guest

    Patricia Shanahan wrote:
    > Ian Wilson wrote:
    > ...
    > > The value of `a%b` is the remainder after integer division of a by b. I
    > > recall Patricia Shanahan saying that the Java remainder operator (`%`)
    > > is almost the same as the usual modulo operator but there is some subtle
    > > distinction which I forget. (P.S. my recollection may be inaccurate, it
    > > often is :)

    > ...
    >
    > The difference is in the extension to negative numbers.
    >
    > One of the examples in the JLS is
    >
    > (-5)%3 produces -2
    >
    > This is correct for remainder, because it maintains consistency with
    > division. I would expect (N mod 3) to be represented by an integer in
    > the range 0 through 2 for any integer N.
    >
    > Patricia


    Thank you.
    NickName, Dec 20, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Khashishi
    Replies:
    3
    Views:
    5,602
    Weng Tianxiang
    Sep 22, 2004
  2. =?ISO-8859-15?Q?Stefan_Schw=E4rzler?=

    what's incorrect ALIAS

    =?ISO-8859-15?Q?Stefan_Schw=E4rzler?=, Aug 3, 2005, in forum: VHDL
    Replies:
    1
    Views:
    451
    Jonathan Bromley
    Aug 4, 2005
  3. TigerJade
    Replies:
    4
    Views:
    487
    Jonathan Bromley
    Aug 5, 2005
  4. Maziar Aflatoun

    asp.net domain alias.. redirectors

    Maziar Aflatoun, Nov 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    422
    Alvin Bruney
    Nov 2, 2003
  5. grocery_stocker
    Replies:
    9
    Views:
    732
    grocery_stocker
    May 24, 2008
Loading...

Share This Page