core java programming - kopykitab.com filecore java programming a practical approach by tushar b....

16

Upload: dinhkhuong

Post on 06-Sep-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …
Page 2: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

CORE JAVA PROGRAMMING

Page 3: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

(xiii)

Page 4: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

CORE JAVA

PROGRAMMINGA PRACTICAL APPROACH

By

TUSHAR B. KUTE

B.E. Computer

Lecturer, Sandip Institute of Technology and Research Center,

Nashik, Maharashtra

UNIVERSITY SCIENCE PRESS(An Imprint of Laxmi Publications Pvt. Ltd.)

An ISO 9001:2008 Company

BENGALURU ● CHENNAI ● COCHIN ● GUWAHATI ● HYDERABADJALANDHAR ●  KOLKATA ●  LUCKNOW ●  MUMBAI ●  RANCHI ● NEW DELHI

BOSTON (USA) ●  ACCRA (GHANA) ●  NAIROBI (KENYA)

Page 5: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

CORE JAVA PROGRAMMING

© by Laxmi Publications (P) Ltd. All rights reserved including those of translation into other languages. In accordance with the Copyright (Amendment) Act, 2012, no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise. Any such act or scanning, uploading, and or electronic sharing of any part of this book without the permission of the publisher constitutes unlawful piracy and theft of the copyright holder’s intellectual property. If you would like to use material from the book (other than for review purposes), prior written permission must be obtained from the publishers.

Printed and bound in India Typeset at Kalyani Computer Services, Delhi

First Edition : 2016ISBN 978-93-83828-29-6

Limits of Liability/Disclaimer of Warranty: The publisher and the author make no representation or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties. The advice, strategies, and activities contained herein may not be suitable for every situation. In performing activities adult supervision must be sought. Likewise, common sense and care are essential to the conduct of any and all activities, whether described in this book or otherwise. Neither the publisher nor the author shall be liable or assumes any responsibility for any injuries or damages arising here from. The fact that an organization or Website if referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers must be aware that the Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.

All trademarks, logos or any other mark such as Vibgyor, USP, Amanda, Golden Bells, Firewall Media, Mercury, Trinity, Laxmi appearing in this work are trademarks and intellectual property owned by or licensed to Laxmi Publications, its subsidiaries or affiliates. Notwithstanding this disclaimer, all other names and marks mentioned in this work are the trade names, trademarks or service marks of their respective owners.

Published in india by

UNIVERSITY SCIENCE PRESS(An Imprint of Laxmi Publications Pvt. Ltd.)

An ISO 9001:2008 Company113, GOLDEN HOUSE, DARYAGANJ, NEW DELHI - 110002, INDIA Telephone : 91-11-4353 2500, 4353 2501 Fax : 91-11-2325 2572, 4353 2528 C—9602/014/12www.laxmipublications.com [email protected] Printed at: Ajit Printing Press, Delhi

& Bengaluru 080-26 75 69 30

& Chennai 044-24 34 47 26, 24 35 95 07

& Cochin 0484-237 70 04, 405 13 03

& Guwahati 0361-254 36 69, 251 38 81

& Hyderabad 040-27 55 53 83, 27 55 53 93

& Jalandhar 0181-222 12 72

& Kolkata 033-22 27 43 84

& Lucknow 0522-220 99 16

& Mumbai 022-24 91 54 15, 24 92 78 69

& Ranchi 0651-220 44 64

Bran

ches

Page 6: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

(v)

CONTENTS

Foreword .... ix

Preface .... xi

Acknowledgement .... xiii

CHAPTER 1. INTRODUCTION TO JAVA .... 1

Objectives .... 1

Introduction .... 1

Fundamentals of Object-Oriented Programming .... 3

History of Java .... 9

Features of Java .... 11

Importance of Learning Java .... 15

The Java Language Fundamentals .... 15

Use of Bytecode .... 19

Java Virtual Machine .... 19

Java Program Structure .... 20

Java Tokens .... 22

Scope of Variables .... 32

Operators .... 33

Type Conversion .... 47

Type Casting .... 49

Symbolic Constants .... 52

Programming Style .... 53

Decision-Making and Branching .... 54

Decision-Making and Looping .... 68

‘Jump’ Statements .... 82

CHAPTER 2. CLASSES, OBJECTS AND METHODS .... 90

Introduction .... 90

Class Definition .... 91

Page 7: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

Constructor .... 99

The ‘This’ Keyword .... 102

Methods Overloading .... 104

Static Members .... 106

Argument Passing to Methods .... 108

Recursion .... 111

Nested and Inner Classes .... 114

Inheritance .... 116

The ‘Super’ Keyword .... 122

Creating Multilevel Hierarchy .... 125

Method Overriding .... 129

Abstract Classes and Methods .... 134

Application of ‘Final’ in Inheritance .... 136

The ‘Object’ Class .... 137

The Visibility Controls .... 138

CHAPTER 3. ARRAYS, STRINGS AND VECTORS .... 141

Objectives .... 141

Arrays .... 141

Anonymous Arrays .... 146

Two-dimensional Arrays .... 146

Variable Size Arrays .... 150

Arrays Class .... 150

Checking the Arrays for Equality .... 152

String Class .... 154

Stringbuffer Class .... 162

Vector .... 165

Wrapper Classes .... 169

Character .... 174

Boolean .... 176

Command Line Arguments .... 178

CHAPTER 4. PACKAGES AND INTERFACES .... 180

Objectives .... 180

Package .... 180

Naming Conventions for the Packages .... 183

Creating the Package .... 183

(vi)

Page 8: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

Finding Packages and Classpath .... 184

Access Protection .... 186

Importing/Accessing the Packages .... 189

Hiding the Classes .... 190

Static Import .... 191

Interface .... 193

Creating Multiple Inheritance .... 198

Nested Interfaces .... 200

Variables in the Interfaces .... 201

Interface Inheritance .... 202

CHAPTER 5. EXCEPTIONS AND MULTITHREADING .... 205

Objectives .... 205

Exceptions .... 205

Types of Exceptions .... 206

Uncaught Exceptions .... 207

Exception Handling Using Try and Catch .... 209

Multiple Catch Statements .... 212

Nesting of Try Statements .... 214

The Finally Block .... 218

The Throw Statement .... 220

The Throws Clause .... 221

Built-in Exception Classes .... 222

Creating Our Own Exception .... 224

Multithreading .... 226

Java’s Thread Model .... 227

Life Cycle of Thread .... 228

The Thread Class and Runnable Interface .... 230

The Main Thread .... 230

Creating Multiple Threads .... 235

Thread Priorities .... 240

Thread Synchronization .... 242

Using Wait( ) and Notify( ) .... 249

The Transient and Volatile Modifiers .... 254

CHAPTER 6. APPLETS AND GRAPHICS PROGRAMMING .... 255

Objectives .... 255

Introduction to Applet .... 255

(vii)

Page 9: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

An Applet Skeleton .... 257

Basic Applet Methods .... 259

Compiling and Executing Applet .... 261

The Applet Display Methods .... 262

Using Update( ) Method .... 265

Using Repaint( ) Method .... 267

Building Applet Onto Html File .... 267

Passing Parameters to the Applet .... 272

Methods of Class Applet .... 275

The Graphics Class .... 279

Working with Color Class .... 288

Working with Fonts .... 291

Using Control Loop on Applet .... 294

Drawing Graphs and Charts .... 295

CHAPTER 7. STREAMS OF FILE I/O .... 299

Objectives .... 299

The File Class .... 299

Streams .... 301

The Byte Streams .... 302

Filtered Byte Streams .... 310

The Randomaccessfile Class .... 315

The Character Streams .... 317

Object Streams .... 328

Object Serialization .... 330

Garbage Collection .... 332

Object Finalization .... 333

Appendix .... 335

Index .... 337

(viii)

Page 10: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

FOREWORD

Living in the age of information technology which has been acquired in almost all theaspects of day to day life, requirements of the computer programming for the expression oflogic with computers communicating across the globe via World Wide Web, the importanceof platform independent computer languages such as Java cannot be neglected. Thedevelopmental phases of today’s computer application software especially web basedapplications require Java programming language. Thus core Java forms the base of creatingvarious software which are to be run on web. Lots of literature are already available on JavaProgramming, but the text presented in ‘Core Java Programming’ by Mr. Tushar Kuteis in a lucid, attractive and user friendly manner. It meets all the requirements of abeginner who are keen to learn the Java programming language though they may not befrom computer or information technology background. Simple programming examples followedby every concept, review questions and programming exercises at the end of every chaptermake the book more readable for everyone. Also it will be a good reference for the studentsstudying in diploma, U.G. and P.G. level with this subject.

Prof. P.M. YawalkarAssistant Prof. & Head,

Deptt. of Information Technology,Sandip Institute of Technology & Research Centre,

Nashik, Maharashtra

(ix)

Page 11: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

(xiii)

Page 12: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

PREFACE

Java programming is one of the rapidly growing advanced technologies in the field ofComputer and Information Technology. Everybody has to keep pace with this latest state-of-the-art technology of programming. The objective of this book is to provide the basicknowledge of Core Java Programming, which is called the base of advanced JavaProgramming. I have tried to cover all topics needed to learn the core Java with maximumpractical implementation. Not only from the point of view of students but also from the pointof view of teachers, this book will prove to be very useful.

Chapter 1 introduces the concepts related to Object Oriented Programming (OOP) first.The comparison between procedure oriented and object oriented programming is also given,by which you will easily know what OOP is. History and features of Java are given in a briefmanner. Reading this you will come to know how Java is an essential programming languageto learn. The basic programming concepts such as identifier, variables, keywords, operators,separators, etc., are illustrated in this chapter. In order to maintain the flow of executionof program the flow controls like if-else, switch-case, for loop, while loop and do-while loopstructures are briefly illustrated with suitable practical examples.

Chapter 2 introduces actual concepts of object oriented programming, i.e., classes andobjects. Various concepts that bind OOP such as polymorphism, inheritance, methodoverloading method, overriding, dynamic method dispatch as well as use of ‘this’, ‘super’,‘final’, ‘abstract’ are given in this chapter. The access specifiers of Java are explained bydemonstrations through diagrams.

Chapter 3 first time introduces the contents from Java’s class library, i.e., String andVectors. Like other programming languages, Java also contains the concept of array includingone-dimensional and two-dimensional arrays. Java contains a special concept called wrapperclasses. This has been illustrated with almost all its useful methods with appropriate examples.

Chapter 4 focuses a small part of Java programming, i.e., packages and interfaces.Though these are small concepts to learn, yet they form a big change in the programming.Creation and practical applications of packages and interfaces are exemplified with Javaprograms. The main advantage of interface, which is to create multiple inheritance isillustrated with real life examples.

Chapter 5 contains two different parts, viz : exceptions and multithreading. Exceptionis the feature that makes Java as the robust language. The concepts of exception handlingsuch as use of try-catch-finally, throw, throws are given in deep with actual implementation.Multithreading is one of the features of Java which has made Java useful than other object

(xi)

Page 13: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

oriented programming languages. Every concept and real life as well as operating systemnotion is demonstrated in this chapter.

Chapter 6 is the introduction of Java’s graphics programming ability features. Today,more than 95% of Java applications are based on web. Applet is a tool that embeds the Javaprograms with the web. This chapter gives the brief introduction of Java applets and graphicsprogramming in front of user. Lastly, the practical implementation such as bar charts andline graphs are also demonstrated.

Chapter 7 is based on Java’s ability of stream handling. This chapter has focused itusing byte as well as character streams. All useful streams and classes are demonstratedwith examples. Garbage collection is also one of the features that made Java a robustlanguage.

Throughout the book, I emphasize need for readers to think themselves on variousfacets of core Java programming. This book is suitable for students of undergraduate,diploma and other courses related to Computer and Information Technology having coreJava as a subject.

I hope that students will find this book more informative and challenging. And ofcourse, it is important that students will apply these principles of core Java programmingfor building their career and hold highest position in the field.

— Author

(xii)

Page 14: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

ACKNOWLEDGEMENT

It’s my pleasure to take this opportunity to thank all those who helped me for thepreparation and publication of this book. The writing of this book would not have beenpossible without consulting various learned authors and practitioners in this field.

I wish to express my thanks to my colleague staff members, my students and other wellwishers. I express my gratitude to Prof. P. M. Yawalkar (Head of Department of InformationTechnology, SITRC, Nashik) and Prof. M. D. Kokate (Principal, K. K. Wagh Polytechnic,Nashik) for their encouragement and especially to my brothers Amit and Amol as well asSeema P. Patil, Anil Patil, G. S. Kshirsagar, Sandeep Walunj, Arjun Sabale, Salim Khan,Santosh Dighe for their sincere support.

—Author

(xiii)

Page 15: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

(xiii)

Page 16: CORE JAVA PROGRAMMING - kopykitab.com fileCORE JAVA PROGRAMMING A PRACTICAL APPROACH By TUSHAR B. KUTE B.E. Computer Lecturer, Sandip Institute of Technology and …

Core Java Programming

Publisher : Laxmi Publications ISBN : 9789383828296 Author : Tushar B. Kute

Type the URL : http://www.kopykitab.com/product/11795

Get this eBook

40%OFF