Different ways to print "Hello world" in Java

  ibmer        2016-05-21 21:49:48       8,111        0    

This post is not about best practice to print "Hello world" in Java, it is about exploring different capabilities offered by Java. Also there are articles about printing "Hello world" in different programming languages, but this post is not about that.

Standard literal

The most commonly used way is to use System.out to print"Hello world".

System.out.println("Hello world");

Enumeration

An enumeration can define a set of values belonging to one category. For example, an enumeration for all months in a year. Another great use of enumeration is in writing testcases to accept different testing parameetrs. Each testing parameter is a category and it will contain a set of testing values. The name of the elements in enumeration can be printed out easily.

First declare an enumeration.

enum Message {
	HELLO_WORLD("Hello world");
	
	private String name;
	private Message(String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}
}

Then print out the message with below call

System.out.println(Message.HELLO_WORLD.getName());

External command

Java provides capability of executing application which is not written in Java. An external application can be ran with Java Runtime.

For example, you can invoke system echo to print the "Hello world" message in Windows as below.

String[] command = {"cmd.exe","/C","echo Hello world"};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);

InputStream in = proc.getInputStream();
InputStreamReader osr = new InputStreamReader (in);
BufferedReader obr = new BufferedReader (osr);
String line;
while ( (line = obr.readLine()) != null )
	System.out.println(line);

proc.waitFor();

This is usually used when there is already an existing application written in other languages which can fulfill the requirement. It reduces the cost of developing a brand new one in Java.

Java Native Interface

Sometimes when platform specific features are desired or performance is a concern, native code can be written and invoked by Java.  To print "Hello world" using JNI, the tutorial A simple tutorial on writing Java native program can help you proceed.

ResourceBundle

In multilingual applications, different people from different countres want to see messages printed in their mother tongue. This requires different resource files created and then use ResourceBundle to load different resource file accordingly.

First create a resource file containing different key/value pairs and put it in the src folder.

#Message to be printed
hello_world = Hello world
welcome = Welcome

Then load the resource with ResourceBundle in Java code.

ResourceBundle msgs = ResourceBundle.getBundle("message", Locale.ENGLISH);
System.out.println(msgs.getString("hello_world"));

All above ways outline different features offered by Java and the use case of these features. You can use them depend on your requirement. There are always different ways to complete a task in programming world.

JAVA  FEATURE 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

try and catch in simple