This book has been developed to suit the needs of the undergraduate students of the CBCS degree course in Andhra Pradesh for the paper on Java programming. The book follows a unique approach that will help readers learn object oriented Java programming on their own. It covers Java language essentials, such as classes, objects, packages and interfaces. Detailed chapters on multithreading, how to create platform-independent GUIs using Java AWT and applets, and on accessing databases using JDBC are also included.
Dr P Radha Krishna is Principal Research Scientist at Infosys Limited, Hyderabad. He is the author of several research papers and is an expert on data structures. His research interests include data warehousing, data mining and electronic contracts.
Acknowledgements Chapter 1 Object Oriented Programming 1.1 Introduction to OOP 1.2 Objects and Classes 1.3 Characteristics of OOP 1.3.1 Encapsulation 1.3.2 Inheritance 1.3.3 Polymorphism 1.4 Difference between OOP and Procedure Oriented Programming 1.5 Summary 1.5.1 Benefi ts of OOP 1.5.2 Drawbacks of OOP 1.5.3 Challenges of OOP Objective type questions Exercises Answers Chapter 2 Introduction to Java Programming 2.1 Introduction 2.1.1 Overview of Java technology 2.1.2 A short history of the development of Java 2.1.3 Java as a new paradigm in programming 2.2 Features of Java 2.3 Comparing Java and Other Languages 2.4 Applications and Applets 2.4.1 Simple applications using Java 2.4.2 Other applications and applets 24 2.5 Java Development Kit 2.6 More Complex Programs 2.7 Java Source File Structure 2.8 Prerequisites for Compiling and Running Java Programs 2.9 Sample Programs Objective type questions Exercises Answers Chapter 3 Java Language Fundamentals 3.1 The Building Blocks of Java 3.1.1 Lexical tokens 3.1.2 Literals 3.1.3 White spaces 3.1.4 Comments 3.2 Data Types 3.2.1 Integral data types 3.2.2 Floating-point numbers 3.2.3 Boolean data types 3.3 Variable Declarations 3.3.1 Declaring, initializing and using variables 3.3.2 Variable types in Java 3.3.3 Object reference variables 3.3.4 Default values for member variables 3.3.5 Initializing local variables of different data types 3.3.6 Initializing local reference variables 3.4 Wrapper Classes 3.5 Operators and Assignment 3.5.1 Operators 3.5.2 Assignment 3.5.3 Mathematical operators 3.5.4 Relational operators 3.5.5 Logical operators 3.5.6 Bitwise operators 3.5.7 Shift operators 3.5.8 Ternary If–Else operator 3.5.9 Comma operator 3.5.10 String operator + 3.5.11 Operator precedence 3.5.12 Casting operators 3.6 Control Structures 3.6.1 Block 3.6.2 Loop 3.6.3 Branching statements 3.7 Arrays 3.7.1 One-dimensional arrays 3.7.2 Multi-dimensional arrays 3.8 Strings 3.8.1 Constructors of String class with character array and string object 3.8.2 Constructors of String class with byte array 3.8.3 Methods of the String class 3.9 The StringBuffer Class 3.9.1 Constructors of the StringBuffer class 3.10 Sample Programs Objective type questions Exercises Answers Chapter 4 Java as an OOP Language 4.1 Defi ning Classes 4.1.1 Creating instance and class variables 4.1.2 Defi ning methods 4.1.3 Knowing this 4.1.4 Variable scope and method defi nitions 4.1.5 Passing arguments to methods 4.1.6 Class methods 4.1.7 Overloading methods 4.1.8 Constructor methods 4.1.9 Inheritance, polymorphism and abstract classes 4.1.10 Overriding methods 4.1.11 Finalizer methods 4.2 Modifiers 4.2.1 Controlling access to methods and variables 4.2.2 Method protection and inheritance 4.2.3 Creating accessor methods 4.2.4 Class variables and methods 4.2.5 Finalizing classes, methods and variables 4.2.6 Abstract classes and methods 4.3 Packages 4.3.1 Using packages 4.3.2 Using package and class names in full 4.3.3 The import command 4.3.4 Name confl icts 4.3.5 Creating packages 4.3.6 Packages and class protection 4.4 Interfaces 4.4.1 Interfaces and classes 4.4.2 Creating and extending interfaces 4.4.3 Other uses of interfaces 4.5 Sample Programs Objective type questions Exercises Answers Chapter 5 Exception Handling 5.1 Introduction 5.2 Basics of Exception Handling in Java 5.3 Exception Hierarchy 5.3.1 Throwable class hierarchy 5.4 Constructors and Methods in Throwable Class 5.4.1 Constructors 5.4.2 Methods 5.5 Unchecked and Checked Exceptions 5.6 Handling Exceptions in Java 5.6.1 Try block 5.6.2 Catch block 5.6.3 Finally block 5.6.4 Multiple catch blocks 5.6.5 Nested try statements 5.6.6 The keyword throw 5.7 Exception and Inheritance 5.8 Throwing User-defi ned Exceptions 5.9 Redirecting and Rethrowing Exceptions 5.9.1 Redirecting exceptions using throws 5.9.2 Rethrowing an exception 5.10 Advantages of the Exception-Handling Mechanism 5.11 Sample Programs Objective type questions Exercises Answers Chapter 6 Multithreading 6.1 Introduction: An Overview of Threads 6.2 Creating Threads 6.2.1 Creating a new thread extending Thread 6.2.2 Creating a thread implementing Runnable interface 6.2.3 Stopping threads: the join() method 6.2.4 Naming a thread 6.3 Thread Life-cycle 6.3.1 The newborn state 6.3.2 The runnable state 6.3.3 The running state 6.3.4 The dead state 6.3.5 The blocked state 6.3.6 Manipulating threads 6.4 Thread Priorities and Thread Scheduling 6.4.1 setPriority and getPriority 6.4.2 Time-slicing 6.4.3 The scheduler 6.5 Thread Synchronization 6.5.1 Synchronized methods 6.5.2 Synchronized statements 6.5.3 Deadlocks 6.6 Daemon Threads 6.7 Thread Groups 6.8 Communication of Threads 6.9 Sample Programs Objective type questions Exercises Answers Chapter 7 Files and I/O Streams 7.1 An overview of I/O Streams 7.2 Java I/O 7.2.1 Character streams 7.2.2 Byte streams 7.2.3 Working with the I/O super-classes 7.3 File Streams 7.4 FileInputStream and FileOutputStream 7.5 Filter Streams 7.5.1 DataInputStream and DataOutputStream 7.5.2 PushbackInputStream 7.6 RandomAccessFile 7.7 Serialization 7.8 Sample Programs Objective type questions Exercises Answers Chapter 8 Applets 8.1 Introduction 8.2 Java Applications Versus Java Applets 8.3 Applet Life-cycle 8.3.1 The init() method 8.3.2 The start() method 8.3.3 The stop() method 8.3.4 The destroy() method 8.3.5 The paint() method 8.4 Working with Applets 8.4.1 Running the applet using AppletViewer 8.4.2 Running the applet using the web browser 8.5 The HTML APPLET Tag 8.5.1 Attributes in the applet tag. 8.5.2 Passing parameters to applets 8.6 The java.Applet package 8.7 Sample Programs Objective type questions Exercises Answers Chapter 9 Database Handling using JDBC 9.1 An Overview of DBMS 9.1.1 Database operations 9.1.2 Database application architectures 9.2 JDBC Architecture 9.2.1 Interfaces in JDBC API 9.2.2 Types of database driver 9.3 Working with JDBC 9.3.1 Accessing a database using JDBC 9.3.2 Registering the driver 9.3.3 Connecting to the database 9.4 Processing Queries 9.4.1 The Statement class 9.4.2 Accessing rows returned from a query 9.4.3 Accessing column data 9.4.4 The method PreparedStatement 9.4.5 The method CallableStatement 9.5 The Transactions Commit and Rollback 9.6 Handling Exceptions 9.7 Mapping Database Types to Java 9.7.1 Java format types 9.8 Accessing Metadata 9.8.1 Methods in the class ResultSetMetadata 9.8.2 The class DatabaseMetadata 9.9 Sample Programs Objective type questions Exercises Answers Chapter 10 The Abstract Window Toolkit 10.1 Introduction 10.2 Basic Classes in AWT 10.2.1 The class Graphics 10.2.2 Colours 10.2.3 Font 10.3 Drawing with Graphics Class 10.4 Class Hierarchy of AWT 10.5 Event Handling 10.5.1 Adapter classes 10.6 AWT Controls 10.6.1 Labels 10.6.2 Buttons 10.6.3 Checkbox 10.6.4 CheckboxGroup for creating radio buttons 10.6.5 Choice control 10.6.6 List control 10.6.7 TextField control 10.6.8 Scroll bars 10.7 Layout Managers 10.7.1 The FlowLayout manager 10.7.2 The GridLayout manager 10.7.3 The BorderLayout manager 10.7.4 The CardLayout manager 10.8 The Java2D API 10.9 Java2D Shapes 10.9.1 Texture paint and gradient paint 10.10 Sample Programs Objective type questions Exercises Answers Appendix: Lab Exercises and Solutions Index