How to Learn core Java step by step

lecture notes on core java, core java lecture notes free download, how to run core java program in eclipse, how to create core java project in eclipse pdf free download
Dr.DavisHardy Profile Pic
Dr.DavisHardy,Germany,Teacher
Published Date:22-07-2017
Your Website URL(Optional)
Comment
Handout: Core Java Version: Core Java/Handout/0408/1.0 Date: 30-04-08 Cognizant 500 Glen Pointe Center West Teaneck, NJ 07666 Ph: 201-801-0233 www.cognizant.com Handout - Core Java TABLE OF CONTENTS Introduction ................................................................................................................................. 28  About this Module ....................................................................................................................... 28  Target Audience ......................................................................................................................... 28  Module Objectives ...................................................................................................................... 28  Pre-requisite ............................................................................................................................... 28  Session 02: Introduction to OOPS .............................................................................................. 29  Learning Objectives .................................................................................................................... 29  Object Oriented Programming .................................................................................................... 29  Procedural Versus OOP ............................................................................................................. 29  Objects ........................................................................................................................................ 29  Class ........................................................................................................................................... 30  Try It Out ..................................................................................................................................... 30  Tips and Tricks ........................................................................................................................... 31  Summary .................................................................................................................................... 31  Test Your Understanding ............................................................................................................ 31  Session 03: Introduction to OOPS .............................................................................................. 33  Learning Objectives .................................................................................................................... 33  Abstraction .................................................................................................................................. 33  Encapsulation ............................................................................................................................. 33  Class Hierarchy .......................................................................................................................... 34  Inheritance .................................................................................................................................. 34  Need of inheritance .................................................................................................................... 35  Try It Out ..................................................................................................................................... 35  Tips and Tricks ........................................................................................................................... 36  Summary .................................................................................................................................... 36  Test Your Understanding ............................................................................................................ 36  Session 04: Introduction to OOPS .............................................................................................. 37  Learning Objectives .................................................................................................................... 37  Polymorphism ............................................................................................................................. 37  Method Overloading ................................................................................................................... 37  Subclassing Polymorphism ........................................................................................................ 37  Relationship Between Objects .................................................................................................... 38  Page 2 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Tips and Tricks ........................................................................................................................... 39  Summary .................................................................................................................................... 39  Test Your Understanding ............................................................................................................ 39  Session 05: Introduction to Java and SDE ................................................................................ 40  Learning Objectives .................................................................................................................... 40  History of Java ............................................................................................................................ 40  Java Technology......................................................................................................................... 40  Features or Characteristics of Java ............................................................................................ 41  Applets ........................................................................................................................................ 41  Overview of Java Virtual Machine .............................................................................................. 41  Java SE or J2SE Runtime Environment ..................................................................................... 41  Tips and Tricks ........................................................................................................................... 42  Summary .................................................................................................................................... 42  Test Your Understanding ............................................................................................................ 42  Session 06: Introduction to Java and SDE ................................................................................ 43  Learning Objectives .................................................................................................................... 43  Defining a Class .......................................................................................................................... 43  Creating Objects ......................................................................................................................... 44  Packages .................................................................................................................................... 44  Import Statement ........................................................................................................................ 44  Object Class ............................................................................................................................... 45  Object Messaging ....................................................................................................................... 45  this keyword ................................................................................................................................ 46  Try It Out ..................................................................................................................................... 46  Tips and Tricks ........................................................................................................................... 47  Summary .................................................................................................................................... 47  Test Your Understanding ............................................................................................................ 47  Session 07: Introduction to Java and SDE ................................................................................ 48  Learning Objectives .................................................................................................................... 48  The main Method ........................................................................................................................ 48  The System.out.println (SOP) Method........................................................................................ 48  Code Structure in Java ............................................................................................................... 48  Compile and Run a Java Program ............................................................................................. 49  Try It Out ..................................................................................................................................... 49  Tips and Tricks ........................................................................................................................... 50  Summary .................................................................................................................................... 50  Page 3 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Test Your Understanding ............................................................................................................ 51  Session 08: Introduction to Java and SDE ................................................................................ 52  Learning Objectives .................................................................................................................... 52  Introduction to Java and SDE ..................................................................................................... 52  Tools ........................................................................................................................................... 53  Tips and Tricks ........................................................................................................................... 54  Summary .................................................................................................................................... 54  Test Your Understanding ............................................................................................................ 54  Session 09: Introduction to Java and SDE ................................................................................ 55  Learning Objectives .................................................................................................................... 55  Creating New Java Project ......................................................................................................... 55  Creating Java Package .............................................................................................................. 56  Creating Java Classes ................................................................................................................ 57  Building Java Classes ................................................................................................................ 58  Running Java Programs ............................................................................................................. 59  Tools ........................................................................................................................................... 60  Tips and Tricks ........................................................................................................................... 60  Summary .................................................................................................................................... 61  Test Your Understanding ............................................................................................................ 61  Session 14: Language Fundamentals and Operators .............................................................. 62  Learning Objectives .................................................................................................................... 62  Java Keywords ........................................................................................................................... 62  Java Literals ................................................................................................................................ 62  Java Literals: Integer .................................................................................................................. 62  Java Literals: Floating point ........................................................................................................ 63  Java Literals: Boolean ................................................................................................................ 63  Java Literals: Character .............................................................................................................. 63  Java Literals: Character .............................................................................................................. 63  Java Literals: String .................................................................................................................... 63  Primitive Data Types .................................................................................................................. 64  Primitive Data Types: Logical-boolean ....................................................................................... 64  Primitive Data Types: Textual-char ............................................................................................ 64  Primitive Data Types: Integral – byte, short, int, and, long ......................................................... 64  Integral data type have the following ranges: ............................................................................. 65  Primitive Data Types: Floating Point-float and double ............................................................... 65  Try It Out ..................................................................................................................................... 65  Page 4 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Tips and Tricks ........................................................................................................................... 66  Summary .................................................................................................................................... 67  Test Your Understanding ............................................................................................................ 67  Session 15: Language Fundamentals and Operators .............................................................. 68  Learning Objectives .................................................................................................................... 68  Variables ..................................................................................................................................... 68  Declaring and Initializing Variables............................................................................................. 68  Reference Variables Versus Primitive Variables ........................................................................ 68  Example of a reference variable and primitive variable.............................................................. 69  Example of memory handling of a reference variable and a primitive variable .......................... 69  Type Casting ............................................................................................................................... 69  Casting Primitive Types .............................................................................................................. 69  Implicit Casting ........................................................................................................................... 69  Explicit Casting ........................................................................................................................... 70  Explicit Casting Examples .......................................................................................................... 70  Casting Objects .......................................................................................................................... 70  Casting Objects Example ........................................................................................................... 70  Try It Out ..................................................................................................................................... 71  Tips and Tricks ........................................................................................................................... 72  Summary .................................................................................................................................... 72  Test Your Understanding ............................................................................................................ 73  Session 16: Language Fundamentals and Operators .............................................................. 74  Learning Objectives .................................................................................................................... 74  Declaring and Initializing Variables: Sample Program ............................................................... 74  Declaring and Initializing Variables: Coding Guidelines ............................................................. 74  Try It Out ..................................................................................................................................... 75  Tips and Tricks ........................................................................................................................... 75  Summary .................................................................................................................................... 76  Test Your Understanding ............................................................................................................ 76  Session 18: Language Fundamentals and Operators .............................................................. 77  Learning Objectives .................................................................................................................... 77  Operators .................................................................................................................................... 77  Arithmetic Operators ................................................................................................................... 77  Arithmetic Operators: Sample Program Output .......................................................................... 78  Arithmetic Operators ................................................................................................................... 78  Increment and Decrement Operators ......................................................................................... 79  Page 5 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Increment and Decrement Operators: Coding Guidelines ......................................................... 79  Relational Operators ................................................................................................................... 79  Relational Operators: Sample Program ..................................................................................... 80  Relational Operators: Sample Program Output .......................................................................... 81  Logical Operators ....................................................................................................................... 81  Logical Operators: &&(logical) and &(boolean logical) AND ...................................................... 82  Logical Operators: (logical) and (boolean logical) inclusive OR ............................................ 83  Logical Operators: (boolean logical exclusive OR) .................................................................. 84  Logical Operators: ( logical NOT) ............................................................................................. 85  Logical Operators: Conditional Operator (?:) ............................................................................. 85  The instanceof Operator ............................................................................................................. 86  Operator Precedence ................................................................................................................. 87  Operator Precedence: Coding Guidelines .................................................................................. 87  Try It Out ..................................................................................................................................... 87  Tips and Tricks ........................................................................................................................... 88  Summary .................................................................................................................................... 89  Test Your Understanding ............................................................................................................ 89  Session 21: Language Fundamentals and Operators .............................................................. 90  Learning Objectives .................................................................................................................... 90  Importance of Test ...................................................................................................................... 90  Test Driven Development ........................................................................................................... 90  Unit Testing ................................................................................................................................. 90  JUnit ............................................................................................................................................ 90  Importance of JUnit .................................................................................................................... 91  Key Goals of JUnit ...................................................................................................................... 91  Services Provided by JUnit ......................................................................................................... 91  Guidelines for Writing Code Based on JUnit Test (Minimum) .................................................... 91  Test Methods .............................................................................................................................. 91  Example 1: Very Simple Test ..................................................................................................... 92  Guidelines for Writing Code Based on JUnit Test (Sophisticated) ............................................. 92  Example 2: More Sophisticated Example ................................................................................... 92  Assert Statements ...................................................................................................................... 93  Fixtures ....................................................................................................................................... 94  Example: setUp .......................................................................................................................... 94  Test Suites .................................................................................................................................. 94  Example: Test Suites .................................................................................................................. 95  TestRunners ............................................................................................................................... 95  TestRunners: Swing ................................................................................................................... 95  Page 6 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Automating Testing (Ant) ............................................................................................................ 96  Ant Batch Mode .......................................................................................................................... 96  JUnit Class Diagram ................................................................................................................... 97  Best Practices: What Should You Test? .................................................................................... 97  Test First and What to Test ........................................................................................................ 98  Testing for Exceptions ................................................................................................................ 98  Test Then Fix .............................................................................................................................. 98  Test then Refactor ...................................................................................................................... 98  Design Patterns for Testing ........................................................................................................ 98  Design for Testing: Factories ...................................................................................................... 99  Design for Testing: Mock Objects ............................................................................................... 99  Testing with Resources (EJB or DB) .......................................................................................... 99  JUnit Extensions ......................................................................................................................... 99  JUnitReport ................................................................................................................................. 99  Cactus (from Jakarta) ...............................................................................................................100  JWebUnit ..................................................................................................................................100  XMLUnit ....................................................................................................................................100  Mock Objects ............................................................................................................................101  StrutsTestCase .........................................................................................................................101  Try It Out ...................................................................................................................................101  Tips and Tricks .........................................................................................................................105  Summary ..................................................................................................................................105  Test Your Understanding ..........................................................................................................106  Session 23: Language Fundamentals and Operators ............................................................107  Learning Objectives ..................................................................................................................107  Introduction to Arrays ...............................................................................................................107  Declaring Arrays .......................................................................................................................107  Array Instantiation .....................................................................................................................108  Sample Program .......................................................................................................................109  Accessing an Array Element ....................................................................................................109  Coding Guidelines ....................................................................................................................110  Array Length .............................................................................................................................110  Coding Guidelines ....................................................................................................................110  Multidimensional Arrays ...........................................................................................................111  Enum ........................................................................................................................................111  enum Keyword ..........................................................................................................................111  enum Type ................................................................................................................................112  Example: enum in for Loop .......................................................................................................112  Page 7 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Key Points on enum and its Constructor ..................................................................................112  Detailed Example......................................................................................................................112  Output .......................................................................................................................................113  Try It Out ...................................................................................................................................113  Tips and Tricks: ........................................................................................................................114  Summary ..................................................................................................................................114  Test Your Understanding ..........................................................................................................115  Session 26: Wrapper Classes, Selection Statements and Iteration Blocks .........................116  Learning Objectives ..................................................................................................................116  The Wrapper Classes ...............................................................................................................116  Why Should You Use Wrapper Classes? .................................................................................117  Converting Primitive Types to Objects (Wrapper) and the Reverse ........................................117  Wrappers are Immutable ..........................................................................................................118  Try It Out ...................................................................................................................................118  Tips and Tricks: ........................................................................................................................119  Summary ..................................................................................................................................119  Test Your Understanding ..........................................................................................................119  Session 27: Wrapper Classes, Selection Statements and Iteration Blocks .........................120  Learning Objectives ..................................................................................................................120  Primitive Types: Wrapper Classes ...........................................................................................120  Converting Strings to Primitive Types ......................................................................................120  Converting Primitive Types to Strings ......................................................................................121  Converting Primitive Types to Strings (Example) .....................................................................121  Autoboxing ................................................................................................................................122  Without Autoboxing (Java versions before 5.0) ........................................................................122  With Autoboxing (Java versions 5.0 or greater) .......................................................................122  Autoboxing Illustrations ............................................................................................................122  Method Arguments ...................................................................................................................123  Return Values ...........................................................................................................................123  Boolean Expressions ................................................................................................................123  Autoboxing With Switch Statement ..........................................................................................123  Operations on Numbers ...........................................................................................................123  Assignments .............................................................................................................................124  Try It Out ...................................................................................................................................124  Tips and Tricks: ........................................................................................................................125  Summary ..................................................................................................................................126  Test Your Understanding ..........................................................................................................126  Page 8 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Session 30: Wrapper Classes, Selection Statements and Iteration Blocks .........................127  Learning Objectives ..................................................................................................................127  Control Structures .....................................................................................................................127  Decision Control Structures ......................................................................................................127  if-statement ...............................................................................................................................127  if-statement Flowchart ..............................................................................................................128  Examples of if-statement ..........................................................................................................128  Coding Guidelines ....................................................................................................................128  if-else Statement .......................................................................................................................129  if-else Flowchart ........................................................................................................................129  Examples of if-else Statement ..................................................................................................129  Coding Guidelines ....................................................................................................................130  if-else-else if Statement ............................................................................................................130  if-else-else if Flowchart .............................................................................................................130  Example of if-else-else if Statement .........................................................................................131  Common Errors ........................................................................................................................131  Sample Program .......................................................................................................................132  switch Statement ......................................................................................................................132  Flowchart of switch Statement ..................................................................................................133  Coding Guidelines ....................................................................................................................134  Try It Out ...................................................................................................................................134  Tips and Tricks: ........................................................................................................................135  Summary ..................................................................................................................................135  Test Your Understanding ..........................................................................................................135  Session 31: Wrapper Classes, Selection Statements and Iteration Blocks .........................137  Learning Objectives ..................................................................................................................137  Repetition Control Structures ...................................................................................................137  while-loop ..................................................................................................................................137  Examples of while-loop .............................................................................................................137  do-while-loop ............................................................................................................................138  Examples of do-while-loop .......................................................................................................138  Coding Guidelines ....................................................................................................................139  for-loop ......................................................................................................................................139  Example of for-loop ..................................................................................................................139  Branching Statements ..............................................................................................................140  Unlabelled Break Statement .....................................................................................................140  Example of Unlabelled Break Statement ..................................................................................140  Labeled Break Statement .........................................................................................................140  Page 9 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Example of Labeled Break Statement ......................................................................................141  Unlabelled Continue Statement ................................................................................................141  Labeled Continue Statement ....................................................................................................141  Return Statement .....................................................................................................................142  Try It Out ...................................................................................................................................142  Tips and Tricks: ........................................................................................................................143  Summary ..................................................................................................................................143  Test Your Understanding ..........................................................................................................143  Session 35: Access Specifiers, Constructors and Methods ..................................................145  Learning Objectives ..................................................................................................................145  Constructors .............................................................................................................................145  Default Constructor (Method) ...................................................................................................145  Overloading Constructor Methods ............................................................................................146  Applying Constructors ..............................................................................................................146  “this()” Constructor Call ............................................................................................................146  Example of “this()” Constructor Call .........................................................................................147  “this” Reference ........................................................................................................................147  Example of “this” Reference .....................................................................................................147  Declaring Methods ....................................................................................................................148  Accessor (Getter) Methods .......................................................................................................148  Example 1: Accessor (Getter) Method .....................................................................................148  Example 2: Accessor (Getter) Method .....................................................................................148  Mutator (Setter) Methods ..........................................................................................................149  Multiple Return Statements ......................................................................................................149  Example: Multiple Return Statements ......................................................................................149  Static Methods ..........................................................................................................................150  Coding Guidelines ....................................................................................................................150  When to Define Static Method? ................................................................................................150  Source Code for StudentRecord Class ....................................................................................150  Sample Source Code that uses StudentRecord Class .............................................................151  Try It Out ...................................................................................................................................152  Tips and Tricks: ........................................................................................................................153  Summary ..................................................................................................................................153  Test Your Understanding ..........................................................................................................154  Session 38: Access Specifiers, Constructors and Methods ..................................................155  Learning Objectives ..................................................................................................................155  Method Overloading .................................................................................................................155  Page 10 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Example of Method Overloading ..............................................................................................155  Program Output ........................................................................................................................156  Try It Out ...................................................................................................................................156  Tips and Tricks: ........................................................................................................................157  Summary ..................................................................................................................................158  Test Your Understanding ..........................................................................................................158  Session 39: Access Specifiers, Constructors and Methods ..................................................159  Learning Objectives ..................................................................................................................159  Access Modifiers.......................................................................................................................159  public Accessibility ....................................................................................................................159  Example: “public” Access Modifier ...........................................................................................159  protected Accessibility ..............................................................................................................160  Example: “protected” Access Modifier ......................................................................................160  default Accessibility ..................................................................................................................160  Example: “default” Access Modifier ..........................................................................................160  private Accessibility ..................................................................................................................160  Example: “private” Access Modifier ..........................................................................................161  Java Program Structure: The Access Modifiers .......................................................................161  Coding Guidelines ....................................................................................................................161  Try It Out ...................................................................................................................................161  Tips and Tricks: ........................................................................................................................162  Summary ..................................................................................................................................163  Test Your Understanding ..........................................................................................................163  Session 42: Inheritance, Interfaces and Abstract Classes ....................................................164  Learning Objectives ..................................................................................................................164  Inheritance ................................................................................................................................164  Importance of Inheritance .........................................................................................................164  Deriving a Subclass ..................................................................................................................164  extends Keyword ......................................................................................................................165  What you can do in a Subclass ................................................................................................165  What you can do in a Sub-class Regarding Fields ...................................................................165  What you can do in a Sub-class Regarding Methods ..............................................................165  Object Class .............................................................................................................................166  Class Hierarchy ........................................................................................................................166  Superclass and Subclass .........................................................................................................166  How Constructor Method of a Super Class gets Called ...........................................................166  Example: Constructor Calling Chain.........................................................................................166  Page 11 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Example: Constructor Calling Chain.........................................................................................167  The “super” Keyword ................................................................................................................167  Overriding Methods ..................................................................................................................168  Example: Overriding Methods ..................................................................................................168  Modifiers in the Overriding Methods .........................................................................................169  Run-time Polymorphism with Overriding Methods ...................................................................169  Example: Run-time Polymorphism ...........................................................................................169  Hiding Methods .........................................................................................................................169  Example: Coding of Hiding Static Method ................................................................................169  Overriding Method Versus Hiding Method ...............................................................................170  Example: Overriding Method Versus Hiding Method During Runtime .....................................170  Hiding Fields .............................................................................................................................170  Try It Out ...................................................................................................................................170  Tips and Tricks: ........................................................................................................................172  Summary ..................................................................................................................................172  Test Your Understanding ..........................................................................................................172  Session 46: Inheritance, Interfaces and Abstract Classes ....................................................174  Learning Objectives ..................................................................................................................174  Abstract Methods ......................................................................................................................174  Abstract Class ...........................................................................................................................174  Sample Abstract Class .............................................................................................................174  Extending An Abstract Class ....................................................................................................175  When to use Abstract Methods and Abstract Class .................................................................175  What is an Interface? ................................................................................................................175  Example 1: Interface .................................................................................................................176  Example 2: OperatorCar Interface............................................................................................176  Why do you use Interfaces? Reason 1 ..................................................................................176  Why do you use Interfaces? Reason 2 ..................................................................................176  Why do you use Interfaces? Reason 3 ..................................................................................177  Interface versus Abstract Class ................................................................................................177  Interface as a Type ...................................................................................................................177  Example: Interface as a Type ...................................................................................................177  Interface versus Class: Commonality .......................................................................................177  Interface versus Class: Differences ..........................................................................................178  Defining Interface......................................................................................................................178  Implementing Interfaces ...........................................................................................................178  Implementing Class ..................................................................................................................179  Relationship of an Interface to a Class .....................................................................................179  Page 12 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Example: Implementing Multiple Interfaces ..............................................................................180  Inheritance among Interfaces ...................................................................................................180  Interface and Polymorphism .....................................................................................................180  Problem of Rewriting an Existing Interface ..............................................................................180  Solution of Rewriting an Existing Interface ...............................................................................181  When to use an Abstract Class Over Interface? ......................................................................181  Try It Out ...................................................................................................................................181  Tips and Tricks: ........................................................................................................................182  Summary ..................................................................................................................................182  Test Your Understanding ..........................................................................................................183  Session 48: Inheritance, Interfaces and Abstract Classes ....................................................184  Learning Objectives ..................................................................................................................184  Using IS-A and HAS-A .............................................................................................................184  IS-A Relationship Test ..............................................................................................................184  Try It Out ...................................................................................................................................185  Tips and Tricks: ........................................................................................................................185  Summary ..................................................................................................................................186  Test Your Understanding ..........................................................................................................186  Session 49: Inheritance, Interfaces and Abstract Classes ....................................................187  Learning Objectives ..................................................................................................................187  What is a “Type”? .....................................................................................................................187  Significance of Type .................................................................................................................187  Casting Primitive Types ............................................................................................................187  Implicit Casting .........................................................................................................................188  Implicit Casting: Example .........................................................................................................188  Explicit Casting .........................................................................................................................188  Explicit Casting: Example .........................................................................................................188  Casting Objects ........................................................................................................................188  Casting Objects: Example ........................................................................................................189  Implicit Type Casting ................................................................................................................189  Type Casting Between Objects ................................................................................................189  Explicit Type Casting ................................................................................................................190  Runtime Type Mismatch Exception ..........................................................................................190  Use instanceof Operator To Prevent Runtime Type Mismatch Error .......................................190  Try It Out ...................................................................................................................................190  Tips and Tricks: ........................................................................................................................191  Summary ..................................................................................................................................192  Page 13 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Test Your Understanding ..........................................................................................................192  Session 50: Inheritance, Interfaces and Abstract Classes ....................................................193  Learning Objectives ..................................................................................................................193  Inner Class ................................................................................................................................193  Accessing Members of Outer Class Within an Inner Class ......................................................193  Java Program Structure: Inner Classes ...................................................................................193  Static Nested Classes ..............................................................................................................194  Static Nested Class: Example ..................................................................................................194  Nested Classes versus Inner Classes ......................................................................................194  Anonymous Classes .................................................................................................................195  Anonymous Inner Class: Example ...........................................................................................195  Anonymous Inner Class: Example ...........................................................................................195  Try It Out ...................................................................................................................................195  Tips and Tricks: ........................................................................................................................196  Summary ..................................................................................................................................197  Test Your Understanding ..........................................................................................................198  Session 52: Inheritance, Interfaces and Abstract Classes ....................................................199  Learning Objectives ..................................................................................................................199  Breakpoint View ........................................................................................................................199  Console View ............................................................................................................................199  Debug View ..............................................................................................................................200  Display View .............................................................................................................................200  Expressions View .....................................................................................................................200  Registers View ..........................................................................................................................200  Tips and Tricks: ........................................................................................................................200  Summary ..................................................................................................................................201  Test Your Understanding ..........................................................................................................201  Session 54: Exception Handling ...............................................................................................202  Learning Objectives ..................................................................................................................202  Exception ..................................................................................................................................202  Exception Example ...................................................................................................................202  Example: Default Exception Handling ......................................................................................202  What Happens when an Exception Occurs? ............................................................................203  Searching the Call Stack for an Exception Handler .................................................................204  Benefits of Java Exception Handling Framework .....................................................................204  Separating Error Handling Code from Regular Code ...............................................................204  Page 14 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Traditional Programming: No Separation of Error Handling Code ...........................................205  Separating Error Handling Code from Regular Code ...............................................................205  Exception Class Hierarchy .......................................................................................................206  Catching Exceptions: The try-catch Statements ......................................................................207  Catching Exceptions: Multiple catch .........................................................................................208  Catching Exceptions: Nested trys .............................................................................................208  Catching Exceptions: Nested trys with Methods ......................................................................208  Catching Exceptions: The finally Keyword ...............................................................................209  Try It Out ...................................................................................................................................210  Tips and Tricks: ........................................................................................................................212  Summary ..................................................................................................................................212  Test Your Understanding ..........................................................................................................213  Session 55: Exception Handling ...............................................................................................214  Learning Objectives ..................................................................................................................214  Throwing Exceptions: The throw Keyword ...............................................................................214  Example: Throwing Exceptions ................................................................................................214  Propagating Errors up the Call Stack .......................................................................................214  Traditional Way of Propagating Errors .....................................................................................215  Using Java Exception Handling ................................................................................................216  Grouping and Differentiating Error Types .................................................................................216  Try It Out ...................................................................................................................................217  Tips and Tricks: ........................................................................................................................217  Summary ..................................................................................................................................218  Test Your Understanding ..........................................................................................................218  Session 58: Exception Handling ...............................................................................................219  Learning Objectives ..................................................................................................................219  Checked and Unchecked Exceptions .......................................................................................219  Creating Your Own Exception Class ........................................................................................219  How to use Your own Exceptions .............................................................................................219  Try It Out ...................................................................................................................................220  Tips and Tricks: ........................................................................................................................222  Summary ..................................................................................................................................222  Test Your Understanding ..........................................................................................................222  Session 61: Strings, String Buffer, and its Functions ............................................................223  Learning Objectives ..................................................................................................................223  The String Class .......................................................................................................................223  Page 15 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java The String Class: Constructors .................................................................................................223  The String Class: Methods .......................................................................................................224  Try It Out ...................................................................................................................................224  Tips and Tricks: ........................................................................................................................225  Summary ..................................................................................................................................226  Test Your Understanding ..........................................................................................................226  Session 62: Strings, String Buffer, and its Functions ............................................................227  Learning Objectives ..................................................................................................................227  The String Class: Example .......................................................................................................227  Try It Out ...................................................................................................................................228  Tips and Tricks: ........................................................................................................................229  Summary ..................................................................................................................................229  Test Your Understanding ..........................................................................................................230  Session 65: Strings, String Buffer, and its Functions ............................................................231  Learning Objectives ..................................................................................................................231  Java Object Law for equals() and hashCode() Methods ..........................................................231  Try It Out ...................................................................................................................................231  Tips and Tricks: ........................................................................................................................232  Summary ..................................................................................................................................233  Test Your Understanding ..........................................................................................................233  Session 66: Strings, String Buffer, and its Functions ............................................................234  Learning Objectives ..................................................................................................................234  The StringBuffer Class .............................................................................................................234  The StringBuffer Class: Methods ..............................................................................................234  The StringBuffer Class: Example .............................................................................................234  The java.lang.StringBuilder Class ............................................................................................235  Try It Out ...................................................................................................................................236  Tips and Tricks: ........................................................................................................................237  Summary ..................................................................................................................................237  Test Your Understanding ..........................................................................................................238  Session 69: Collections and Util package ...............................................................................239  Learning Objectives ..................................................................................................................239  Collection ..................................................................................................................................239  Collection Framework ...............................................................................................................239  Benefits of Collection Framework .............................................................................................239  Page 16 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Collection Interfaces .................................................................................................................240  Implementations .......................................................................................................................240  Types of Implementations ........................................................................................................240  General Purpose Implementations ...........................................................................................240  Algorithms .................................................................................................................................240  Core Collection Interfaces Hierarchy ........................................................................................241  Core Collection Interfaces ........................................................................................................241  “Collection” Interface ................................................................................................................241  “Collection” Interface (Java SE 5) .............................................................................................241  Example: Usage “Collection” Interface as a Type ....................................................................242  add() and remove() Methods of Collection Interface ................................................................242  Bulk Operations ........................................................................................................................242  Example: removeAll() ...............................................................................................................243  Array Operations.......................................................................................................................243  Example: Array Operations ......................................................................................................243  Try It Out ...................................................................................................................................243  Tips and Tricks: ........................................................................................................................244  Summary ..................................................................................................................................245  Test Your Understanding ..........................................................................................................245  Session 70: Collections and Util package ...............................................................................246  Learning Objectives ..................................................................................................................246  “Set” Interface ...........................................................................................................................246  “Set” Interface (Java SE 5) .......................................................................................................246  “equals” Operation of Set Interface ..........................................................................................247  “SortedSet” Interface ................................................................................................................247  Implementations of “Set” Interface ...........................................................................................247  HashSet ....................................................................................................................................247  Caveats of Using HashSet .......................................................................................................247  Example: Set Interface and HashSet .......................................................................................247  TreeSet .....................................................................................................................................248  Example: Set Interface and TreeSet ........................................................................................248  LinkedHashSet .........................................................................................................................249  Example: Set Interface and LinkedHashSet .............................................................................249  “List” Interface ...........................................................................................................................249  Additional Operations Supported by “List” Interface over “Collection” .....................................249  “List” Interface ...........................................................................................................................250  Implementations of “List” Interface ...........................................................................................250  “Map” Interface .........................................................................................................................250  Page 17 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java “Map” Interface (Java SE 5) .....................................................................................................251  “SortedMap” Interface ...............................................................................................................251  Implementations of “Map” Interface ..........................................................................................251  “Queue” Interface......................................................................................................................252  Implementations of Queue Interface ........................................................................................252  Implementations of Queue Interface ........................................................................................252  Abstract Classes .......................................................................................................................252  Routine Data Manipulation .......................................................................................................252  Searching ..................................................................................................................................253  Composition ..............................................................................................................................253  Try It Out ...................................................................................................................................253  Tips and Tricks: ........................................................................................................................254  Summary ..................................................................................................................................255  Test Your Understanding ..........................................................................................................255  Session 73: Collections and Util package ...............................................................................256  Learning Objectives ..................................................................................................................256  Two Schemes of Traversing Collections ..................................................................................256  Iterator Interface .......................................................................................................................256  Use Iterator Over for-each ........................................................................................................256  The Iterable Interface ...............................................................................................................257  Try It Out ...................................................................................................................................257  Tips and Tricks: ........................................................................................................................258  Summary ..................................................................................................................................259  Test Your Understanding ..........................................................................................................259  Session 75: Collections and Util package ...............................................................................260  Learning Objectives ..................................................................................................................260  The Date Class .........................................................................................................................260  The Date Class: Example .........................................................................................................260  The Properties Class ................................................................................................................260  The Properties Class: Example ................................................................................................260  Try It Out ...................................................................................................................................261  Tips and Tricks: ........................................................................................................................262  Summary ..................................................................................................................................262  Test Your Understanding ..........................................................................................................262  Session 81: Threads ...................................................................................................................263  Learning Objectives ..................................................................................................................263  Page 18 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java Threads .....................................................................................................................................263  Multi-Threading in Java Platform ..............................................................................................263  Thread Priorities .......................................................................................................................263  Try It Out ...................................................................................................................................264  Tips and Tricks : .......................................................................................................................265  Summary ..................................................................................................................................265  Test Your Understanding ..........................................................................................................266  Session 82: Threads ...................................................................................................................267  Learning Objectives ..................................................................................................................267  The Thread Class: Constructor ................................................................................................267  The Thread Class: Constants ...................................................................................................267  The Thread Class: Methods .....................................................................................................268  Two Ways of Creating and Starting a Thread ..........................................................................268  Extending Thread Class ...........................................................................................................268  Two Schemes of Starting a Thread from a Subclass ...............................................................268  Scheme 1: start() Method is not in the Constructor of Subclass ..............................................268  Scheme 2: start() Method is in a Constructor of the Subclass .................................................269  Runnable Interface ...................................................................................................................270  Two Ways of Starting a Thread for a Class that Implements Runnable ..................................270  Scheme 1: Caller Thread Creates a Thread Object and Starts it Explicitly..............................270  Scheme 2: Thread Object is Created and Started Within a Constructor ..................................271  Extending Thread versus Implementing Runnable Interface ...................................................272  ThreadGroup Class ..................................................................................................................272  Example: ThreadGroup ............................................................................................................272  Try It Out ...................................................................................................................................273  Tips and Tricks: ........................................................................................................................273  Summary ..................................................................................................................................274  Test Your Understanding ..........................................................................................................274  Session 84: Threads ...................................................................................................................275  Learning Objectives ..................................................................................................................275  Race Condition and How to Solve it .........................................................................................275  An Unsynchronized Example ...................................................................................................275  Synchronization: Locking an Object .........................................................................................276  Option 1: Use Synchronized Method ........................................................................................276  Option 1: Executing Synchronized Method ..............................................................................277  Option 2: Use Synchronized Statement on a Common Object ................................................277  Inter-thread Communication: Methods from Object Class .......................................................278  Page 19 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected Handout - Core Java wait() Method of Object Class ..................................................................................................278  notify() Method ..........................................................................................................................279  Try It Out ...................................................................................................................................279  Tips and Tricks: ........................................................................................................................280  Summary ..................................................................................................................................280  Test Your Understanding ..........................................................................................................281  Session 86: Threads ...................................................................................................................282  Learning Objectives ..................................................................................................................282  Concurrency Utilities: JSR-166 .................................................................................................282  Why Use Concurrency Utilities? ...............................................................................................282  Concurrency Utilities .................................................................................................................282  Task Scheduling Framework ....................................................................................................282  Executor Interface ....................................................................................................................283  Executor and ExecutorService .................................................................................................283  Creating ExecutorService From Executors ..............................................................................283  Code example of poor resource management (pre-J2SE 5.0 code) ........................................284  Code example for better resource management (Using Executors of Java2,v5.0) .................284  Callable's and Future's: Problem (pre-J2SE 5.0) .....................................................................285  Callables and Futures ...............................................................................................................285  Build CallableExample (This is Callee) ...................................................................................285  Future Example (Caller) ...........................................................................................................285  Semaphores .............................................................................................................................286  Semaphore Example ................................................................................................................286  BlockingQueue Interface ..........................................................................................................286  Blocking Queue Example 1 ......................................................................................................287  Blocking Queue Example 2 ......................................................................................................287  Atomics .....................................................................................................................................287  Locks ........................................................................................................................................288  ReadWriteLock .........................................................................................................................288  ReadWrite Lock Example .........................................................................................................288  Try It Out ...................................................................................................................................289  Tips and Tricks: ........................................................................................................................290  Summary ..................................................................................................................................290  Test Your Understanding ..........................................................................................................291  Session 89: I/O operations in JAVA .........................................................................................292  Learning Objectives ..................................................................................................................292  I/O Streams ...............................................................................................................................292  Page 20 ©Copyright 2007, Cognizant Technology Solutions, All Rights Reserved C3: Protected

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.