Hello Script for Java

After the Hello Script for JavaScript, here is the Hello Script for Java. ‘Hello Script’ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.

Warning: I am NOT an expert in Java – I am just a beginner. There may will be errors(bad programming methods – not compiler errors) in the following script. If you notice any such issues, please point them out in the comments.


If you want to run the code, save it to a file named ‘Hello.java’ and compile in using the command ‘javac Hello.java’. After that you can run the code using the command ‘java Hello’.

import java.io.*;
import java.util.regex.*;

public class Hello {
	public static void main(String[] Args) {
		// Printing(IO)
		System.out.println("Hello World");
		// Variables, concatenation
		String name = "Binny";
		int year = 2008;
		System.out.println("Hello, " + name + " - welcome to " + year);
		if(year > 2008) {
			System.out.println("Welcome to the future - yes, we have flying cars!");
		} else if(year < 2008) {
			System.out.println("The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!");
		} else {
			System.out.println("Anything wrong with your time machine? You have not gone anywhere, kiddo.");
		// For loop
		int i=0;
		for(i=0; i<3; i++) {
			System.out.println(i + ") Hi there!");
		//Numerical Array, While
		String rules[] = {"Do no harm", "Obey", "Continue Living"};
		i = 0;
		while(i<rules.length) {
			System.out.println("Rule " + (i+1) + " : " + rules[i]);
		// Associated array, foreach
		// Hmm - does Java have Associated arrays? ArrayList?
		String csv_string = "hello,world,how,are,you";
		String csv_values[] = csv_string.split(",");
		// No native Join method
		// Function, argument, return, call
		System.out.println(Hello.hello("Binny")); //The function definition is at the end of this file.	

		//Class stuff...
		Movie ncfom = new Movie("New Country for Old Men"); //It's a sequel!

		// File IO
		// File reading, easy method...
		try {
			File read_file = new File ("/tmp/Hello.txt");
			FileReader in_stream = new FileReader(read_file); // Create a Character Input Stream
			BufferedReader in = new BufferedReader(in_stream);// Filter the Input Stream - buffers characters for efficiency
			try {
				System.out.println(in.readLine()); // read the first line
			} catch(IOException E) {
				System.out.println("No idea what went wrong. Sorry!");
		} catch(FileNotFoundException E) {
			System.out.println("File not found. Sorry!");
		try {
			// Writing to a file
			File out_file = new File("/tmp/HelloJava.txt");
			FileOutputStream out_stream = new FileOutputStream(out_file); // Create an Output Stream
			PrintWriter out = new PrintWriter(out_stream); // Filter bytes to ASCII
			out.println("Hello, from Java"); // Here we actually write to file
		} catch(java.io.FileNotFoundException E) {
			System.out.println("File not found. Sorry!");
		System.out.println("\nLS command results...");
		// Command Executing
		try {
			// Execute a command
			String command = "ls";
			Process child = Runtime.getRuntime().exec (command);
			// Read from an input stream
			InputStream in = child.getInputStream();
			int c;
			while ((c = in.read()) != -1) {
		} catch (IOException e) {
		//Regular Expression
		String str = new String("Hello World");
		//Find a pattern
		Pattern hell_check = Pattern.compile("^Hell");
		Matcher matches = hell_check.matcher(str);
		if(matches.find()) System.out.println("Yup - its evil");
		System.out.println(str.replaceAll("l([^l])", "$1")); //Remove an 'l' from both words. Should print 'Helo Word'

	//Function declaration.
	private static String hello(String name) {
		return "Hello, " + name;
	// One for the OOP fanboys - Class, members, object and stuff.
	private static class Movie {
		public String name = "";
		public int rating = 0;
		public Movie(String name) {
			this.name = name;
		public void rateMovie() {
			this.rating = (this.name.length() % 10) + 1; //IMDBs rating algorithm. True story!
		public void printMovieDetails() {
			System.out.println("Movie  : " + this.name);
			System.out.println("Rating : " + this.rating);

Next Hello Script – C

1 Comment

  1. I know this is about “Hello world” programs, but it will be more useful to many if you could include database connectivity, networking etc in the code. Coz, that is what most of us use all the time. It is good that you have included basic file handling this time.

Comments are closed.