This document provides an overview of Java essentials including:
- Java addressed issues with C/C++ like inconsistent frameworks and memory management issues.
- Java introduced a common framework, garbage collection, and object oriented programming.
- The history of Java versions from 1.0 to 6.0 is summarized.
- A simple "Hello World" Java program is broken down and explained.
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
Java essentials guide covers language history and basics
1. JAVA ESSENTIALS BY RICH HELTON (SUN CERTIFIED (SC) JAVA PROGRAMMER, SC JAVA DEVELOPER, SC ENTERPRISE ARCHITECT) January 2009
2.
3.
4. Java, the history … Version Year New Features 1.0 1996 1.1 1997 Inner classes 1.2 1998 Swing, Collections 1.3 2000 Performance enhancements 1.4 2002 Assertions, XML 5 2004 Generic classes, enhanced for loop, auto-boxing, enumerations 6 2006 Library improvements
5. My First Java Program… public class MyFirstJavaProgram { // Start the program public static void main(String[] args){ if(args.length >0){ // If argument print name String myName = (String) args[0]; System.out.println("Hello " +myName); }else{ // Else print System.out.println("Hello there"); } } }
14. Sample Reserved Words public class MyFirstJavaProgram { // Start the program public static void main(String[] args){ if (args.length >0){ // If argument print name String myName = (String) args[0]; System.out.println("Hello " +myName); } else { // Else print System.out.println("Hello there"); } } }
15.
16.
17. Sample Modifiers public class MyFirstJavaProgram { // Start the program public static void main(String[] args){ if(args.length >0){ // If argument print name String myName = (String) args[0]; System.out.println("Hello " +myName); }else{ // Else print System.out.println("Hello there"); } } }
60. Create a File import java.io.*; public class CreateAFile { public static void main(String[] argv) throws IOException { // Ensure that a filename (or something) was given in argv[0] if (argv.length == 0) { System.err.println("Usage: CreateAFile filename"); System.exit(1); } // If arg is filled then create that file for (int i = 0; i< argv.length; i++) { new File(argv[i]).createNewFile( ); } } }
61.
62. Open a File import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FileReader { public static void main(String[] args) { try{ Scanner text = new Scanner( new File( "Test.txt" ) ); }catch(FileNotFoundException ex){ System. out.println("Error: " +ex.getMessage()); } } }
74. Browsing becomes simplified public static void main(String[] args) { org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display .getDefault(); SimpleSWTBrowser thisClass = new SimpleSWTBrowser(); thisClass.createSShell(); thisClass.sShell.open(); while (!thisClass.sShell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } //The URL is called in the createSShell code
80. Now it is an XML object <?xml version="1.0" encoding="UTF-8"?> <java version="1.6.0_07" class="java.beans.XMLDecoder"> <object class="MyData"> <void property="name"> <string>testXML</string> </void> </object> </java>
81.
82. SAX import java.io.IOException; import org.xml.sax.*; public class SAXParser{ public static void main(String[] args) throws Exception { new SAXParser(args); } public SAXParser(String[] args) throws SAXException, IOException { XMLReader parser = XMLReaderFactory. createXMLReader(); parser.setContentHandler(new MyDataHandler()); parser.parse(args.length == 1 ? args[0] : "Test.xml"); }
83. SAX (handler) // Inner class provides DocumentHandler class MyDataHandler extends DefaultHandler { boolean name = false; // Set to true when string tag is found public void startElement(String nsURI, String localName, String rawName, Attributes attributes) throws SAXException { if (rawName.equalsIgnoreCase("string")) name = true; } public void characters(char[] ch, int start, int length) { if (name) { //Will print all name strings found System. out.println("MyData name: " + new String(ch, start, length)); name = false; } } } }
88. Arrays, the code public class ArrayProgram { public static void main(String[] args){ String [] strArray = new String[3]; strArray[0] = "One"; //Java starts at 0 strArray[1] = "Two"; strArray[2] = "Three"; int [] [] intArray = new int[2][]; // 2 rows intArray[0] = new int[3]; // three columns for row 0 intArray[1] = new int[4]; // four columns for row 1 int data = 1; for(int i1 = 0; i1 < intArray.length; i1++){ for(int i2 = 0; i2 < intArray[i1].length; i2++){ intArray[i1][i2] = data++; } }
89.
90.
91.
92.
93.
94. Switch demo class SwitchDemo { public static void main(String[] args) { int month = 8; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month.");break; }}}
95.
96.
97.
98.
99. “ break” and “continue example public static void main(String[] args) { boolean bFirstLoop = true, boolean bBreakMe = true, bContinue = true; while(bFirstLoop){ System.out.println("While Loop"); if(bBreakMe) break; // Prints only one “While Loop” } for(int i = 0; i < 3;i++){ if(bContinue){ bContinue = false; continue; } System.out.println("For Loop"); //Prints twice, skips first time } }
100.
101.
102. Linked List, some code import java.util.LinkedList; public class ListProgram { public static void main(String[] args) { LinkedList < String> strList = new LinkedList <String>(); strList.add("One"); strList.add("Two"); strList.add("Three"); System. out.print("Output -> The Linked List vaues ="); for(int i1 = 0; i1 < strList.size(); i1++) System. out.printf("%s ", strList.get(i1)); } } Output -> The Linked List vaues =One Two Three