{"id":111,"date":"2008-06-11T23:54:01","date_gmt":"2008-06-11T18:24:01","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/?p=111"},"modified":"2008-06-11T23:54:01","modified_gmt":"2008-06-11T18:24:01","slug":"hello-script-for-java","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2008\/06\/hello-script-for-java\/","title":{"rendered":"Hello Script for Java"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" 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>\n<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>\n<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>\n<h2>Code<\/h2>\n<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>\n<pre><code class=\"java\">\nimport java.io.*;\nimport java.util.regex.*;\n\npublic class Hello {\n\tpublic static void main(String[] Args) {\n\t\t\/\/ Printing(IO)\n\t\tSystem.out.println(\"Hello World\");\n\t\t\n\t\t\/\/ Variables, concatenation\n\t\tString name = \"Binny\";\n\t\tint year = 2008;\n\t\t\n\t\tSystem.out.println(\"Hello, \" + name + \" - welcome to \" + year);\n\t\t\n\t\tif(year &gt; 2008) {\n\t\t\tSystem.out.println(\"Welcome to the future - yes, we have flying cars!\");\n\t\t} else if(year &lt; 2008) {\n\t\t\tSystem.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!\");\n\t\t} else {\n\t\t\tSystem.out.println(\"Anything wrong with your time machine? You have not gone anywhere, kiddo.\");\n\t\t}\n\t\t\n\t\t\/\/ For loop\n\t\tint i=0;\n\t\tfor(i=0; i&lt;3; i++) {\n\t\t\tSystem.out.println(i + \") Hi there!\");\n\t\t}\n\t\t\n\t\t\/\/Numerical Array, While\n\t\tString rules[] = {\"Do no harm\", \"Obey\", \"Continue Living\"};\n\t\ti = 0;\n\t\twhile(i&lt;rules.length) {\n\t\t\tSystem.out.println(\"Rule \" + (i+1) + \" : \" + rules[i]);\n\t\t\ti++;\n\t\t}\n\t\t\n\t\t\/\/ Associated array, foreach\n\t\t\/\/ Hmm - does Java have Associated arrays? ArrayList?\n\t\t\n\t\tString csv_string = \"hello,world,how,are,you\";\n\t\tString csv_values[] = csv_string.split(\",\");\n\t\t\/\/ No native Join method\n\t\t\n\t\t\/\/ Function, argument, return, call\n\t\tSystem.out.println(Hello.hello(\"Binny\")); \/\/The function definition is at the end of this file.\t\n\n\t\t\/\/Class stuff...\n\t\tMovie ncfom = new Movie(\"New Country for Old Men\"); \/\/It's a sequel!\n\t\tncfom.printMovieDetails();\n\n\t\t\/\/ File IO\n\t\t\/\/ File reading, easy method...\n\t\ttry {\n\t\t\tFile read_file = new File (\"\/tmp\/Hello.txt\");\n\t\t\tFileReader in_stream = new FileReader(read_file); \/\/ Create a Character Input Stream\n\t\t\tBufferedReader in = new BufferedReader(in_stream);\/\/ Filter the Input Stream - buffers characters for efficiency\n\t\t\ttry {\n\t\t\t\tSystem.out.println(in.readLine()); \/\/ read the first line\n\t\t\t} catch(IOException E) {\n\t\t\t\tSystem.out.println(\"No idea what went wrong. Sorry!\");\n\t\t\t}\n\t\t} catch(FileNotFoundException E) {\n\t\t\tSystem.out.println(\"File not found. Sorry!\");\n\t\t}\n\t\t\n\t\ttry {\n\t\t\t\/\/ Writing to a file\n\t\t\tFile out_file = new File(\"\/tmp\/HelloJava.txt\");\n\t\t\tFileOutputStream out_stream = new FileOutputStream(out_file); \/\/ Create an Output Stream\n\t\t\tPrintWriter out = new PrintWriter(out_stream); \/\/ Filter bytes to ASCII\n\t\t\tout.println(\"Hello, from Java\"); \/\/ Here we actually write to file\n\t\t} catch(java.io.FileNotFoundException E) {\n\t\t\tSystem.out.println(\"File not found. Sorry!\");\n\t\t}\n\t\t\n\t\tSystem.out.println(\"\\nLS command results...\");\n\t\t\/\/ Command Executing\n\t\ttry {\n\t\t\t\/\/ Execute a command\n\t\t\tString command = \"ls\";\n\t\t\tProcess child = Runtime.getRuntime().exec (command);\n\t\t\n\t\t\t\/\/ Read from an input stream\n\t\t\tInputStream in = child.getInputStream();\n\t\t\tint c;\n\t\t\twhile ((c = in.read()) != -1) {\n\t\t\t\tSystem.out.print((char)c);\n\t\t\t}\n\t\t\tin.close();\n\t\t} catch (IOException e) {\n\t\t\tSystem.out.println(\"Error\");\n\t\t}\n\t\t\n\t\tSystem.out.print(\"\\n\");\n\t\t\/\/Regular Expression\n\t\tString str = new String(\"Hello World\");\n\t\t\n\t\t\/\/Find a pattern\n\t\tPattern hell_check = Pattern.compile(\"^Hell\");\n\t\tMatcher matches = hell_check.matcher(str);\n\t\tif(matches.find()) System.out.println(\"Yup - its evil\");\n\t\t\n\t\t\/\/Replace\n\t\tSystem.out.println(str.replaceAll(\"l([^l])\", \"$1\")); \/\/Remove an 'l' from both words. Should print 'Helo Word'\n\n\t}\n\t\n\t\/\/Function declaration.\n\tprivate static String hello(String name) {\n\t\treturn \"Hello, \" + name;\n\t}\n\t\n\t\/\/ One for the OOP fanboys - Class, members, object and stuff.\n\tprivate static class Movie {\n\t\tpublic String name = \"\";\n\t\tpublic int rating = 0;\n\t\t\n\t\tpublic Movie(String name) {\n\t\t\tthis.name = name;\n\t\t\tthis.rateMovie();\n\t\t}\n\t\t\n\t\tpublic void rateMovie() {\n\t\t\tthis.rating = (this.name.length() % 10) + 1; \/\/IMDBs rating algorithm. True story!\n\t\t}\n\t\t\n\t\tpublic void printMovieDetails() {\n\t\t\tSystem.out.println(\"Movie  : \" + this.name);\n\t\t\tSystem.out.println(\"Rating : \" + this.rating);\n\t\t}\n\t}\n}\n<\/code><\/pre>\n<p>Next Hello Script &#8211; C<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>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 <a class=\"mh-excerpt-more\" href=\"https:\/\/www.bin-co.com\/blog\/2008\/06\/hello-script-for-java\/\" title=\"Hello Script for Java\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,28],"tags":[80,137,154,245,289],"class_list":["post-111","post","type-post","status-publish","format-standard","hentry","category-scripts","category-tutorial","tag-code","tag-hello","tag-java","tag-script","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}