Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

60%
+1 −0
Q&A Tips for golfing in Java

A clever way to replace Math functions You can shorten if-else statements through the a?b:c syntax, where a is the statement of if, b is the result of if, and c is the result of else. static vo...

posted 3y ago by General Sebast1an‭  ·  edited 3y ago by General Sebast1an‭

Answer
#2: Post edited by user avatar General Sebast1an‭ · 2021-11-09T04:03:18Z (about 3 years ago)
  • # A clever way to replace `Math` functions
  • You can shorten `if-else` statements through the `a?b:c` syntax, where `a` is the statement of `if`, `b` is the result of `if`, and `c` is the result of `else`.
  • <!-- language-all: lang-java -->
  • <pre><code>static void main(String[]a){
  • &#9;int i = 95;
  • &#9;System.out.print(i&gt;0?"positive":"negative");
  • }
  • </code></pre>
  • [Try it online!][TIO-kurz24jb]
  • [TIO-kurz24jb]: https://tio.run/##y0osS9TNSsn@n5lXklqUlpicquBb/b@4JLEkM1mhLD8zRSE3MTNPI7ikKDMvPTo2UbOaixOoVCFTwVbB0tSaizO4srgkNVcvv7RErwCopkQj087AXqkgvzizJLMsVclKKS81PRHM1LTmqv1f@x8A "Java (JDK) – Try It Online"
  • With this, you can take out the following functions from `java.util.Math`:
  • - `Math.max()` and `Math.min()`
  • <!-- language-all: lang-java -->
  • <pre><code>import java.util.*;
  • interface M{
  • &#9;static void main(String[]x){
  • &#9;&#9;int a = 20, b = 30;
  • &#9;&#9;System.out.println(Math.max(a,b));
  • &#9;&#9;System.out.println(a&gt;b?a:b);
  • &#9;&#9;System.out.println(Math.min(a,b));
  • &#9;&#9;System.out.println(a&lt;b?a:b);
  • &#9;}
  • }
  • </code></pre>
  • [Try it online!][TIO-kurz8p62]
  • [TIO-kurz8p62]: https://tio.run/##fY69DoIwFIVn@hR3pAYbopv48wRMjMbhFlBvpYXAhWAMz147ucl0knO@fDkGJ9ya6uU92a7tGUwo1MjUqE0mBDmu@zuWNeQfEQ2MTCVMLVVgkVxccE/ucb3NMqxRgAHhBLs0AR1yn2ahLd4D11a1I6su0Ny4OEd@KotzjImW8g@EZ33Bg5arjvBh1XH8ORaxeP8F "Java (JDK) – Try It Online"
  • # A clever way to replace `Math` functions
  • You can shorten `if-else` statements through the `a?b:c` syntax, where `a` is the statement of `if`, `b` is the result of `if`, and `c` is the result of `else`.
  • <!-- language-all: lang-java -->
  • <pre><code>static void main(String[]a){
  • &#9;int i = 95;
  • &#9;System.out.print(i&gt;0?"positive":"negative/zero");
  • }
  • </code></pre>
  • [Try it online!][TIO-kurz24jb]
  • [TIO-kurz24jb]: https://tio.run/##FcqxDoIwEADQmX7FpRMMogsDEuELmBiNwwUOchha0p5NlPDtFbY3vBkDXubhHdkIuRF7gnaLXlC4h2B5gAXZpJ04NtPzhdmmkqMCwwPKolJJ9/VCS24/kq/HkZTrW6NX61k4kL5rQxOevP7IWZ1Vao97/AM "Java (JDK) – Try It Online"
  • With this, you can take out the following functions from `java.util.Math`:
  • - `Math.max()` and `Math.min()`
  • <!-- language-all: lang-java -->
  • <pre><code>import java.util.*;
  • interface M{
  • &#9;static void main(String[]x){
  • &#9;&#9;int a = 20, b = 30;
  • &#9;&#9;System.out.println(Math.max(a,b));
  • &#9;&#9;System.out.println(a&gt;b?a:b);
  • &#9;&#9;System.out.println(Math.min(a,b));
  • &#9;&#9;System.out.println(a&lt;b?a:b);
  • &#9;}
  • }
  • </code></pre>
  • [Try it online!][TIO-kurz8p62]
  • [TIO-kurz8p62]: https://tio.run/##fY69DoIwFIVn@hR3pAYbopv48wRMjMbhFlBvpYXAhWAMz147ucl0knO@fDkGJ9ya6uU92a7tGUwo1MjUqE0mBDmu@zuWNeQfEQ2MTCVMLVVgkVxccE/ucb3NMqxRgAHhBLs0AR1yn2ahLd4D11a1I6su0Ny4OEd@KotzjImW8g@EZ33Bg5arjvBh1XH8ORaxeP8F "Java (JDK) – Try It Online"
#1: Initial revision by user avatar General Sebast1an‭ · 2021-10-15T06:16:03Z (about 3 years ago)
# A clever way to replace `Math` functions

You can shorten `if-else` statements through the `a?b:c` syntax, where `a` is the statement of `if`, `b` is the result of `if`, and `c` is the result of `else`.

<!-- language-all: lang-java -->

<pre><code>static void main(String[]a){
&#9;int i = 95;
&#9;System.out.print(i&gt;0?"positive":"negative");
}
</code></pre>

[Try it online!][TIO-kurz24jb]

[TIO-kurz24jb]: https://tio.run/##y0osS9TNSsn@n5lXklqUlpicquBb/b@4JLEkM1mhLD8zRSE3MTNPI7ikKDMvPTo2UbOaixOoVCFTwVbB0tSaizO4srgkNVcvv7RErwCopkQj087AXqkgvzizJLMsVclKKS81PRHM1LTmqv1f@x8A "Java (JDK) – Try It Online"

With this, you can take out the following functions from `java.util.Math`:

- `Math.max()` and `Math.min()`

<!-- language-all: lang-java -->

<pre><code>import java.util.*;

interface M{
&#9;static void main(String[]x){
&#9;&#9;int a = 20, b = 30;
&#9;&#9;System.out.println(Math.max(a,b));
&#9;&#9;System.out.println(a&gt;b?a:b);
&#9;&#9;System.out.println(Math.min(a,b));
&#9;&#9;System.out.println(a&lt;b?a:b);
&#9;}
}
</code></pre>

[Try it online!][TIO-kurz8p62]

[TIO-kurz8p62]: https://tio.run/##fY69DoIwFIVn@hR3pAYbopv48wRMjMbhFlBvpYXAhWAMz147ucl0knO@fDkGJ9ya6uU92a7tGUwo1MjUqE0mBDmu@zuWNeQfEQ2MTCVMLVVgkVxccE/ucb3NMqxRgAHhBLs0AR1yn2ahLd4D11a1I6su0Ny4OEd@KotzjImW8g@EZ33Bg5arjvBh1XH8ORaxeP8F "Java (JDK) – Try It Online"