Sample Copy. Not for Distribution.

Similar documents
An Object Oriented Programming with C

CS201 - Introduction to Programming Glossary By

Short Notes of CS201

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Introduction to Programming Using Java (98-388)

Sample Copy. Not For Distribution.

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

C++ (Non for C Programmer) (BT307) 40 Hours

JAVA An overview for C++ programmers

Sample Copy. Not For Distribution.

Java Overview An introduction to the Java Programming Language

Compaq Interview Questions And Answers

Computer Programming C++ (wg) CCOs

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Sri Vidya College of Engineering & Technology

Core JAVA Training Syllabus FEE: RS. 8000/-

Sample Copy. Not For Distribution.

CPS 506 Comparative Programming Languages. Programming Language

September 10,

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Selected Java Topics

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

15CS45 : OBJECT ORIENTED CONCEPTS

Modern Programming Languages. Lecture Java Programming Language. An Introduction

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING


GUJARAT TECHNOLOGICAL UNIVERSITY

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Java Primer 1: Types, Classes and Operators

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Sample Copy. Not For Distribution.

QUIZ. What is wrong with this code that uses default arguments?

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Variables. Data Types.

VALLIAMMAI ENGINEERING COLLEGE

Get Unique study materials from

Vector and Free Store (Pointers and Memory Allocation)

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

Introduction to Java

ABOUT CORE JAVA COURSE SCOPE:

Seminar report Java Submitted in partial fulfillment of the requirement for the award of degree Of CSE

Sample Copy. Not For Distribution.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Business and Scientific Applications of the Java Programming Language


AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

A brief introduction to C programming for Java programmers

VALLIAMMAI ENGINEERING COLLEGE

Ch. 3: The C in C++ - Continued -

Object-Oriented Programming in C# (VS 2015)

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Core Java Syllabus. Overview

Question. 1 Features of Java.

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Java Internals. Frank Yellin Tim Lindholm JavaSoft

Principles of Programming Languages. Objective-C. Joris Kluivers

NOOTAN PADIA ASSIST. PROF. MEFGI, RAJKOT.

Computer Science 306 Study Guide

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Latest Solved MCQs

CSCI 171 Chapter Outlines

Answer1. Features of Java

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Introduce C# as Object Oriented programming language. Explain, tokens,

Extending SystemVerilog Data Types to Nets

COP 3330 Final Exam Review

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

Programming Languages Third Edition. Chapter 7 Basic Semantics

Types. What is a type?

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

WA1278 Introduction to Java Using Eclipse

20 Most Important Java Programming Interview Questions. Powered by

[Course Overview] After completing this module you are ready to: Develop Desktop applications, Networking & Multi-threaded programs in java.

Topic 9: Type Checking

Topic 9: Type Checking

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

1 Shyam sir JAVA Notes

Lecture Notes on Programming Languages

From C++ to Java. Duke CPS

Oracle 10g: Java Programming

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

CS 231 Data Structures and Algorithms, Fall 2016


CORE JAVA TRAINING COURSE CONTENT

Chapter 17 vector and Free Store. Bjarne Stroustrup

Chapter 1: Object-Oriented Programming Using C++

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Special Topics: Programming Languages

Object-Oriented Programming

Sample Copy. Not For Distribution.

Kakadu and Java. David Taubman, UNSW June 3, 2003

Transcription:

A Practical Approach to Learn JAVA i

EDUCREATION PUBLISHING RZ 94, Sector - 6, Dwarka, New Delhi - 110075 Shubham Vihar, Mangla, Bilaspur, Chhattisgarh - 495001 Website: www.educreation.in Copyright, 2018, Dr Mayank Patel All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form by any means, electronic, mechanical, magnetic, optical, chemical, manual, photocopying, recording or otherwise, without the prior written consent of its writer. ISBN: 978-1-5457-1879-7 Price: `295.00 The opinions/ contents expressed in this book are solely of the authors and do not represent the opinions/ standings/ thoughts of Educreation or the Editors.The book is released by using the services of self-publishing house. Printed in India ii

A Practical Approach to Learn JAVA By Dr Mayank Patel EDUCREATION PUBLISHING (Since 2011) www.educreation.in iii

iv

About The Author Dr. Mayank Patel is Associate Professor in the Department of Computer Science and Engineering at Geetanjali Institute of Technical Studies, Udaipur (Rajasthan). His has completed Ph.D. in the domain of Multimedia Services over Wireless LAN. His area of interest includes Programming in C, Data Structure and Algorithms, OOPS, Programming in JAVA, Web Application Development through J2EE, Web Application Development through.net Framework, Computer Networks and Wireless Networks, Principles of Programming Languages and many more. He has a profound expertise in taking the full depth training of engineering students in the programming languages. Apart from the expertise in core programming languages, he has published large number of quality research papers in prominent international journals. ***** v

About The Book The proposed book is a special practical lead to all who want to learn the Core Java Programming from basic without having the deep knowledge of theoretical concept. Book covers most of the basic concepts of core JAVA. The concepts are explained with the help of simple programs and explicated with the outputs. For easy learning more than 300 solved programs are elaborated. Book has a comprehensive coverage of complicated topics like Packages, Interfaces, Multithreading, File Handling, Collections, Applets, AWTs, Derby Database Swing and Calendar class with detailed. The book also has challenging programming questions based on real practical applications. In addition, for assisting interviews, many interview base questions along with answers are included. ***** vi

Table of Content S.No. Chapter Page 1. Java Features 1 2. Basic Programs and Static Keyword 9 3. Data Types, Literals and Operators 15 4. Flow Statements 25 5. Command Line Arguments 43 6. Class and Object 45 7. Scanner Class 51 8. Constructor 55 9. Inheritance 59 10. Final Keyword and Super keyword and Abstract Keyword 11 Array 71 12 String Class 77 13 Math Class 82 14 Exception Handling 83 15 Interface 95 16 Package 101 17 Multi-Threading 109 18 Inner Class 125 19 Garbage Collections 133 20 File Handling 137 21 Applet 147 22 AWT Components 163 23 Event Handling 179 24 Frame and Layouts 201 25 Swing 219 26 Derby Database 233 27 Java Collections 237 65 vii

28 Calendar Class 241 29 Java Challenging Questions 243 30 Java Interview Questions 249 viii

A Practical Approach to Learn JAVA 1. Java Features The basic features that make java a powerful and popular programming language are as follows: Platform Independence: The write- once- run- anywhere ideal has not been achieved (tuning for differentplatforms usually required), but closer than with other languages. Object oriented: Object oriented throughout- no coding outside of class definitions, including main ( ), An extensive class library available in the core language packages. Complier/ Interpreter Combo: Code is compiled to byte codes that are interpreted by java virtual machines (JVM) This provides portability to any machine for which a virtual machine has been written. The two steps of complication and interpretation allow for extensive code checking and improved security. Robust: Exception handing built- in, strong type checking (that is, all data must be declared an explicit type), local variables must be initialized. Several dangerous features of C & C++ eliminated: No memory Pointers No Processor Array index limit checking Automatic memory management: Automatic garbage collection- memory management handled by JVM. Security: No memory pointers Programs run inside the virtual machine sandbox. Array index limit checking 1

2 Dr Mayank Patel Code pathologies reduced byo Byte code verifier- checks classes after loading. o Class loader- confines objects to unique namespaces. Prevents loading a hacked Java lang. security manager class, for example. o Security manager- determines what resources a class can access such as reading and writing to the local disk. Dynamic binding: The linking of data and methods to where they are located is done at run time. BASIC FEATURES THAT MAKE JAVA A POWERFUL New classes can be loaded while a program is running. Linking is done on the fly. Even if libraries are recompiled, there is no need to recompile code that uses classes in those libraries. This differs from C++, which uses static binding. This can result in fragile classes for cases where linked code is changed and memory pointers then point to the wrong addresses. Good performance Interpretation of byte codes slowed performance in early versions, but advanced virtual machines with adaptive and justin- time compilation and other techniques now typically provide performance up to 50% to 100% the speed of C++ programs. Threading: Light weight processes, called threads, can easily be spun off to perform multi processing. Can take advantage of multi processors where available. Great for multimedia displays. Built in networking: Java was designed with networking in mind and comes with many classes to develop sophisticated internet communications. DIFFERNCES BETWEEN C AND JAVA:- There are numbers of important differences between C and Java, which are summarized in the following list: No preprocessor Java does not include a preprocessor and does not define any analogs of the #define, #include, and #ifdef directives. Constant definitions are replaced with static final fields in Java. (See the Java lang. math. PI field for an example). Macro definitions are not available in Java, but advanced compiler technology and in lining have made them less useful. Java does

A Practical Approach to Learn JAVA not require #include directive because Java has no header files. Java class file contains both class API & the class implementation, and the compiler reads API information from class files as necessary. Java lacks any form of conditional complication, but its cross platform portability means that this feature is very really needed. No global variables: Java defines a very clean namespace. Packages contain classes, classes contain fields and methods, and method contains local variables. But there are no global variables in Java, and thus, there is no possibility of namespace collisions among those variables. Well defined Primitive type size: All the primitive types in Java have well- defined sizes. In C, the size of short, in+, and long types is platform- dependent, which hampers portability. No pointers: Java classes and arrays are reference types, and references to object and arrays are akin to pointers in C. Unlike C pointers, however, references in Java are entirely opaque. There is no way to convert a reference to primitive type, and a reference cannot be incremented or decremented. There is no address of operator like * or ->, or size of operator. Pointers are a notorious source of bugs. Eliminating hem simplifies the language and makes Java programs more robust and secure. Garbage collection: The Java Virtual Machine performs garbage collection so that Java programmers do not have to explicitly manage the memory used by all objects and arrays. This feature eliminates memory leaks from Java programs. No go to statement: Java does not support a go to statement use of go to except in certain well- defined circumstances is regarded as poor programming practice. Java adds exception handing and labeled break and continues statements to the flow control statements offered by C. These are a good substitute for go to. Variable declarations anywhere: C requires local variable declarations to be made at the beginning of the method or lock, while Java allows then anywhere in a method or block. Many programmers prefer to keep all their variable declarations grouped together at the top of a method, however. Forward referenced: 3

Dr Mayank Patel The Java complier is smarter than the C complier, in that it allows methods to be invoked before they are defined. This eliminates the need to declare functions in a header file before defining them in a program file, as is done in C. Method overloading: Java programs can define multiple methods with the same name, as long as the methods have different parameter lists. No struct and union types: Java does not support C struct and union types. A Java class can be thought of as an enhanced struct, however. No enumerated types: Java does not support the enum keyword used in C to define types that consists of fixed sets of named values. This is surprising for a strongly typed language like Java, but there are ways to stimulate this feature with object constants. No bit fields: Java does not support the (infrequency used) ability of C to specify the number of individual bits occupied by fields of a struct. No typedef: Java does not support the typedef keyword used in C to define aliases for type names. Java s lack of pointers makes its typenaming scheme simpler and more consistent than C s. However, so many of the common uses of typedef are not really necessary in Java. No method pointers: C allows you to store the address of a function in a variable and pass this function pointer to other functions. You cannot do this with Java methods, but u can often achieve similar results by passing an object that implements a particular interface. Also a Java method can be represented and invoked through a Java lang. reflect method object. No variable length argument lists: Java doesn t allow you to define methods such as C s print f( ) that take a variable number of arguments. Method overloading allows you to stimulate C varargs functions for simple cases, but there is no general replacement for this feature SIMILARITIES AND DIFFERENCES BETWEEN C++ AND JAVA C++ Features Not Found in Java: 1. Java has no pointers 4

A Practical Approach to Learn JAVA The referencing and dereferencing of objects is handled for you automatically by Java. Java does not allow you to manipulate pointers or memory address of any kind: It does not allow you to cast object or array references into integers or vice versa It does not allow you to do pointer arithmetic It does not allow you to compare the size in bytes of any primitive type or object. There are two reasons for these restrictions: a) Pointers are a source of bugs. Eliminating them simplifies the language and eliminates many potential bugs. b) Pointers and pointer arithmetic could be used to sidestep Java s runtime checks and security mechanisms. Removing pointers allows Java to provide the security guarantees that it does. 2. Java has no global variables In Java, every field and method is declared within a class and forms part of that class. The fields and methods of a class are known as the members of a class. 3. Java does not support Multiple Inheritance C++ supports multiple inheritances of method implementations for more than one super class at a time. While it seems like a very useful feature, adding it to the language turns out to introduce many complexity by using interfaces instead. Thus, a class in Java can only inherit method implementations from a single super class, but it can inherit method declarations from any number of interfaces. In practice, this is not any particular hardship. 4. Java does not Support Templates C++ supports templates that allow you, for e.g. to implement a stack of integers and then instantiate as stack <int> or stack <double> to produce two separate types: a stack of integers and a stack of floating point values. Java has no such facility. However, the fact that every class in Java is a subclass of object means that every object can be cast to an instance of object. Thus, in Java, it is often sufficient to define a data structure (such as a stack class) that operates on object values- the objects can be cast back to their actual type whenever necessary. 5. Java does not allow operator overloading C++ allows you to define operators that perform arbitrary operations on instances of your class. In effect, it allows extending the syntax of the language. This is a nifty feature, 5

Dr Mayank Patel called operator overloading that makes for very elegant examples. In practice however, it tends to make code hard to understand. After much debate, the Java language designers decided to omit such operator overloading from the language. Note, though, that the use of the +operator for string concatenation in Java is at least reminiscent of operator overloading. 6. No processor Java does not include any kind of preprocessor like the C++ preprocessor. It may seem hard to imagine programming without # define, # include, and # ifdef, But in fact Java really does not require these constructs. SIMILARITIES BETWEEN C++ AND JAVA 1. Local object declarations A feature that Java has borrowed from C++ is the ability to declare & initialize local variables anywhere in a method body or other block of code. Declarations and their initializes no longer have to be the first statements in any block. You can declare them where it is convenient & fits well with the structure of your code. 2. Method overloading A technique that Java borrows from C++ is called method overloading. Overloaded methods are methods that have the same name, but have different signatures. In other words, they take different types of arguments, a different number of arguments, or the same type of arguments in different positions in the argument list. You cannot overload a method by changing only its return type. Two methods with the same name may have different return types, but only if the method arguments also differ, similarity, two overloaded methods may throw different exceptions, but only if their arguments differ as well. Method overloading is commonly used in Java to define a number of related functions with the same name, but different arguments. Overloaded method usually performs the same basic operation, but allow the programmer to specify arguments in different ways depending on what is convenient in a given situation. 6

A Practical Approach to Learn JAVA TABLE COMAPRING C, C++ AND JAVA Feature C C++ Java Paradigms Procedural Procedural, OOP, generic programming OOP, generic programming (from Java 5) Form of compiled source code Memory management Pointers Preprocessor string type Complex data types Executable native code Executable native code Java byte code Manual Manual Managed, using a garbage collector Yes, very commonly used Yes, character arrays Structures, unions Yes, very commonly used, but some form of references available too Yes, character arrays, objects Structures, unions, classes Inheritance N/A Multiple class inheritance Operator overloading Automatic conversion Goto statement N/A Yes No Yes, with warnings if loss could occur Yes, with warnings if loss could occur Yes Yes No No pointers references are used instead. No objects Classes Single class inheritance, multiple interface implementation Not at all, if loss could occur ; must cast explicitly 7

Get Complete Book At Educreation Store www.educreation.in