"Small" Program Challenge.

D

Daniel Pitts

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.
 
D

Daniel Pitts

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");}}"
 
S

Stefan Ram

Daniel Pitts said:
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').
 
S

Stefan Ram

Daniel Pitts said:
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.
 
D

Daniel Pitts

Daniel Pitts said:
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.
 
D

Daniel Pitts

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.
 
D

Daniel Pitts

Ah, yes. That was just habit on my part.


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.
 
G

glen herrmannsfeldt

(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
 
M

markspace

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.
 
J

javax.swing.JSnarker

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*! ;)
 
R

Roedy Green

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
..
 
P

Paul Cager

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.
 
B

Bent C Dalager

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.
 
H

Hiram Hunt

Hiram Hunt said:
Roedy Green 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 ([email protected])

Sorry, I think I missed your point that this was just the obvious
solution. Other posts are already public-less on class.

-- Hiram Hunt ([email protected])
 
S

Stefan Ram

Bent C Dalager said:
$ java A "Hello World

A mere »System.out.println()« will suffice with

java A -Dline.separator="Hello World
"

, however, some shells might not treat the embedded line
separator as intended.
 
S

Stefan Ram

Daniel Pitts said:
I've got mine down to 61 characters. See if you can match that.

Here is a new variant of the above challenge:

Write a java program (source code) with less than 4000
characters and a java command line with less than 1000
characters that writes »Hello World« followed by a newline
character and nothing else, but does so in a somewhat
surprising or unusual way.

My entry:

public class Main
{ public static void main( final java.lang.String[] args )
{ System.out.println(); System.out.print( '\n' ); }}

java -Dline.separator="Hello World" Main
 
D

Daniel Pitts

Daniel Pitts said:
I've got mine down to 61 characters. See if you can match that.

Here is a new variant of the above challenge:

Write a java program (source code) with less than 4000
characters and a java command line with less than 1000
characters that writes »Hello World« followed by a newline
character and nothing else, but does so in a somewhat
surprising or unusual way.

My entry:

public class Main
{ public static void main( final java.lang.String[] args )
{ System.out.println(); System.out.print( '\n' ); }}

java -Dline.separator="Hello World" Main

A slightly obfuscated program which illustrates a few surprising things.

public class Hello {
static Object left = "Top", right = "Bottom";
static Object top = "Left";
static Object bottom = "Right";

public static void main(String[] args) throws Exception {
for (int i = 0; i < 2; ++i) {
s.out.print(new Hello());
y(left, right, top, bottom);
}
}

public String toString() {
try {
top = "value";
bottom = "count";
return "enumeration" + top + bottom;
} finally {
return getClass().getName();
}
}

static <T extends java.lang.reflect.AccessibleObject> T t(T t) {
t.setAccessible(true); return t;}

static <T> void y(T l, T r, T... os) throws Exception {
for (Object o : os) {
x(l, o).set(l, x(l, o).get(r));
}
}

private static java.lang.reflect.Field x(Object l, Object o) throws
NoSuchFieldException {
return t(l.getClass().getDeclaredField(o.toString()));
}

{
left = toString();
right = " W" + b + 'r' + a + "d\n";
}

static Object a = "l";
static Object b = "o";
static System s;
}
 
G

Gene Wirchenko

Daniel Pitts said:
I've got mine down to 61 characters. See if you can match that.

Here is a new variant of the above challenge:

Write a java program (source code) with less than 4000
characters and a java command line with less than 1000
characters that writes »Hello World« followed by a newline
character and nothing else, but does so in a somewhat
surprising or unusual way.

My entry:

public class Main
{ public static void main( final java.lang.String[] args )
{ System.out.println(); System.out.print( '\n' ); }}

java -Dline.separator="Hello World" Main

The IOCCC (International Obfuscated C Code Contest) has been
running yearly for twenty years. Is someone trying to start an IOJJJ
(International Obfuscated Java Jungle of Junk?)?

Sincerely,

Gene Wirchenko
 
D

Daniel Pitts

Daniel Pitts said:
I've got mine down to 61 characters. See if you can match that.

Here is a new variant of the above challenge:

Write a java program (source code) with less than 4000
characters and a java command line with less than 1000
characters that writes »Hello World« followed by a newline
character and nothing else, but does so in a somewhat
surprising or unusual way.

My entry:

public class Main
{ public static void main( final java.lang.String[] args )
{ System.out.println(); System.out.print( '\n' ); }}

java -Dline.separator="Hello World" Main
Another lesson: Exceptions, and the deprecated (and highly dangerous)
Thread.stop(Throwable) method.

import java.awt.EventQueue;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;

public class Hello extends Exception {
public static void main(String[] args) throws Exception {
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace(System.out);
}
});
final Thread thread = Thread.currentThread();
EventQueue.invokeAndWait(new Runnable() {
public void run() {
thread.stop(new Hello());
}
});
}

public void printStackTrace(PrintStream s) {
s.print("Hello World");
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Programming Challenge 2
Virtuous Hacking challenge 13
Tutorial challenge program help 19
small program 49
JFrame stays unusably small in applet 5
Tasks 1
[Challenge] Cookie Monster! 6
Html download challenge 25

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top