|
Authored by: Anonymous on Friday, April 27 2012 @ 02:27 PM EDT |
I don't know about Java, but in C++ you can "overload"
an operator to make it do more than it's default operation.
It's been a while, but try this out.
Normally, '>' assumes numbers. It could be overloaded to
accept objects, e.g "google > oracle" could be made to evaluate as
true.
This is a very useful and powerful technique in object
oriented programming.
[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Friday, April 27 2012 @ 02:31 PM EDT |
The max method can take a long list of inputs and return the one that is the
greatest, it's not designed to just compare 2 numbers. Also, java is object
oriented, so you would still have to have the "greater than" method
defined separately, and then call it from your method.[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Friday, April 27 2012 @ 02:31 PM EDT |
> (greater than) is not a function, it is an operator. Of course java have
the ">"-operator, but if x and y are large/complicated expressions,
you might not want to have to write them twice, as you need to if you wrote it
as "(x > y ? x : y)" or with an if-statement as in your example.[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Friday, April 27 2012 @ 02:35 PM EDT |
Yes, Java has a > operator. It even has a ternary operator, like C, so you
could
also write something like:
return (x > y) ? x :
y;
The primary reasons for using built-in methods instead of writing
your own are
readability, maintainability, performance (for more complicated
methods), and
correctness. In the Math.max() example, the
overhead of the
extra static method call is minimal, and any decent JIT
compiler, such as
Dalvik on Android or Sun's HotSpot VM, will inline the
implementation of small
methods such as Math.max() whenever it compiles
frequently-executed code
sequences into native code. [ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Friday, April 27 2012 @ 02:40 PM EDT |
Both sides have been using it as an example because it is a very simple
method.
The reason programmers would call a method like this from their
program, is for clarity. (i.e. not because the library writer wrote a "super
awesome Math.max function which is 2x as fast", of course that doesn't happen..
using this particular method might be slower, but its still worthwhile for other
reasons, like readability).
An expression like
((someValue >
0.0f)? someValue : 0.0f)
is perfectly fine, but idiomatic, harder
to read for people unfamiliar with the idiom. The expression
Math.max(someValue, 0.0f)
does the same thing, but the
reader is likely to notice the "Math.max" and easily understand what it
does.
It also has the advantage that you can search for "max(" with any text
editor and find occurrences of this kind of code. Idiomatic expressions tend to
be hard to search for.
Most API methods, probably 99% of all methods across
those 37 packages, do "more stuff" in the black box part, so you save more
effort by reusing them. The advantage of having a standard name for that
functionality and being able to search for it, etc. still applies. Also, some
things like "sorting" or "binary search" could be written by anybody, but
extremely good implementations have been discovered which are actually faster or
better than the ones that most people could write on their own. For example:
Anybody could write a quicksort routine after reading a wikipedia page about it
for 10 minutes. But Timsort does the same job, but is usually faster, sometimes
much faster! So you usually shouldn't be writing your own sorting routines,
most platforms already provide excellent ones. [ Reply to This | Parent | # ]
|
|
Authored by: kuroshima on Friday, April 27 2012 @ 02:41 PM EDT |
The thing here, is that by providing multiple overriden max
methods, you end with code that can compare two whatevers,
without actually knowing what the whatevers are. You can
then have a list of items of arbitrary, and sort it, without
having to think how to sort every single possible item.
If we're dealing with ints, or floats, it's easy. In fact,
you can simply use the > operator. If we're dealing with
Objects, then it's harder, but Java includes the Comparable
Interface, and that means that two objects of the same type,
if the type implements the Comparable interface, will have
the compareTo method, that will return an int that
represents if one element is greater, lesser or equal to
another.
Mind you, you end up with multiple signatures for max, one
for ints, one for floats, one for objects,... but then
inside each implementation you can have the best performing
algorithm for it, and your sortable list will still work,
with any possible comparable items
How do you compare two Strings? How do you compare two
employees? and yet if you've done things properly, your
sorting algorithm will sort them as long as they're sortable, and you will write
ONE sorting algorithm, not one
per type of sortable thing
Of course, the max method in the math package mostly deals
with numbers (I checked after I started writing this) making
most of the reply moot, but the idea that you encapsulate in
order to reuse is prevalent over the language and API.[ Reply to This | Parent | # ]
|
|
Authored by: cbc on Friday, April 27 2012 @ 02:48 PM EDT |
Returns the greater of two double values. That is, the result is the argument
closer to positive infinity. If the arguments have the same value, the result is
that same value. If either value is NaN, then the result is NaN. Unlike the the
numerical comparison operators, this method considers negative zero to be
strictly smaller than positive zero. If one argument is positive zero and the
other negative zero, the result is positive zero.
From javadoc for java.math.max(double double). ...max(float float) similar.[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Friday, April 27 2012 @ 02:53 PM EDT |
If you are using complex numbers such things make the
programmers job easier. No need to write your own code
for doing such things as the math library already has the
(hopefully) correct way to handle it.
[ Reply to This | Parent | # ]
|
- java.math.max - Authored by: Anonymous on Saturday, April 28 2012 @ 12:35 AM EDT
|
Authored by: Anonymous on Friday, April 27 2012 @ 03:00 PM EDT |
Since you're a C programmer, look at "math.h". It's more or less
equivalent to java.math.
The real answer is that max(a, b) is a lot easier to read than using the ternary
operator.
Reusing code also ensures that edge cases are handled consistently. For
example, your code returns different results for max(a,b) and max(b,a) if one of
the arguments is NaN (not a number).
I figured I'd respond since there are some wrong ideas in the responses. Java
doesn't allow you to override operators. The Java max function doesn't accept a
list.
[ Reply to This | Parent | # ]
|
|
Authored by: Anonymous on Friday, April 27 2012 @ 03:34 PM EDT |
Thanks guys. (and gals). I just compiled the if/else example and the ?:
example and looked at the assembly output, assuming they would be the same.
Turns out that, at least with this C compiler, they are not. The if/else
construct used registers, the ?: construction used the stack, and was (a few
bytes) bigger. So there might be more at stake here than just readability.
As mentioned, not really familiar with java (though did work with p-code
interpreter back in the beforetime) so I can see that the math.max function
might well be more efficient that using the > operator, depending on the
implementation.
The implementation is, after all, where a lot of the creativity of programming
is brought to bear. Not to beat a dead horse... :)
dpa
[ Reply to This | Parent | # ]
|
- java.math.max - Authored by: Anonymous on Friday, April 27 2012 @ 04:10 PM EDT
|
Authored by: Imaginos1892 on Friday, April 27 2012 @ 04:39 PM EDT |
Well... X and Y can be lots of things - integers of
different sizes, floats, extended characters, yada yada.
"X > Y" is real simple to write but evaluating it may get
pretty involved and could cover up to a dozen different
methods.
----------------------
Gentlemen! You can't fight in here -- this is the War Room![ Reply to This | Parent | # ]
|
|
Authored by: GriffMG on Friday, April 27 2012 @ 07:27 PM EDT |
the max() function is really simple to implement for integers and other 'simple'
things.
The beauty of and API, and (I hate saying this) OOP in general, is that max()
can be implemented for complex situations.
The first (more complex situation) is for a string, a series of characters (a
series of integers really) and the implementation of that is a smidge more
complex...
An API can descibe in general terms how to decide on the max() of two washing
machines, a car and a bike, the Bible and the (please excuse the spelling)
Quaran, a cage of monkeys and a cage of parrots!
Implementing those are a LOT harder
---
Keep B-) ing[ Reply to This | Parent | # ]
|
|
Authored by: celtic_hackr on Friday, April 27 2012 @ 09:30 PM EDT |
Everyone seems to forget, and it's easy to forget.
But NOTHING can be written in any computer language without using an API.
NOTHING.
The base API is the CPU instruction set. So, if you're a masochistic programmer
who writes in binary (I can do it), you are using the the binary instructions
which call the firmware API called the "instruction set", and you'd
better know what the arguments are.
If you are writing in assembly you are using the assembly language which is
composed of an API to the the binary language API which is passed to the CPU to
be implemented.
If you are writing in a higher level language then you are using it's API which
includes things like ">" or "GREATER THAN" or
"GT" which is an API which gets converted to several assembly binary
code statements.
EVERYTHING in EVERY COMPUTER PROGRAM, is an Application Programming Interface.
In Java, et al you are interfacing with a higher level language which interfaces
with a byte code compiler, which creates an interface with a VM which uses an
interface with the computer in binary code, which interfaces with the CPU.
Sure, in common parlance we consider libraries of often used code that implement
much bigger blocks of code than "GREATER THAN". Because up until
Oracle's insane argument we built on top of the work done before, so APIs have
gotten bigger and bigger with more an more functionality.
Now personally, I think every coder who has taken the stand for Oracle should be
banned from working in the computer and software industry. Such vile and
perjured wretches* have no place calling themselves computer scientists. Just as
I would demand any doctor removing a spleen instead of an appendix should be
removed from practice. But that's just my opinion.
*I'm not saying they openly perjured themselves, it is a famous expression of
speech. But, I'm not saying they didn't either.
[ Reply to This | Parent | # ]
|
|
|
|
|