NOOTAN PADIA ASSIST. PROF. MEFGI, RAJKOT.

Similar documents
Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

Java Basic Datatypees

Java Overview An introduction to the Java Programming Language

Operators and Expressions

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CSC 1214: Object-Oriented Programming

Programming. Syntax and Semantics

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Java Primer 1: Types, Classes and Operators

6.096 Introduction to C++ January (IAP) 2009

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

CS201 - Introduction to Programming Glossary By

Introduction to Programming Using Java (98-388)

Sri Vidya College of Engineering & Technology

STRUCTURING OF PROGRAM

2 rd class Department of Programming. OOP with Java Programming

Short Notes of CS201

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

3. Java - Language Constructs I

Introduction to Programming (Java) 2/12

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

Index COPYRIGHTED MATERIAL

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

The Java language has a wide variety of modifiers, including the following:

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

B.V. Patel Institute of BMC & IT, UTU 2014

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

1 Shyam sir JAVA Notes

.Net Technologies. Components of.net Framework

CS260 Intro to Java & Android 03.Java Language Basics

Values and Variables 1 / 30

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

CS11 Java. Fall Lecture 1

Java: framework overview and in-the-small features

Language Fundamentals Summary

Special Topics: Programming Languages

Chapter 1 Introduction to Java

Programming in C++ 4. The lexical basis of C++

Question. 1 Features of Java.

Object Oriented Programming. Solved MCQs - Part 2

Operators. Java operators are classified into three categories:

Selected Java Topics

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CMPT 125: Lecture 3 Data and Expressions

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CS201 Some Important Definitions

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

SECTION II: LANGUAGE BASICS

Java An Introduction. Outline. Introduction

Compaq Interview Questions And Answers

Getting started with Java

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Programming Language Basics

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

Basics of Java Programming

Lecture 02 C FUNDAMENTALS

Computers Programming Course 5. Iulian Năstac

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

(2½ hours) Total Marks: 75

Learning the Java Language. 2.1 Object-Oriented Programming

Java Programming. Atul Prakash

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Sample Copy. Not for Distribution.

Chapter 2 - Introduction to C Programming

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

WA1278 Introduction to Java Using Eclipse

Character Stream : It provides a convenient means for handling input and output of characters.

Program Fundamentals

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

20 Most Important Java Programming Interview Questions. Powered by

Zheng-Liang Lu Java Programming 45 / 79

Programming in C and C++

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

Certified Core Java Developer VS-1036

An overview of Java, Data types and variables

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

JAVA OPERATORS GENERAL

Objects and Iterators

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Interview Questions of C++

COP 3330 Final Exam Review

Structure of this course. C and C++ Past Exam Questions. Text books

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

The Java Programming Language

Object-Oriented Programming (OOP) Fundamental Principles of OOP

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Basic Types, Variables, Literals, Constants

Lecture Set 4: More About Methods and More About Operators

Computers Programming Course 6. Iulian Năstac

A flow chart is a graphical or symbolic representation of a process.

Core Java Syllabus. Overview

Absolute C++ Walter Savitch

C: How to Program. Week /Mar/05

Preview from Notesale.co.uk Page 9 of 108

Transcription:

NOOTAN PADIA ASSIST. PROF. MEFGI, RAJKOT.

Object and Classes Data Abstraction and Encapsulation Inheritance Polymorphism Dynamic Binding Message Communication

Objects are the basic runtime entities in an object oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program may handle. Class is a collection of objects of similar type. A class may be thought of as a data type and an object as a variable of that data type. Once a class has been defined, we can create any no. of objects belonging to that class.

The wrapping up of data and methods into a single unit (called class) is known as encapsulation. Insulation of data from direct access by the program is called data hiding. Abstractionrefers to the act of representing essential features without including the background details or explanations.

It is the process by which objects of one class acquires the properties of objects of another class. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it.

It means the ability to take more than one form. It plays an important role in allowing objects having different internal structure to share the same external interface. It is extensively used in implementing inheritance.

Bindingrefers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with the given procedure call is not known until the time of the call at run time.

An OOP consists of a set of objects that communicate with each other. The process of programming in an object oriented language, involves the following basic steps : Creating classes that define objects and their behavior Creating objects from class definitions. Establishing communication among objects. Employee.salary(name);

Write a note on OOP features with appropriate e.g.

Why is java so popular? Platform Independent Provides applet and servletfor use on the web Object oriented programming language. Easy

Here, a platform refers to a combination of computer H/W and the operating system on which the code is being executed. Platform independent means we need not to compile the code for each platform. Compilation and execution for C.

Compilation and execution for Java.

Most commonly used in web based applications. Interaction between the web browser and the web server for getting HTML content. Interaction between the web browser and the web server for getting an Applet.

Interaction between web browser and web server for accessing a servlet.

All the concepts of OOP

Similar to C language. No pointer concept. No bother about de-allocation. De-allocating is taken care by the garbage collector within the JVM. Easy in terms of programming for N/W applications and multi threaded applications.

In the mid 1980s, C was one of the most popular language. By the late 1980s,object oriented concepts were well established. Originally, development of Java was started under Project Green, by a group of Sun engineers led by James Gosling called the new language Oak, after the tree outside his window. Later it was found that Oak was already trademarked by Oak Technologies.

It was then renamed as Java. At that time java was popular as a language for consumer electronics. However, java really took off as a language for development on the Web, after Sun Microsystems showcased the use of applets in a browser at Sunworld 95 on May 23,1995 (this date is also considered as a birth date of Java). The first version of java was later released in 1996.

1JDK Alpha and Beta (1995) 2JDK 1.0 (January 23, 1996) 3JDK 1.1 (February 19, 1997) 4J2SE 1.2 (December 8, 1998) 5J2SE 1.3 (May 8, 2000) 6J2SE 1.4 (February 6, 2002) 7 J2SE 5.0 (September 30, 2004) 8Java SE 6 (December 11, 2006) 9Java SE 7 (July 28, 2011)

Installing JDK (http://java.sun.com/javase/downloads/index.jsp ) Sample Application Compiling Java Files Execution of a Java Application Using Command Line Arguments

Note : 1. The name of the public class must match the java file name. the java file may contain a non public class whose name may be different from the file name. 2. A Java file may contain any no. of class definition.

2 Categories Primitive Data Types byte short int long float double char boolean Reference Data Types Arrays Classes Interfaces Enums Annotations

byte: byte data type is a 8-bit signed two's complement integer. Minimum value is -128 (-2^7) Maximum value is 127 (inclusive)(2^7-1) Default value is 0 Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int. Example : byte a = 100, byte b = -50

short: short data type is a 16-bit signed two's complement integer. Minimum value is -32,768 (-2^15) Maximum value is 32,767(inclusive) (2^15-1) Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int Default value is 0. Example : short s= 10000, short r = -20000

int: intdata type is a 32-bit signed two's complement integer. Minimum value is - 2,147,483,648.(-2^31) Maximum value is 2,147,483,647(inclusive).(2^31-1) Intis generally used as the default data type for integral values unless there is a concern about memory. The default value is 0. Example : inta = 100000, intb = -200000

long: long data type is a 64-bit signed two's complement integer. Minimum value is - 9,223,372,036,854,775,808.(-2^63) Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63-1) This type is used when a wider range than intis needed. Default value is 0L. Example : long a = 100000L, intb = -200000L

float: float data type is a single-precision 32-bit IEEE 754 floating point. float is mainly used to save memory in large arrays of floating point numbers. Default value is 0.0f. Example : float f1 = 234.5f

double: double data type is a double-precision 64-bit IEEE 754 floating point. This data type is generally used as the default data type for decimal values. generally the default choice. Default value is 0.0d. Example : double d1 = 123.4

boolean: booleandata type represents one bit of information. There are only two possible values : true and false. This data type is used for simple flags that track true/false conditions. Default value is false. Example : booleanone = true

char: char data type is a single 16-bit Unicode character. Minimum value is '\u0000' (or 0). Maximum value is '\uffff' (or 65,535 inclusive). Char data type is used to store any character. Example. char lettera ='A'

Arrays Classes Interfaces Enums Annotations

Data Type Default Value (for fields) byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char String (or any object) boolean '\u0000' null false

Escape Sequence Description \t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point. \r Insert a carriage return in the text at this point. \f Insert a formfeed in the text at this point. \' \" Insert a single quote character in the text at this point. Insert a double quote character in the text at this point. \\ Insert a backslash character in the text at this point.

In Java, the execution starts from main()method. But for compilation, main() is not required. Java's main() method syntax is quiet different from C/C++. Following is the main() method signature public static void main(string args[]) Every word in the above statement has got a meaning to the JVM.

1. public:it is a keyword and denotes that any other class (JVM) can call the main() method without any restrictions. 2. static:it is a keyword and denotes that any other class (JVM) can call the main() method without the help of an object. The Java program under execution takes some context area (execution area) in the RAM. JVM is in another context area. To call the main(), the JVM requires an object. When the JVM has not entered into the program, how it can create an object or get an object. To overcome this, allow the JVM to access the main() without object just by declaring static. Once the JVM enters, it can create hundreds of objects later.

3. void:it is a keyword and denotes that the main() method does not return a value. 4. main():it is the name of the method. 5. String args[]:the parameter is astring arrayby name args. The string array is used to access command-line arguments.

Documentation Section Suggested Package Statement Import Statement Interface Statement Optional Class Definitions Main Method Class { Main Method Definition } Essential

The Java language supports three kinds of comments: /*text*/ The compiler ignores everything from/* to*/. /**documentation*/ This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use/*and*/. The JDKjavadoctool uses doc comments when preparing automatically generated documentation. //text The compiler ignores everything from//to the end of the line.

Java does not include the c unique statement keywords like goto, sizeof and typedef. Java does not contain data types struct, union and enum. Java does not define the type modifier keyword auto, extern, register, signed and unsigned. Java does not support an explicit pointer type. Java does not have preprocessor and there fore we cannot use #define, #include and #ifdef statements.

Java does not support any mechanism for defining variable arguments to functions. Java requires that functions with no arguments must be declared with empty parenthesis and not with the void keyword as done in c. Java adds new operators such as instanceofand >>>. Java adds labeled break and continue statements. Java adds many features required for objectoriented programming.

Java does not support operator overloading. Java does not have template classes as in c++. Java does not support multiple inheritance of class and a new feature called interface accomplishes it. Java does not support global variables. Every variable and method is declared within a class and forms part of that class. Java does not use pointers. Java has replaced the destructor function with finalize () function. There is no header file in java.

double float long int short char byte

We have a relationship of super type and sub type between data types. Thus, the sub type would always be usable wherever the super type is usable. For e.g, in case of primitive data types, intis sub type of long, as intcan always be used wherever long is used. For the reference data types, the super type sub type relationships involve inheritance. The java. lang. Object class is the super type for all the reference data types.

There are a couple of special interfaces java.lang.cloneableand java.io.serializable, which are super types for all the array types. The super types for any class, are The direct super class of the class All the super types of this super class The interfaces directly implemented by the class The super types for any interface are The java.lang.object class All the interfaces directly extended by the interface All the super types of the interfaces directly extended by the interface.

The super types for an array of any primitive type are The java.lang.object class The java.lang.cloneable interface The java.io.serialiazable interface The super types for an array of any reference type referencetype The arrays of all the super types of the reference type The java.lang.object class The java.lang.cloneableinterface and java.io.serialiazable interface

Arithmetic Unary + and String concatenation Relational Logical Bitwise Increment decrement Conditional Assignment, cast and instanceof Other operators ( new, [] and.)

If a = 60; and b = 13; Now in binary format they will be as follows: a = 0011 1100 b = 0000 1101

Op era tor & Description Binary AND Operator copies a bit to the result if it exists in both operands. Example (A & B) will give 12 which is 0000 1100 Binary OR Operator copies a bit if it exists in eather operand. (A B) will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ~ Binary Ones Complement Operator is unary and has the efect of (~A ) will give -60 'flipping' bits. which is 1100 0011 << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000 >> >>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. A >> 2 will give 15 which is 1111 A >>>2 will give 15 which is 0000 1111

variable x = (expression)? value if true : value if false Following is the example: public class Test { public static void main(string args[]) { inta=10, b; b = (a == 1)? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10)? 20: 30; System.out.println( "Value of b is : " + b ); } }

() is casting operator It is used to convert the type of any expression to one of its sub type. Down casting (<sub type-name>) <expression> Up casting (<super type-name>) <expression> Downcastingin primitive types can result in loss of original value, whereas in case of upcasting there can be loss of precision. Downcastingfor the reference types can result in an error at the runtime.

E.g. Not result in any loss of value for the code int s = 75; byte b = (byte) s; Loss of original value int s= 375; byte b = (byte) s; No errors Run time error Vehicle v = new Car(); Car c = (Car) v; Vehicle v = new Scooter(); Car c = (Car) v;

This operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). instanceof operator is written as: ( Object reference variable ) instanceof (class/interface type) E.g. : String name = 'James'; booleanresult = name instanceofstring; // This will return true since name is type of String

The new operator is always used to create a new instance of some class or for allocating an array of any data type. This can result in an error at the runtime if there is not enough memory to do the allocation for the instance being created. The [] operator is used to access the elements of array. This operator requires an int expression. There can be an error at the runtime if the value of an expression used to access the element is not in the range from 0 to the length of array 1.

The. Operator is used to access any member of an instance or invoking methods on an instance. This can also result in an error at the runtime in case we try to access any member for the null value.

Condition statements if if else switch - case Loop statements for while do -while break, continue and return

Narrowing and widening conversion Boxing - unboxing

A widening conversion is the conversion of sub type to one of its super types. For numeric types, the following are the widening conversions : byte to short, int, long, float or double short to int, long, float, or double char to int, long, float or double intto long, float or double long to float or double float to double E. g. inti = 123456; float f = I;

The narrowing conversion is the conversion of a super type to one of its sub type. For numeric types, the following are the narrowing conversion : double to byte, short, char, int, long or float float to byte, short, char, int, or long long to byte, short, char, or int intto byte, short or char short to byte E.g. int a=128; byte b; b=(byte)a; Here variable b will contain 1 as the calculation of 128%127.

A mixed conversion is the conversion that involves first a widening conversion followed by a narrowing conversion. For numeric types, the following are the cases for mixed conversions : char to byte or short short to char byte to char E.g. for char to byte first we need widening conversion (from char to int) and then narrowing conversion (int to byte)

Access-specifier class Class_Name { //members of class }

Instance variables Methods Constructors Garbage Collector and The finalize method Static variable and static methods Initializer block Class initializer block

(Refer class book)

(Refer class book)

A java constructorhas the same name as the name of the class to which it belongs. Constructor s syntax does not include a return type, since constructors never return a value. Constructors may include parameters of various types. When the constructor is invoked using the new operator, the types must match those that are specified in the constructor definition. Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided. E.g. (Refer class book)

1) objects are created on heap in Java irrespective of there scope e.g. local or member variable. while its worth noting that class variables or static members are created in method area ofjava memory spaceand both heap and method area is shared between different thread. 2) Garbage collection is a mechanism provided by Java Virtual Machine to reclaim heap spacefrom objects which are eligible for Garbage collection. 3) Garbage collectionrelieves java programmer from memory managementwhich is essential part of C++ programming and gives more time to focus on business logic.

4) Garbage Collection in Javais carried by a daemon thread called Garbage Collector. 5) Before removing an object from memory Garbage collection thread invokes finalize () methodof that object and gives an opportunity to perform any sort of cleanup required. 6) You as Java programmer can not force Garbage collection in Java; it will only triggerif JVM thinks it needs a garbage collection based on Java heap size.

7) There are methods like System.gc ()and Runtime.gc() which is used to send request of Garbage collection to JVMbut it snot guaranteed that garbage collection will happen. 8) If there is no memory space for creating new object in Heap Java Virtual Machine throws OutOfMemoryError or java.lan g.outofmemoryerror heap space

protected void finalize() { //content }

static variable It is a variable which belongs to the class and not to object(instance) Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables A single copyto be shared by all instances of the class A static variable can be accessed directlyby the class nameand doesn t need any object Syntax : <class-name>.<variable-name>

static method It is a method which belongs to the class and not to the object(instance) A static method can access only static data. It can not access non-static data (instance variables) A static method can call onlyother static methodsand can not call a non-static method from it. A static method can be accessed directlyby the class nameand doesn t need any object Syntax : <class-name>.<method-name> A static method cannot refer to this or super keywords in anyway

Class Initializer block In a class definition, we can have a member block with no name. Such a block is known as initializer block. An initializerblock is never invoked by any application directly since it does not have any name. However, it is always invoked on an instance as soon as it is allocated. It is invoked just before the constructor is invoked on the instance.

Syntax : class classname { { //initializer block }.. }

Class initializer block Just like we have the constructor for initializing the instance variables, we use the class initializerblock to initialize class variables. A class initializerblock is created just like the initializer block, but it is declared to be static. We also call this block as the static block. This block is automatically executed whenever the class is loaded. The class would be loaded by the JVM only if the class is used in an application

Syntax : class classname { static { //class initializer block }.. }

Explain java features in detail. Java data types with super type and sub type relationship Write a short note on type conversion. Static variable and static methods Constructor and constructor overloading Garbage collection Initializer block and class initializer block