Java Tutorial

317 Pages · 2.98 mb ·

Tutorials

Table of contents

- Java Tutorial (Page 3)
- Audience (Page 3)
- Prerequisites (Page 3)
- Copyright & Disclaimer Notice (Page 3)
- Java Overview (Page 16)
- History of Java: (Page 17)
- Tools you will need: (Page 17)
- What is Next? (Page 17)
- Java Environment Setup (Page 18)
- Setting up the path for windows 2000/XP: (Page 18)
- Setting up the path for windows 95/98/ME: (Page 18)
- Setting up the path for Linux, UNIX, Solaris, FreeBSD: (Page 18)
- Popular Java Editors: (Page 19)
- What is Next? (Page 19)
- Java Basic Syntax (Page 20)
- First Java Program: (Page 20)
- Basic Syntax: (Page 21)
- Java Identifiers: (Page 21)
- Java Modifiers: (Page 22)
- Java Variables: (Page 22)
- Java Arrays: (Page 22)
- Java Enums: (Page 22)
- Example: (Page 22)
- Java Keywords: (Page 23)
- Comments in Java (Page 23)
- Using Blank Lines: (Page 23)
- Inheritance: (Page 23)
- Interfaces: (Page 24)
- What is Next? (Page 24)
- Java Object & Classes (Page 25)
- Objects in Java: (Page 25)
- Classes in Java: (Page 26)
- Constructors: (Page 26)
- Singleton Classes (Page 27)
- Implementing Singletons: (Page 27)
- Example 1: (Page 27)
- Example 2: (Page 27)
- Creating an Object: (Page 28)
- Accessing Instance Variables and Methods: (Page 28)
- Example: (Page 29)
- Source file declaration rules: (Page 29)
- Java Package: (Page 30)
- Import statements: (Page 30)
- A Simple Case Study: (Page 30)
- What is Next? (Page 32)
- Java Basic Data Types (Page 33)
- Primitive Data Types: (Page 33)
- byte: (Page 33)
- short: (Page 33)
- int: (Page 34)
- long: (Page 34)
- float: (Page 34)
- double: (Page 35)
- boolean: (Page 35)
- char: (Page 35)
- Reference Data Types: (Page 35)
- Java Literals: (Page 36)
- What is Next? (Page 37)
- Java Variable Types (Page 38)
- Local variables: (Page 38)
- Example: (Page 39)
- Example: (Page 39)
- Instance variables: (Page 39)
- Example: (Page 40)
- Class/static variables: (Page 41)
- Example: (Page 41)
- What is Next? (Page 42)
- Java Modifier Types (Page 43)
- 1. Java Access Modifiers (Page 43)
- Default Access Modifier - No keyword: (Page 43)
- Example: (Page 43)
- Private Access Modifier - private: (Page 44)
- Example: (Page 44)
- Public Access Modifier - public: (Page 44)
- Example: (Page 44)
- Protected Access Modifier - protected: (Page 44)
- Example: (Page 45)
- Access Control and Inheritance: (Page 45)
- 2. Non Access Modifiers (Page 45)
- Access Control Modifiers: (Page 46)
- Non Access Modifiers: (Page 46)
- Access Control Modifiers: (Page 46)
- Non Access Modifiers: (Page 46)
- What is Next? (Page 47)
- Java Basic Operators (Page 48)
- The Arithmetic Operators: (Page 48)
- The Relational Operators: (Page 49)
- Example (Page 50)
- The Bitwise Operators: (Page 50)
- Example (Page 51)
- The Logical Operators: (Page 52)
- Example (Page 52)
- The Assignment Operators: (Page 52)
- Example: (Page 53)
- Misc Operators (Page 54)
- Conditional Operator (?:): (Page 54)
- instanceof Operator: (Page 55)
- Precedence of Java Operators: (Page 55)
- What is Next? (Page 56)
- Java Loop Control (Page 57)
- The while Loop: (Page 57)
- Syntax: (Page 57)
- Example: (Page 57)
- The do...while Loop: (Page 58)
- Syntax: (Page 58)
- Example: (Page 58)
- The for Loop: (Page 59)
- Syntax: (Page 59)
- Example: (Page 59)
- Enhanced for loop in Java: (Page 60)
- Syntax: (Page 60)
- Example: (Page 60)
- The break Keyword: (Page 60)
- Syntax: (Page 61)
- Example: (Page 61)
- The continue Keyword: (Page 61)
- Syntax: (Page 61)
- Example: (Page 61)
- What is Next? (Page 62)
- Java Decision Making (Page 63)
- The if Statement: (Page 63)
- Syntax: (Page 63)
- Example: (Page 63)
- The if...else Statement: (Page 64)
- Syntax: (Page 64)
- Example: (Page 64)
- The if...else if...else Statement: (Page 64)
- Syntax: (Page 64)
- Example: (Page 65)
- Nested if...else Statement: (Page 65)
- Syntax: (Page 65)
- Example: (Page 65)
- The switch Statement: (Page 66)
- Syntax: (Page 66)
- Example: (Page 66)
- What is Next? (Page 67)
- Java Numbers (Page 68)
- Example: (Page 68)
- Number Methods: (Page 69)
- xxxValue() (Page 70)
- compareTo() (Page 71)
- equals() (Page 72)
- valueOf() (Page 73)
- toString() (Page 74)
- parseInt() (Page 75)
- abs() (Page 76)
- ceil() (Page 77)
- floor() (Page 78)
- rint() (Page 79)
- round() (Page 79)
- min() (Page 80)
- max() (Page 81)
- exp() (Page 82)
- log() (Page 83)
- pow() (Page 83)
- sqrt() (Page 84)
- sin() (Page 85)
- cos() (Page 86)
- tan() (Page 87)
- asin() (Page 87)
- acos() (Page 88)
- atan() (Page 89)
- atan2() (Page 90)
- toDegrees() (Page 91)
- toRadians() (Page 91)
- random() (Page 92)
- What is Next? (Page 93)
- Java Characters (Page 94)
- Example: (Page 94)
- Example: (Page 94)
- Escape Sequences: (Page 94)
- Example: (Page 95)
- Character Methods: (Page 95)
- isLetter() (Page 96)
- isDigit() (Page 97)
- isWhitespace() (Page 97)
- isUpperCase() (Page 98)
- isLowerCase() (Page 99)
- toUpperCase() (Page 100)
- toLowerCase() (Page 100)
- toString() (Page 101)
- What is Next? (Page 102)
- Java Strings (Page 103)
- Creating Strings: (Page 103)
- String Length: (Page 103)
- Concatenating Strings: (Page 104)
- Creating Format Strings: (Page 104)
- String Methods: (Page 105)
- char charAt(int index) (Page 107)
- int compareTo(Object o) (Page 108)
- int compareTo(String anotherString) (Page 109)
- int compareToIgnoreCase(String str) (Page 110)
- String concat(String str) (Page 111)
- boolean contentEquals(StringBuffer sb) (Page 112)
- static String copyValueOf(char[] data) (Page 113)
- boolean endsWith(String suffix) (Page 114)
- boolean equals(Object anObject) (Page 115)
- boolean equalsIgnoreCase(String anotherString) (Page 115)
- byte getBytes() (Page 116)
- byte[] getBytes(String charsetName) (Page 118)
- void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) (Page 119)
- int hashCode() (Page 120)
- int indexOf(int ch) (Page 121)
- int indexOf(int ch, int fromIndex) (Page 122)
- int indexOf(String str) (Page 124)
- int indexOf(String str, int fromIndex) (Page 125)
- String intern() (Page 126)
- int lastIndexOf(int ch) (Page 127)
- int lastIndexOf(int ch, int fromIndex) (Page 129)
- int lastIndexOf(String str) (Page 130)
- int lastIndexOf(String str, int fromIndex) (Page 132)
- int length() (Page 133)
- boolean matches(String regex) (Page 134)
- boolean regionMatches(boolean ignoreCase, int toffset, (Page 135)
- String other, int ooffset, int len) (Page 135)
- boolean regionMatches(int toffset, String other, int ooffset, int len) (Page 136)
- String replace(char oldChar, char newChar) (Page 138)
- String replaceAll(String regex, String replacement) (Page 138)
- String replaceFirst(String regex, String replacement) (Page 139)
- String[] split(String regex) (Page 140)
- String[] split(String regex, int limit) (Page 142)
- boolean startsWith(String prefix) (Page 143)
- boolean startsWith(String prefix, int toffset) (Page 144)
- CharSequence subSequence(int beginIndex, int endIndex) (Page 145)
- String substring(int beginIndex) (Page 146)
- String substring(int beginIndex, int endIndex) (Page 147)
- char[] toCharArray() (Page 148)
- String toLowerCase() (Page 149)
- String toLowerCase(Locale locale) (Page 150)
- String toString() (Page 151)
- String toUpperCase() (Page 151)
- String toUpperCase(Locale locale) (Page 152)
- String trim() (Page 153)
- static String valueOf(primitive data type x) (Page 154)
- Java Arrays (Page 157)
- Declaring Array Variables: (Page 157)
- Example: (Page 157)
- Creating Arrays: (Page 157)
- Example: (Page 158)
- Processing Arrays: (Page 158)
- Example: (Page 158)
- The foreach Loops: (Page 159)
- Example: (Page 159)
- Passing Arrays to Methods: (Page 159)
- Returning an Array from a Method: (Page 160)
- The Arrays Class: (Page 160)
- Java Date and Time (Page 161)
- Getting Current Date & Time (Page 162)
- Date Comparison: (Page 162)
- Date Formatting using SimpleDateFormat: (Page 162)
- Simple DateFormat format codes: (Page 163)
- Date Formatting using printf: (Page 163)
- Date and Time Conversion Characters: (Page 165)
- Parsing Strings into Dates: (Page 166)
- Sleeping for a While: (Page 166)
- Measuring Elapsed Time: (Page 167)
- GregorianCalendar Class: (Page 167)
- Example: (Page 169)
- Java Regular Expressions (Page 171)
- Capturing Groups: (Page 171)
- Example: (Page 172)
- Regular Expression Syntax: (Page 172)
- Methods of the Matcher Class: (Page 173)
- Index Methods: (Page 173)
- Study Methods: (Page 174)
- Replacement Methods: (Page 174)
- The start and end Methods: (Page 174)
- The matches and lookingAt Methods: (Page 175)
- The replaceFirst and replaceAll Methods: (Page 176)
- The appendReplacement and appendTail Methods: (Page 176)
- PatternSyntaxException Class Methods: (Page 177)
- Java Methods (Page 178)
- Creating Method: (Page 178)
- Example: (Page 179)
- Method Calling: (Page 179)
- Example: (Page 179)
- The void Keyword: (Page 180)
- Example: (Page 180)
- Passing Parameters by Value: (Page 180)
- Example: (Page 180)
- Method Overloading: (Page 181)
- Using Command-Line Arguments: (Page 182)
- Example: (Page 182)
- The Constructors: (Page 183)
- Example: (Page 183)
- Example: (Page 183)
- Variable Arguments(var-args): (Page 184)
- Example: (Page 184)
- The finalize( ) Method: (Page 185)
- Java Streams, Files and I/O (Page 186)
- Byte Streams (Page 186)
- Character Streams (Page 187)
- Standard Streams (Page 188)
- Reading and Writing Files: (Page 189)
- FileInputStream: (Page 189)
- ByteArrayInputStream (Page 190)
- Example: (Page 190)
- DataInputStream (Page 191)
- Example: (Page 192)
- FileOutputStream: (Page 193)
- ByteArrayOutputStream (Page 193)
- Example: (Page 194)
- DataOutputStream (Page 195)
- Example: (Page 195)
- Example: (Page 196)
- File Navigation and I/O: (Page 197)
- File Class (Page 197)
- Example: (Page 199)
- FileReader Class (Page 200)
- Example: (Page 201)
- FileWriter Class (Page 201)
- Example: (Page 202)
- Directories in Java: (Page 203)
- Creating Directories: (Page 203)
- Listing Directories: (Page 203)
- Java Exceptions (Page 205)
- Exception Hierarchy: (Page 205)
- Exceptions Methods: (Page 207)
- Catching Exceptions: (Page 207)
- Example: (Page 208)
- Multiple catch Blocks: (Page 208)
- Example: (Page 208)
- The throws/throw Keywords: (Page 209)
- The finally Keyword (Page 209)
- Example: (Page 210)
- Declaring you own Exception: (Page 210)
- Example: (Page 211)
- Common Exceptions: (Page 212)
- Java Inheritance (Page 214)
- IS-A Relationship: (Page 214)
- Example: (Page 215)
- Example: (Page 215)
- The instanceof Keyword: (Page 215)
- HAS-A relationship: (Page 216)
- Java Overriding (Page 217)
- Example: (Page 217)
- Rules for method overriding: (Page 218)
- Using the super keyword: (Page 219)
- Java Polymorphism (Page 220)
- Example: (Page 220)
- Virtual Methods: (Page 221)
- Java Abstraction (Page 224)
- Abstract Class: (Page 224)
- Extending Abstract Class: (Page 225)
- Abstract Methods: (Page 226)
- Java Encapsulation (Page 228)
- Example: (Page 228)
- Benefits of Encapsulation: (Page 229)
- Java Interfaces (Page 230)
- Declaring Interfaces: (Page 231)
- Example: (Page 231)
- Example: (Page 231)
- Implementing Interfaces: (Page 231)
- Extending Interfaces: (Page 232)
- Extending Multiple Interfaces: (Page 233)
- Tagging Interfaces: (Page 233)
- Java Packages (Page 234)
- Creating a package: (Page 234)
- Example: (Page 234)
- The import Keyword: (Page 235)
- Example: (Page 235)
- The Directory Structure of Packages: (Page 236)
- Set CLASSPATH System Variable: (Page 237)
- Java Data Structures (Page 239)
- The Enumeration: (Page 239)
- Example: (Page 240)
- The BitSet (Page 240)
- Example: (Page 242)
- The Vector (Page 243)
- Example: (Page 246)
- The Stack (Page 247)
- Example: (Page 247)
- The Dictionary (Page 248)
- Map Interface (Page 249)
- Example: (Page 250)
- The Hashtable (Page 251)
- Example: (Page 252)
- The Properties (Page 253)
- Example: (Page 254)
- Java Collections (Page 256)
- The Collection Interfaces: (Page 256)
- The Collection Classes: (Page 257)
- The Collection Algorithms: (Page 258)
- How to use an Iterator? (Page 259)
- Using Java Iterator (Page 259)
- The Methods Declared by Iterator: (Page 259)
- The Methods Declared by ListIterator: (Page 260)
- Example: (Page 260)
- How to use a Comparator? (Page 261)
- Using Java Comparator (Page 261)
- The compare Method: (Page 261)
- The equals Method: (Page 262)
- Example: (Page 262)
- Summary: (Page 263)
- Java Generics (Page 264)
- Generic Methods: (Page 264)
- Example: (Page 264)
- Bounded Type Parameters: (Page 265)
- Example: (Page 265)
- Generic Classes: (Page 266)
- Example: (Page 266)
- Java Serialization (Page 268)
- Serializing an Object: (Page 269)
- Deserializing an Object: (Page 269)
- Java Networking (Page 271)
- Url Processing (Page 271)
- URL Class Methods: (Page 272)
- Example: (Page 272)
- URLConnections Class Methods: (Page 273)
- Example: (Page 274)
- Socket Programming: (Page 275)
- ServerSocket Class Methods: (Page 276)
- Socket Class Methods: (Page 277)
- InetAddress Class Methods: (Page 278)
- Socket Client Example: (Page 278)
- Socket Server Example: (Page 279)
- Java Sending E-mail (Page 281)
- Send a Simple E-mail: (Page 281)
- Send an HTML E-mail: (Page 282)
- Send Attachment in E-mail: (Page 284)
- User Authentication Part: (Page 285)
- Java Multithreading (Page 286)
- Life Cycle of a Thread: (Page 286)
- Thread Priorities: (Page 287)
- Create Thread by Implementing Runnable Interface: (Page 287)
- STEP 1: (Page 287)
- STEP 2: (Page 287)
- STEP 3 (Page 287)
- Example: (Page 288)
- Create Thread by Extending Thread Class: (Page 289)
- STEP 1 (Page 289)
- STEP 2 (Page 289)
- Example: (Page 289)
- Thread Methods: (Page 290)
- Example: (Page 291)
- Major Java Multithreading Concepts: (Page 293)
- What is Thread synchronization? (Page 293)
- Multithreading example without Synchronization: (Page 293)
- Multithreading example with Synchronization: (Page 295)
- Handling threads inter communication (Page 296)
- Example: (Page 297)
- Handling threads deadlock (Page 298)
- Example: (Page 298)
- Deadlock Solution Example: (Page 299)
- Major thread operatios (Page 300)
- Example: (Page 300)
- Java Applet Basics (Page 303)
- Life Cycle of an Applet: (Page 303)
- A "Hello, World" Applet: (Page 304)
- The Applet CLASS: (Page 304)
- Invoking an Applet: (Page 305)
- Getting Applet Parameters: (Page 306)
- Specifying Applet Parameters: (Page 307)
- Application Conversion to Applets: (Page 307)
- Event Handling: (Page 308)
- Displaying Images: (Page 309)
- Playing Audio: (Page 310)
- Java Documentation (Page 312)
- What is Javadoc? (Page 312)
- The javadoc Tags: (Page 313)
- Example: (Page 314)
- Java Library Classes (Page 316)