|
|||
|
I saw a challenge Roedy posted on cljh, and I thought I might have a
slightly more interesting one. Write a Java program which outputs "Hello World" followed by a new line (and nothing else). Now, do it using as few characters in the .java source code as possible. I've got mine down to 61 characters. See if you can match that. |
|
|
||||
|
||||
|
|
|
|||
|
On 6/13/12 1:45 PM, Daniel Pitts wrote:
> I saw a challenge Roedy posted on cljh, and I thought I might have a > slightly more interesting one. > > Write a Java program which outputs "Hello World" followed by a new line > (and nothing else). > > Now, do it using as few characters in the .java source code as possible. > > I've got mine down to 61 characters. See if you can match that. Hint, the following is 82 characters, if you remove line wrapping. Where do I trim the 21 characters? "class M{public static void main(String[]args){System.out.println("Hello World");}}" |
|
|||
|
Daniel Pitts <newsgroup.nospam@virtualinfinity.net> writes:
>>Write a Java program which outputs "Hello World" followed by a new line (...) >"class M{public static void main(String[]args){System.out.println("Hello World");}}" »System.out.println("Hello World");« does not output "Hello World" followed by a new line, but "Hello World" followed by the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n'). |
|
|||
|
Daniel Pitts <newsgroup.nospam@virtualinfinity.net> writes:
>Where do I trim the 21 characters? You can trim the »args« to »a«. Possibly, some earlier JDKs allowed omission of the main method, but a recent JDK seems to require it. |
|
|||
|
On 6/13/12 2:06 PM, Stefan Ram wrote:
> Daniel Pitts<newsgroup.nospam@virtualinfinity.net> writes: >>> Write a Java program which outputs "Hello World" followed by a new line > (...) >> "class M{public static void main(String[]args){System.out.println("Hello World");}}" > > »System.out.println("Hello World");« does not output "Hello > World" followed by a new line, but "Hello World" followed by > the line separator string. The line separator string is > defined by the system property line.separator, and is not > necessarily a single newline character ('\n'). > I didn't say a "new line" character. However, print("Hello World\n") is the same length. My intent was line separator, however if you choose to interpret it the other way, there is no benefit or penalty. |
|
|||
|
On 6/13/12 2:29 PM, Stefan Ram wrote:
> Daniel Pitts<newsgroup.nospam@virtualinfinity.net> writes: >> Where do I trim the 21 characters? > > You can trim the »args« to »a«. Ah, yes. That was just habit on my part. > Possibly, some earlier JDKs allowed > omission of the main method, but a recent JDK seems to require it. > The JDK doesn't require anything of a class. java on the other hand goes through a specific sequence when asked to "run" a Java program. My smallest program is still 61 characters long, the example I posted, after replacing args with a, is 79. |
|
|||
|
On 6/13/12 4:19 PM, Daniel Pitts wrote:
> On 6/13/12 2:29 PM, Stefan Ram wrote: >> Daniel Pitts<newsgroup.nospam@virtualinfinity.net> writes: >>> Where do I trim the 21 characters? >> >> You can trim the »args« to »a«. > Ah, yes. That was just habit on my part. > >> Possibly, some earlier JDKs allowed >> omission of the main method, but a recent JDK seems to require it. >> > > The JDK doesn't require anything of a class. java on the other hand goes > through a specific sequence when asked to "run" a Java program. Ah, although now I see reports that Java 7 does some validation before some of that sequence. So, my 61 character source compiles fine, but won't run on Java 7. I wonder why they bothered. |
|
|||
|
Daniel Pitts <newsgroup.nospam@virtualinfinity.net> wrote:
(snip) > I didn't say a "new line" character. However, print("Hello World\n") is > the same length. My intent was line separator, however if you choose to > interpret it the other way, there is no benefit or penalty. Note that there is no requirement that the host system even use a newline character. There are systems that keep track of lines by length. Now, the C tradition of using '\n' as a line terminator, even on systems that don't store files that way, isn't completely gone in Java. Writing a "\n" will likely generate a new line even on systems that don't use a newline character. -- glen |
|
|||
|
On 6/13/2012 1:52 PM, Daniel Pitts wrote:
> On 6/13/12 1:45 PM, Daniel Pitts wrote: >> I saw a challenge Roedy posted on cljh, and I thought I might have a >> slightly more interesting one. >> >> Write a Java program which outputs "Hello World" followed by a new line >> (and nothing else). >> >> Now, do it using as few characters in the .java source code as possible. >> >> I've got mine down to 61 characters. See if you can match that. > > Hint, the following is 82 characters, if you remove line wrapping. Where > do I trim the 21 characters? > > "class M{public static void main(String[]args){System.out.println("Hello > World");}}" This is a good one, although the options for really trimming things down in surprising ways is absent in Java. Another good one is to write a Java program that prints its own source text. No fair using an external file, of course. |
|
|||
|
On 13/06/2012 4:45 PM, Daniel Pitts wrote:
> I saw a challenge Roedy posted on cljh, and I thought I might have a > slightly more interesting one. > > Write a Java program which outputs "Hello World" followed by a new line > (and nothing else). > > Now, do it using as few characters in the .java source code as possible. > > I've got mine down to 61 characters. See if you can match that. class X{static{System.out.println("Hello World");for(; ;}}is 59 characters. Hey, you didn't say it has to actually *terminate*! ![]() -- public final class JSnarker extends JComponent A JSnarker is an NNTP-aware component that asynchronously provides snarky output when the Ego.needsPuncturing() event is fired in cljp. |
|
|||
|
On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts
<newsgroup.nospam@virtualinfinity.net> wrote, quoted or indirectly quoted someone who said : > >I've got mine down to 61 characters. See if you can match that. here's the obvious solution at 88 chars: public class C{public static void main(String[] a){System.out.println("Hello World");}} -- Roedy Green Canadian Mind Products http://mindprod.com Controlling complexity is the essence of computer programming. ~ Brian W. Kernighan 1942-01-01 .. |
|
|||
|
On Jun 13, 9:45*pm, Daniel Pitts
<newsgroup.nos...@virtualinfinity.net> wrote: > I saw a challenge Roedy posted on cljh, and I thought I might have a > slightly more interesting one. > > Write a Java program which outputs "Hello World" followed by a new line > (and nothing else). > > Now, do it using as few characters in the .java source code as possible. > > I've got mine down to 61 characters. See if you can match that. You may also find some of the challenges on http://codegolf.stackexchange.com/ interesting. |
|
|||
|
On 2012-06-13, Daniel Pitts <newsgroup.nospam@virtualinfinity.net> wrote:
> I saw a challenge Roedy posted on cljh, and I thought I might have a > slightly more interesting one. > > Write a Java program which outputs "Hello World" followed by a new line > (and nothing else). > > Now, do it using as few characters in the .java source code as possible. How much are you permitted to offload to the launcher? Trivial example of offloading: class A{public static void main(String[] a){System.out.print(a[0]);}} (69 chars) with launch instructions: run like this (bash command line example shown, other launch environments will have other ways of expressing the newline) $ java A "Hello World > " $ And how much can you offload to a hypothetical "library" function that just happens to do exactly what you want? class B{public static void main(String[] a){L.f();}} (52 chars) Cheers, Bent. -- Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd powered by emacs |
|
|||
|
"Roedy Green" <see_website@mindprod.com.invalid> wrote in message news:jqnit7pv9phoig3t2il0s4jf4s19ot7p3i@4ax.com... > On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts > <newsgroup.nospam@virtualinfinity.net> wrote, quoted or indirectly > quoted someone who said : > >> >>I've got mine down to 61 characters. See if you can match that. > > here's the obvious solution at 88 chars: > > public class C{public static void main(String[] > a){System.out.println("Hello World");}} No need for public on class. -- Hiram Hunt (hiramhunt@verizon.net) |
|
|||
|
"Hiram Hunt" <hiramhunt@verizon.net> wrote in message news:4fd9d7d1$0$1727$c3e8da3$aae71a0a@news.astrawe b.com... > > "Roedy Green" <see_website@mindprod.com.invalid> wrote in message > news:jqnit7pv9phoig3t2il0s4jf4s19ot7p3i@4ax.com... >> On Wed, 13 Jun 2012 13:45:18 -0700, Daniel Pitts >> <newsgroup.nospam@virtualinfinity.net> wrote, quoted or indirectly >> quoted someone who said : >> >>> >>>I've got mine down to 61 characters. See if you can match that. >> >> here's the obvious solution at 88 chars: >> >> public class C{public static void main(String[] >> a){System.out.println("Hello World");}} > > No need for public on class. > > -- Hiram Hunt (hiramhunt@verizon.net) Sorry, I think I missed your point that this was just the obvious solution. Other posts are already public-less on class. -- Hiram Hunt (hiramhunt@verizon.net) |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|