<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bin-Blog &#187; java</title>
	<atom:link href="http://www.bin-co.com/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bin-co.com/blog</link>
	<description>Learn about the latest in Web Development - as soon as I do.</description>
	<lastBuildDate>Tue, 13 Oct 2009 18:55:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hello Script for Java</title>
		<link>http://www.bin-co.com/blog/2008/06/hello-script-for-java/</link>
		<comments>http://www.bin-co.com/blog/2008/06/hello-script-for-java/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 18:24:01 +0000</pubDate>
		<dc:creator>Binny V A</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[hello]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.bin-co.com/blog/?p=111</guid>
		<description><![CDATA[
After the Hello Script for JavaScript, here is the Hello Script for Java. &#8216;Hello Script&#8217; 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 &#8211; I am just [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.bin-co.com/blog/wp-content/uploads/2008/06/java-logo.png" alt="" title="Java Logo" width="125" height="217" class="size-full wp-image-112 intro" align="right" /></p>
<p class="intro">After the <a href="http://www.bin-co.com/blog/2008/05/hello-script-for-javascript/">Hello Script for JavaScript</a>, here is the Hello Script for Java. <strong class="highlight">&#8216;Hello Script&#8217; is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet</strong> when working with that language.</p>
<p>Warning: I am NOT an expert in Java &#8211; I am just a beginner. There <strikeout>may</strikeout> will be errors(bad programming methods &#8211; not compiler errors) in the following script. If you notice any such issues, please point them out in the comments.</p>
<h2>Code</h2>
<p>If you want to run the code, save it to a file named &#8216;Hello.java&#8217; and compile in using the command &#8216;javac Hello.java&#8217;. After that you can run the code using the command &#8216;java Hello&#8217;.</p>
<pre><code class="java">
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 &gt; 2008) {
			System.out.println("Welcome to the future - yes, we have flying cars!");
		} else if(year &lt; 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&lt;3; i++) {
			System.out.println(i + ") Hi there!");
		}

		//Numerical Array, While
		String rules[] = {"Do no harm", "Obey", "Continue Living"};
		i = 0;
		while(i&lt;rules.length) {
			System.out.println("Rule " + (i+1) + " : " + rules[i]);
			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!
		ncfom.printMovieDetails();

		// 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) {
				System.out.print((char)c);
			}
			in.close();
		} catch (IOException e) {
			System.out.println("Error");
		}

		System.out.print("\n");
		//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");

		//Replace
		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;
			this.rateMovie();
		}

		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);
		}
	}
}
</code></pre>
<p>Next Hello Script &#8211; C</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bin-co.com/blog/2008/06/hello-script-for-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
