Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java Basics


Published on

Core Java Basics

Java Basics

  1. 1. Java Basics
  2. 2. Objectives <ul><li>Class </li></ul><ul><li>Objects </li></ul><ul><li>Identifiers </li></ul><ul><li>Methods </li></ul><ul><li>Encapsulation, inheritance, polymorphism </li></ul><ul><li>Constructors </li></ul><ul><li>GC </li></ul>
  3. 3. Class <ul><li>Template </li></ul><ul><li>State and behaviour of object at run time </li></ul><ul><li>Abstract class </li></ul><ul><li>Final class </li></ul>
  4. 4. Interface <ul><li>A 100% abstract class </li></ul><ul><li>A contact for what a implementing class can do. </li></ul><ul><li>Behavioural purposes </li></ul><ul><li>Class can implement multiple interfaces </li></ul>
  5. 5. Variables <ul><li>Primitives </li></ul><ul><li>Reference </li></ul><ul><li>Local variables </li></ul><ul><li>Arrays </li></ul><ul><li>Final variables </li></ul><ul><li>Transient </li></ul>
  6. 6. Methods <ul><li>Final Methods </li></ul><ul><li>Final Arguments </li></ul><ul><li>Abstract methods </li></ul><ul><li>Synchronized </li></ul><ul><li>Native </li></ul><ul><li>Strictfp </li></ul><ul><li>Var-args </li></ul>
  7. 7. Encapsulation <ul><li>Keep data protected </li></ul><ul><li>Keep services open </li></ul><ul><li>Setters and getters method </li></ul>
  8. 8. Inheritance <ul><li>Re-usability </li></ul><ul><li>Sometimes we have no implementation </li></ul><ul><li>Is-a vs has-a relationship </li></ul>
  9. 9. Polymorphism <ul><li>Overloading </li></ul><ul><li>Overridding </li></ul>
  10. 10. Constructors <ul><li>Need? </li></ul><ul><li>Default constructor </li></ul><ul><li>Constructor overloading </li></ul><ul><li>Constructor calls in inheritance tree </li></ul>
  11. 11. Static <ul><li>Functions </li></ul><ul><li>Variables </li></ul>
  12. 12. Memory Management <ul><li>Stack </li></ul><ul><li>Heap </li></ul><ul><li>Garbage collector </li></ul>
  13. 13. Exception <ul><li>What are exceptions? </li></ul><ul><li>Exceptions VS Program Errors </li></ul><ul><li>Exceptions VS Errors </li></ul>
  14. 14. Exception handling <ul><li>Try catch finally block </li></ul><ul><li>Catching exception </li></ul><ul><li>Ducking exception </li></ul><ul><li>Creating new exceptions </li></ul><ul><li>Checked / Unchecked exceptions </li></ul>
  15. 15. Time for some practical example <ul><li>A rudimentary banking application </li></ul><ul><li>Objective: Create some customers and create their saving and current accounts. Display them on the screen. That’s it! </li></ul>
  16. 16. Practical Example 2 <ul><li>Duck simulation game once again? </li></ul><ul><li>Objective: To show different type of ducks on the screen (Time being each duck can be represented by different text). Some of the ducks can fly / quack while some cannot. Code should first be implemented for Mallard and rubber duck. It should be expandable for other duck type. </li></ul>
  17. 17. Collections – some groundwork <ul><li>toString() method (To be covered later) </li></ul><ul><li>“ ==“ VS “equals()” </li></ul><ul><li>== compares references. It’s a bit to bit comparison. </li></ul><ul><li>However, equals method is overridden by some classes to compares the values of reference variables for ex: String, Integer etc. </li></ul><ul><li>Example @ Eclipse </li></ul>
  18. 18. Collections – some groundwork 2 <ul><li>When to override equals? </li></ul><ul><li>For class Car? </li></ul><ul><li>Color </li></ul><ul><li>Engine </li></ul><ul><li>Make </li></ul><ul><li>2WD / 4WD </li></ul><ul><li>VIN </li></ul>
  19. 19. Collections – some groundwork 3 <ul><li>hashCode() </li></ul><ul><li>Object ID, not necessarily unique, used my HashMap / HashSet etc to store reference to the object. hashCode is used to locate the object in the memory. </li></ul>
  20. 20. Collections – An Introduction <ul><li>What are collections? </li></ul><ul><li>What do we do with collections? </li></ul><ul><li>Add objects </li></ul><ul><li>Remove objects </li></ul><ul><li>Find an Object </li></ul><ul><li>Iterate through collection </li></ul>
  21. 21. Collections - continued <ul><li>Key 9 interfaces: </li></ul><ul><li>Collection, Set, SortedSet </li></ul><ul><li>List, Map, SortedMap </li></ul><ul><li>Queue, NavigableSet, NavigableMap </li></ul>
  22. 22. Collections - continued <ul><li>Ordered VS Sorted </li></ul><ul><li>Mainly used concrete classes: </li></ul><ul><li>Maps – HashMap, Hashtable, TreeMap, LinkedHashMap </li></ul><ul><li>Sets – HashSet, LinkedHashSet, TreeSet </li></ul><ul><li>Lists – ArrayList, Vector, LinkedList </li></ul><ul><li>Queues – PriorityQueue </li></ul><ul><li>Utilities – Collections, Arrays </li></ul>
  23. 23. Collections - Sorting <ul><li>Collections.sort </li></ul><ul><li>OK – But what about classes – How can we compare them? </li></ul><ul><li>Comparable Interface – compareTo method </li></ul><ul><li>Comparator Interface – compare method </li></ul><ul><li>Examples at Eclipse </li></ul>
  24. 24. Collections - Search <ul><li>Searches are performed using binarySearch() </li></ul><ul><li>Successful searches return index of element being searched </li></ul><ul><li>Collection / Array should be sorted to facilitate search </li></ul><ul><li>If searched element is not there, then the return value of search is = -(index at which if searched element is inserted, sorted order will be maintained) -1. Like a, c, d, e, f if we search for b, return value will be -2 </li></ul><ul><li>Array / Collection is sorted using comparator, same comparator should be used in search. </li></ul>
  25. 25. Inner Classes <ul><li>A Class can have: </li></ul><ul><li>Instance variable </li></ul><ul><li>Methods </li></ul><ul><li>Classes???? </li></ul><ul><li>Yes, but what is the use? </li></ul>
  26. 26. Inner Classes – Purpose and Use <ul><li>Chat client example: </li></ul><ul><li>Normal operation like typing, sending, getting messages from server can be done in ChatClass. But, what about even handling messages? Where should they be? Options are: </li></ul><ul><ul><li>Same class </li></ul></ul><ul><ul><li>Other class </li></ul></ul><ul><ul><li>Inner class </li></ul></ul>
  27. 27. Inner classes - types <ul><li>(Regular) Inner classes </li></ul><ul><li>Method local Inner classes </li></ul><ul><li>Anonymous inner classes </li></ul><ul><li>Static inner classes </li></ul>
  28. 28. Inner Classes <ul><li>Regular Inner Class (Eclipse) </li></ul><ul><li>Method Inner Class </li></ul><ul><ul><li>Same as regular inner class with following differences: </li></ul></ul><ul><ul><li>declared inside method </li></ul></ul><ul><ul><li>Cannot be instantiated outside method </li></ul></ul><ul><ul><li>Cannot use method local variables </li></ul></ul><ul><ul><li>Can not use modifiers like public, private, protected, static </li></ul></ul>
  29. 29. Inner Classes <ul><li>Anonymous Inner classes - Example at Eclipse </li></ul><ul><li>Static Nested classes – Example at Eclipse </li></ul>
  30. 30. Threads <ul><li>A thread is a single sequential flow of control within a program. </li></ul><ul><li>Multi-threading </li></ul><ul><li>Types </li></ul><ul><ul><li>Extending thread class </li></ul></ul><ul><ul><li>Implementing runnable interface </li></ul></ul>
  31. 31. States of Thread <ul><li>New </li></ul><ul><li>Runnable </li></ul><ul><li>Running </li></ul><ul><li>Waiting/Blocked/Sleeping </li></ul><ul><li>Dead </li></ul>Waiting / Blocked / Sleeping Dead Running New Runnable
  32. 32. Thread execution <ul><li>JVM & Scheduler </li></ul><ul><li>Round-robin </li></ul><ul><li>Time slicing scheduler </li></ul><ul><li>Thread priorities </li></ul><ul><li>Sleep </li></ul><ul><li>Yield </li></ul><ul><li>Join </li></ul>
  33. 33. Multi-Threading <ul><li>Race condition </li></ul><ul><li>Preventing race condition </li></ul><ul><ul><li>Finding atomic operations </li></ul></ul><ul><ul><li>Make variable private </li></ul></ul><ul><ul><li>Synchronize the code changing variables </li></ul></ul>
  34. 34. Java I/O <ul><li>I/O Streams </li></ul><ul><ul><li>Byte Streams handle I/O of raw binary data. </li></ul></ul><ul><ul><li>Character Streams handle I/O of character data, automatically handling translation to and from the local character set. </li></ul></ul><ul><ul><li>Buffered Streams optimize input and output by reducing the number of calls to the native API. </li></ul></ul><ul><ul><li>Scanning and Formatting allows a program to read and write formatted text. </li></ul></ul><ul><ul><li>I/O from the Command Line describes the Standard Streams and the Console object. </li></ul></ul><ul><ul><li>Data Streams handle binary I/O of primitive data type and String values. </li></ul></ul><ul><ul><li>Object Streams handle binary I/O of objects. </li></ul></ul><ul><li>File I/O </li></ul><ul><ul><li>File Objects help you to write platform-independent code that examines and manipulates files. </li></ul></ul><ul><ul><li>Random Access Files handle non-sequential file access. </li></ul></ul>
  35. 35. Stream IO <ul><li>Decendents of classes: </li></ul><ul><ul><li>InputStream </li></ul></ul><ul><ul><li>OutputStream </li></ul></ul><ul><ul><li>For example: </li></ul></ul><ul><ul><li>FileInputStream </li></ul></ul><ul><ul><li>FileOutputStream </li></ul></ul>
  36. 36. Character IO <ul><li>Wraps byte stream. </li></ul><ul><li>All character stream classes are descended from </li></ul><ul><ul><li>Reader </li></ul></ul><ul><ul><li>Writer </li></ul></ul><ul><ul><li>For Example: </li></ul></ul><ul><ul><li>FileReader </li></ul></ul><ul><ul><li>FileWriter </li></ul></ul>
  37. 37. Buffered IO <ul><li>Buffered Streams: </li></ul><ul><ul><li>BufferedInputStream </li></ul></ul><ul><ul><li>BufferedOutputStream </li></ul></ul><ul><ul><li>BufferedReader </li></ul></ul><ul><ul><li>BufferedWriter </li></ul></ul><ul><li>Flush() </li></ul>
  38. 38. Scanning and formatting <ul><li>Scanner API </li></ul><ul><ul><li>Breaks input stream into Tokens </li></ul></ul><ul><li>Formatting API </li></ul><ul><ul><li>PrintWriter </li></ul></ul><ul><ul><li>PrintStream </li></ul></ul><ul><ul><li>Format method </li></ul></ul>
  39. 39. Command Line IO <ul><li>Standard Streams by: </li></ul><ul><ul><li>InputStreamReader( </li></ul></ul><ul><ul><li>OutputStreamWriter(System.out) </li></ul></ul><ul><li>Console class: </li></ul><ul><ul><li>C.readLine() – String </li></ul></ul><ul><ul><li>C.readPassword – Character[] </li></ul></ul>
  40. 40. Data Streams <ul><li>To read write primitives </li></ul><ul><li>DataInputStream </li></ul><ul><li>DataOutputStream </li></ul>
  41. 41. Object IO <ul><li>To read write state of object </li></ul><ul><ul><li>ObjectInputStream </li></ul></ul><ul><ul><li>ObjectOutputStream </li></ul></ul>
  42. 42. File Operations <ul><li>Class File </li></ul><ul><ul><li>Methods: </li></ul></ul><ul><ul><ul><li>Create file </li></ul></ul></ul><ul><ul><ul><li>Change file attributes </li></ul></ul></ul><ul><ul><ul><li>Read / write operation using FileReader / FileWriter of Buffered Readers and writer </li></ul></ul></ul><ul><ul><ul><li>Delete files </li></ul></ul></ul><ul><ul><ul><li>Rename files </li></ul></ul></ul><ul><ul><ul><li>Create directories </li></ul></ul></ul><ul><li>Class RandomAccessFile </li></ul><ul><ul><li>Constructor RandomAccessFile(filename, mode); </li></ul></ul><ul><ul><li>Functions: skipBytes, seek, getFilePointer </li></ul></ul>
  43. 43. Java nio operations <ul><li>Speed IO </li></ul><ul><li>Block based IO </li></ul><ul><li>File read: </li></ul><ul><ul><li>FileInputStream fin = new FileInputStream( &quot;readandshow.txt&quot; ); </li></ul></ul><ul><ul><li>FileChannel fc = fin.getChannel(); </li></ul></ul><ul><ul><li>ByteBuffer buffer = ByteBuffer.allocate( 1024 ); </li></ul></ul><ul><ul><li> buffer ); </li></ul></ul><ul><li>File Write </li></ul><ul><ul><li>FileOutputStream fout = new FileOutputStream( &quot;writesomebytes.txt&quot; ); </li></ul></ul><ul><ul><li>FileChannel fc = fout.getChannel(); </li></ul></ul><ul><ul><li>ByteBuffer buffer = ByteBuffer.allocate( 1024 ); </li></ul></ul><ul><ul><li>for (int i=0; i<message.length; ++i) { </li></ul></ul><ul><ul><li>buffer.put( message[i] ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>buffer.flip(); </li></ul></ul><ul><ul><li>fc.write( buffer ); </li></ul></ul>
  44. 44. Serialization <ul><li>Preserving state of object in a file </li></ul><ul><li>Called serialization or flattening of objects </li></ul><ul><li>Three ways to do Serialization: </li></ul><ul><ul><li>using the default protocol </li></ul></ul><ul><ul><li>customizing the default protocol </li></ul></ul><ul><ul><li>creating our own protocol </li></ul></ul>
  45. 45. Serialization using the default protocol <ul><li>You have to implement interface “Serializable” </li></ul><ul><li>This interface has no methods. So it is just a marker interface. </li></ul><ul><li>ObjectOutputStream </li></ul>
  46. 46. Serialization using Customizing the Default Protocol - I <ul><li>What will happen if our class contains objects of some other classes which are not serializable? </li></ul>
  47. 47. Serialization using Customizing the Default Protocol - II <ul><li>We have to mark those classes as serializable? </li></ul><ul><li>What if we don’t have access to code of those classes? </li></ul><ul><ul><li>In case, it is not important to save state of those objects, the way is to mark the variables referring to those objects as transient. </li></ul></ul><ul><ul><li>But, what if we also want to save state of object. </li></ul></ul>
  48. 48. Serialization using Customizing the Default Protocol - III <ul><li>So, What should we do to instantiate transient variables? Call our own method after de-serialization? </li></ul><ul><li>Of course this is a solution. But, what if the other developers does not know about that? </li></ul><ul><li>What should we do now? </li></ul>
  49. 49. Serialization using Customizing the Default Protocol - II <ul><li>Provide following two methods in your class: </li></ul><ul><ul><li>private void writeObject(ObjectOutputStream out) throws IOException; </li></ul></ul><ul><ul><li>private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException; </li></ul></ul><ul><ul><li>Do your operations and then call: </li></ul></ul><ul><ul><li>defaultWriteObject(); </li></ul></ul><ul><ul><li>defaultReadObject(); </li></ul></ul><ul><ul><li>Note that these methods are private. So they are not inhereted, overloaded. In fact, when VM sees that class provides implementation of these methods it call these methods than default methods. Remember, VM can call private methods, any other oject cannot. </li></ul></ul><ul><ul><li>Also, they can be used to make a call non-serializable even if super class is serializable. </li></ul></ul>
  50. 50. Serialization using Creating our Own Protocol <ul><li>Instead of implementing the Serializable interface, you can implement Externalizable, which contains two methods: </li></ul><ul><ul><li>public void writeExternal(ObjectOutput out) throws IOException; </li></ul></ul><ul><ul><li>public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException; </li></ul></ul><ul><li>Just override those methods to provide your own protocol. This protocol is entirely in your hands. An example situation for that alternate type of serialization: read and write PDF files with a Java application. If you know how to write and read PDF (the sequence of bytes required), you could provide the PDF-specific protocol in the writeExternal and readExternal methods. </li></ul>
  51. 51. References <ul><li>Head First Java </li></ul><ul><li>SCJP Guide – Kethy Sierra </li></ul><ul><li>Thinking in Java vol.3 </li></ul>
  52. 52. Queries
  53. 53. Next Sessions – Servlets and JSP <ul><li>Pre-requisites: </li></ul><ul><ul><li>Basic knowledge of HTTP - h ttp:// </li></ul></ul><ul><ul><li>Basic Knowledge of HTML – Youtube video - Basic HTML and CSS Tutorial. Howto make website from scratch </li></ul></ul><ul><ul><li>Introduction to Enterprise applications - </li></ul></ul><ul><ul><li>Basic Knowledge of J2EE - Youtube videos - </li></ul></ul><ul><ul><ul><li>JEE Introduction </li></ul></ul></ul><ul><ul><ul><li>JAVA Enterprise Edition Tutorial </li></ul></ul></ul><ul><ul><ul><li>Using the HttpSession Object: Servlet and JSP Tutorials J2EE </li></ul></ul></ul>