{"id":26864,"date":"2021-05-03T23:10:06","date_gmt":"2021-05-04T03:10:06","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/example-java-programs-programming-code-examples-java-j2ee-j2me-j2se\/"},"modified":"2021-05-03T23:10:06","modified_gmt":"2021-05-04T03:10:06","slug":"example-java-programs-programming-code-examples-java-j2ee-j2me-j2se","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26864","title":{"rendered":"Example Java Programs #Programming Code Examples #Java\/J2EE\/J2ME #J2SE"},"content":{"rendered":"<pre>\nVery Simple Java Example Programs\nHelloWorld.java\n\npublic class HelloWorld {\n\n    \/\/ method main(): ALWAYS the APPLICATION entry point\n    public static void main (String[] args) {\n\tSystem.out.println (&quot;Hello World!&quot;);\n    }\n}\n<\/pre>\n<pre style='font-size:12px;padding:10px'>\n\n\/\/ Print Today's Date\nimport java.util.*;\n\npublic class HelloDate {\n    public static void main (String[] args) {\n\tSystem.out.println (&quot;Hello, it's: &quot;);\n\tSystem.out.println(new Date());\n    }\n}\n\n\n\/\/example: function call in Java\npublic class FunctionCall {\n\n    public static void funct1 () {\n\tSystem.out.println (&quot;Inside funct1&quot;);\n    }\n\n    public static void main (String[] args) {\n\tint val;\n\n\tSystem.out.println (&quot;Inside main&quot;);\n\n\tfunct1();\n\n\tSystem.out.println (&quot;About to call funct2&quot;);\n\n\tval = funct2(8);\n\n\tSystem.out.println (&quot;funct2 returned a value of &quot; + val);\n\n\tSystem.out.println (&quot;About to call funct2 again&quot;);\n\n\tval = funct2(-3);\n\n\tSystem.out.println (&quot;funct2 returned a value of &quot; + val);\n    }\n\n    public static int funct2 (int param) {\n\tSystem.out.println (&quot;Inside funct2 with param &quot; + param);\n\treturn param * 2;\n    }\n}\n\n\n\/\/Array in Java\npublic class ArrayDemo {\n    public static void main(String[] args) {\n\tint[] anArray;\t        \/\/ DECLARE an array of integers\n\n\tanArray = new int[10];\t\/\/ CREATE an array of integers\n\n\t\/\/ assign a value to each array element \n\tfor (int i = 0; i &lt; anArray.length; i++) {\n\t\tanArray[i] = i;\n\t    }\n\n\t\/\/ print a value from each array element\n\tfor (int i = 0; i &lt; anArray.length; i++) {\n\t    System.out.print(anArray[i] + &quot; &quot;);\n\t}\n\tSystem.out.println();\n    }\n}\n\n\n\/\/class usage in Java\nclass Point2d {\n    \/* The X and Y coordinates of the point--instance variables *\/\n    private double x;\n    private double y;\n    private boolean debug;\t\/\/ A trick to help with debugging\n\n    public Point2d (double px, double py) { \/\/ Constructor\n\tx = px;\n\ty = py;\n\n\tdebug = false;\t\t\/\/ turn off debugging\n    }\n\n    public Point2d () {\t\t\/\/ Default constructor\n\tthis (0.0, 0.0);        \/\/ Invokes 2 parameter Point2D constructor\n    }\n    \/\/ Note that a this() invocation must be the BEGINNING of\n    \/\/ statement body of constructor\n\n    public Point2d (Point2d pt) {\t\/\/ Another consructor\n\tx = pt.getX();\n\ty = pt.getY();\n\n\t\/\/ a better method would be to replace the above code with\n\t\/\/    this (pt.getX(), pt.getY());\n\t\/\/ especially since the above code does not initialize the\n\t\/\/ variable debug.  This way we are reusing code that is already\n\t\/\/ working.\n    }\n\n    public void dprint (String s) {\n\t\/\/ print the debugging string only if the &quot;debug&quot;\n\t\/\/ data member is true\n\tif (debug)\n\t    System.out.println(&quot;Debug: &quot; + s);\n    }\n\n    public void setDebug (boolean b) {\n\tdebug = b;\n    }\n\n    public void setX(double px) {\n\tdprint (&quot;setX(): Changing value of X from &quot; + x + &quot; to &quot; + px );\n\tx = px;\n    }\n\n    public double getX() {\n\treturn x;\n    }\n\n    public void setY(double py)  {\n\tdprint (&quot;setY(): Changing value of Y from &quot; + y + &quot; to &quot; + py );\n\ty = py;\n    }\n\n    public double getY() {\n\treturn y;\n    }\n\n    public void setXY(double px, double py) {\n\tsetX(px);\n\tsetY(py);\n    }\n\n    public double distanceFrom (Point2d pt) {\n\tdouble dx = Math.abs(x - pt.getX());\n\tdouble dy = Math.abs(y - pt.getY());\n\n\t\/\/ check out the use of dprint()\n\tdprint (&quot;distanceFrom(): deltaX = &quot; + dx);\n\tdprint (&quot;distanceFrom(): deltaY = &quot; + dy);\n\n\treturn Math.sqrt((dx * dx) + (dy * dy));\n    }\n\n    public double distanceFromOrigin () {\n\treturn distanceFrom (new Point2d ( ));\n    }\n\n    public String toStringForXY() {\n\tString str = &quot;(&quot; + x + &quot;, &quot; + y;\n\treturn str;\n    }\n\n    public String toString() {\n\tString str = toStringForXY() + &quot;)&quot;;\n\treturn str;\n    }\n}\n\n\n\n\/\/read integer from Keyboard\nimport java.io.*;\nimport java.util.*;\n\npublic class KeyboardIntegerReader {\n\n public static void main (String[] args) throws java.io.IOException {\n  String s1;\n  String s2;\n  int num = 0;\n\n  \/\/ set up the buffered reader to read from the keyboard\n  BufferedReader br = new BufferedReader (new InputStreamReader (\n\t\t\tSystem.in));\n\n  boolean cont = true;\n\n  while (cont)\n     {\n      System.out.print (&quot;Enter an integer:&quot;);\n      s1 = br.readLine();\n      StringTokenizer st = new StringTokenizer (s1);\n      s2 = &quot;&quot;;\n\n      while (cont &amp;&amp; st.hasMoreTokens())\n\t {\n          try \n          {\n           s2 = st.nextToken();\n           num = Integer.parseInt(s2);\n           cont = false;\n          }\n          catch (NumberFormatException n)\n          {\n           System.out.println(&quot;The value in &quot;&quot; + s2 + &quot;&quot; is not an \n\ninteger&quot;);\n          }\n\t}\n     }\n\n  System.out.println (&quot;You entered the integer: &quot; + num);\n }\n}\n\n\n\/\/read data from keyboard\nimport java.io.*;\nimport java.util.*;\n\npublic class KeyboardReader\n{\n\n public static void main (String[] args) throws java.io.IOException\n {\n\n  String s1;\n  String s2;\n\n  double num1, num2, product;\n\n  \/\/ set up the buffered reader to read from the keyboard\n  BufferedReader br = new BufferedReader (new InputStreamReader (\n\t\t\tSystem.in));\n\n  System.out.println (&quot;Enter a line of input&quot;);\n\n  s1 = br.readLine();\n\n  System.out.println (&quot;The line has &quot; + s1.length() + &quot; characters&quot;);\n\n  System.out.println ();\n  System.out.println (&quot;Breaking the line into tokens we get:&quot;);\n\n  int numTokens = 0;\n  StringTokenizer st = new StringTokenizer (s1);\n\n  while (st.hasMoreTokens())\n     {\n      s2 = st.nextToken();\n      numTokens++;\n      System.out.println (&quot;    Token &quot; + numTokens + &quot; is: &quot; + s2);\n     }\n }\n}\n\n\n\/\/read from file\nimport java.io.*;\nimport java.util.*;\n\npublic class MyFileReader\n{\n\n public static void main (String[] args) throws java.io.IOException\n {\n\n  String s1;\n  String s2;\n\n  \/\/ set up the buffered reader to read from the keyboard\n  BufferedReader br = new BufferedReader (new FileReader \n\n(&quot;MyFileReader.txt&quot;));\n\n  s1 = br.readLine();\n\n  System.out.println (&quot;The line is &quot; + s1);\n  System.out.println (&quot;The line has &quot; + s1.length() + &quot; characters&quot;);\n\n  System.out.println ();\n  System.out.println (&quot;Breaking the line into tokens we get:&quot;);\n\n  int numTokens = 0;\n  StringTokenizer st = new StringTokenizer (s1);\n\n  while (st.hasMoreTokens())\n     {\n      s2 = st.nextToken();\n      numTokens++;\n      System.out.println (&quot;    Token &quot; + numTokens + &quot; is: &quot; + s2);\n     }\n }\n}\n\n\n\/\/exception handling in Java\n\npublic class HelloWorldException {\n    public static void main (String[] args) throws Exception {\n        System.out.println(&quot;Bienvenitos!&quot;);\n        throw new Exception(&quot;Generic Exception&quot;);\n    }\n}\n\n\n\n\/\/exception handling\n\n\nimport java.io.*;\nimport java.util.*;\n\n\/** Causes a compilation error due to an unhandled Exception\n *\/\npublic class KeyboardReaderError {\n\n  public static void main (String[] args) { \/\/ throws java.io.IOException\n\n    String s1;\n    String s2;\n\n    double num1, num2, product;\n\n    \/\/ set up the buffered reader to read from the keyboard\n    BufferedReader br = new BufferedReader (new InputStreamReader \n\n(System.in));\n\n    System.out.println (&quot;Enter a line of input&quot;);\n    \n    \/* Following line triggers the error.  Error will show the type of\n       unhandled exception and where the call occurs *\/\n    s1 = br.readLine();\n\n    System.out.println (&quot;The line has &quot; + s1.length() + &quot; characters&quot;);\n\n    System.out.println ();\n    System.out.println (&quot;Breaking the line into tokens we get:&quot;);\n\n    int numTokens = 0;\n    StringTokenizer st = new StringTokenizer (s1);\n\n    while (st.hasMoreTokens()) {\n\ts2 = st.nextToken();\n\tnumTokens++;\n\tSystem.out.println (&quot;    Token &quot; + numTokens + &quot; is: &quot; + s2);\n    }\n  }\n}\n\n\/\/encounters an error\n\nimport java.io.*;\n\n\/\/ This program shows a stack track that occurs when java\n\/\/ encounters a terminal error when running a program.\n\npublic class DivBy0\n{\n\n public static void funct1 ()\n {\n  System.out.println (&quot;Inside funct1()&quot;);\n\n  funct2();\n }\n\n public static void main (String[] args)\n {\n  int val;\n\n  System.out.println (&quot;Inside main()&quot;);\n\n  funct1();\n\n }\n\n public static void funct2 ()\n {\n  System.out.println (&quot;Inside funct2()&quot;);\n  int i, j, k;\n\n  i = 10;\n  j = 0;\n\n  k = i\/j;\n }\n\n}\n\n\n\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10198<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, J2SE<br \/>Tags:Java\/J2EE\/J2MEJ2SE<br \/> Post Data:2017-01-02 16:04:23<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Very Simple Java Example Programs HelloWorld.java public class HelloWorld { \/\/ method main(): ALWAYS the APPLICATION entry point public static void main (String[] args) { System.out.println (&quot;Hello World!&quot;); } } \/\/ Print Today&#8217;s Date import java.util.*; public class HelloDate { public static void main (String[] args) { System.out.println (&quot;Hello, it&#8217;s: &quot;); System.out.println(new Date()); } } &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26864\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26864","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10104,"url":"http:\/\/bangla.sitestree.com\/?p=10104","url_meta":{"origin":26864,"position":0},"title":"Example Java Programs","author":"","date":"August 3, 2015","format":false,"excerpt":"HelloWorld.java public class HelloWorld { \u00a0\u00a0\u00a0 \/\/ method main(): ALWAYS the APPLICATION entry point \u00a0\u00a0\u00a0 public static void main (String[] args) { \u00a0\u00a0 \u00a0System.out.println (\"Hello World!\"); \u00a0\u00a0\u00a0 } } \/\/ Print Today's Date import java.util.*; public class HelloDate { \u00a0\u00a0\u00a0 public static void main (String[] args) { \u00a0\u00a0 \u00a0System.out.println (\"Hello,\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":22134,"url":"http:\/\/bangla.sitestree.com\/?p=22134","url_meta":{"origin":26864,"position":1},"title":"Pretty Simple Java Code #SCJP","author":"Sayed","date":"March 10, 2021","format":false,"excerpt":"\/** * The HelloWorldApp class implements an application that * simply prints \"Hello World!\" to standard output. *\/ class HelloWorldApp { public static void main(String[] args) { System.out.println(\"Hello Worlld\"); } } class ArrayDemo { public static void main(String[] args) { \/\/ declares an array of integers int[] anArray; int[] theArray;\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10543,"url":"http:\/\/bangla.sitestree.com\/?p=10543","url_meta":{"origin":26864,"position":2},"title":"Example demonstrating the use of packages","author":"","date":"August 29, 2015","format":false,"excerpt":"&&&&&&&&&&&&&&&&&&& Example demonstrating the use of packages. \u00a0\u00a0\u00a0 * Class1.java defined in package1. \u00a0\u00a0\u00a0 * Class2.java defined in package2. \u00a0\u00a0\u00a0 * Class3.java defined in package2.package3. \u00a0\u00a0\u00a0 * Class1.java defined in package4. \u00a0\u00a0\u00a0 * PackageExample.java Driver for package example &&&&&&&&&&&&&&&&&&&&& ~~~~~~~~~~~~~~~~~~~~~ Class1.java defined in package1. ~~~~~~~~~~~~~~~~~~~~~ package package1; ***************** \u00a0 public\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10546,"url":"http:\/\/bangla.sitestree.com\/?p=10546","url_meta":{"origin":26864,"position":3},"title":"Statics.java Demonstrates static and non-static methods.","author":"","date":"August 29, 2015","format":false,"excerpt":"*\/ public class Statics { \u00a0public static void main(String[] args) { \u00a0\u00a0\u00a0 staticMethod(); \u00a0\u00a0\u00a0 Statics s1 = new Statics(); \u00a0\u00a0\u00a0 s1.regularMethod(); \u00a0 } \u00a0 public static void staticMethod() { \u00a0\u00a0\u00a0 System.out.println(\"This is a static method.\"); \u00a0 } \u00a0 public void regularMethod() { \u00a0\u00a0\u00a0 System.out.println(\"This is a regular method.\"); \u00a0 }\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10194,"url":"http:\/\/bangla.sitestree.com\/?p=10194","url_meta":{"origin":26864,"position":4},"title":"UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server.","author":"","date":"August 25, 2015","format":false,"excerpt":"UrlRetriever2.java\u00a0 Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; \/** Read a remote file using the standard URL class \u00a0*\u00a0 instead of connecting explicitly to the HTTP server. \u00a0* \u00a0*\u00a0 Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10517,"url":"http:\/\/bangla.sitestree.com\/?p=10517","url_meta":{"origin":26864,"position":5},"title":"StringTest.java Demonstrates various methods of the String class.","author":"","date":"August 29, 2015","format":false,"excerpt":"\/** Taken from Core Web Programming from \u00a0*\u00a0 Prentice Hall and Sun Microsystems Press, \u00a0*\u00a0 . \u00a0*\u00a0 \u00a9 2001 Marty Hall and Larry Brown; \u00a0*\u00a0 may be freely used or adapted. \u00a0*\/ \u00a0 public class StringTest { \u00a0 public static void main (String[] args) { \u00a0\u00a0\u00a0 String str = \"\";\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26864"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26864\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26864"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}