Documente Academic
Documente Profesional
Documente Cultură
Syllabus:-
Object oriented thinking :- Need for oop paradigm, A way of viewing world – Agents,
responsibility, messages, methods, classes and instances, class hierarchies (Inheritance),
method binding, overriding and exceptions, summary of oop concepts, coping with
complexity, abstraction mechanisms.
Encapsulation
Inheritance
Re-usability
Information Hiding
Encapsulation:
The way we make a logical boundary around behaviours (methods) and data (properties)
they work on is called Encapsulation.
Inheritance:
As in real life, objects in OOP relate to each other in one way or another, the relationship
in most of the case is parent/child relationship. The child objects inherent all the
functionalities (methods) and data (properties) of their parents.
Re-usability
Along with inheritance, some other phenomena like method overloading and overriding,
provide code-reuse, which is known to be a very basic feature of object oriented
programming.
Information Hiding
When we have ways to reuse our code through one way or other, we are also in need of
some security regarding our source code. To protect it from unauthorized access/
alteration. In object oriented programming, this is called Information Hiding and is
achieved through "Access Modifiers" which lets us restrict access to all or some parts of
our objects methods and/or data
What is a Class
class
FIGURE
Ob1 Ob3
class <ClassName>
{
attributes/variables;
Constructors();
methods();
}
Instance
• Instance is an Object of a class which is an entity with its own attribute values and
methods.
Creating an Instance
ClassName refVariable;
refVariable = new Constructor();
or
ClassName refVariable = new Constructor();
Inheritance
• Inheritance allows to reuse classes by deriving a new class from an existing one
• The existing class is called the parent class, or superclass, or base class
• The derived class is called the child class or subclass.
• The child class inherits characteristics of the parent class(i.e the child class
inherits the methods and data defined for the parent class
Method Binding
There may be some occasions when we want an object to respond to the same
method but have different behaviour when that method is called.
That means, we should override the method defined in the superclass. This is
possible by defining a method in a sub class that has the same name, same
arguments and same return type as a method in the superclass.
Then when that method is called, the method defined in the sub class is invoked
and executed instead of the one in the superclass. This is known as overriding.
Exception
Summary of oops
The following are the basic oops concepts: They are as follows:
1. Objects.
2. Classes.
3. Data Abstraction.
4. Data Encapsulation.
5. Inheritance.
6. Polymorphism.
7. Dynamic Binding.
8. Message Passing
Unit-II
Syllabus
History of Java, Java buzzwords, datatypes, variables, scope and life time of variables,
arrays, operators, expressions, control statements, type conversion and costing, simple
java program, classes and objects – concepts of classes, objects, constructors, methods,
access control, this keyword, garbage collection, overloading methods and constructors,
parameter passing, recursion, string handling.
Evolution
Before Java C
Java, having been developed in 1991, is a relatively new programming language. At that
time, James Gosling from Sun Microsystems and his team began designing the first
version of Java aimed at programming home appliances which are controlled by a wide
variety of computer processors.
Java has become so successful and essentially cemented its position as an industry
standard for many reasons:
• C++ is powerful, but also dangerous. The power and popularity of C derived from
the extensive use of pointers. However, any incorrect use of pointers can cause
memory leaks, leading the program to crash.
• In a complex program, such memory leaks are often hard to detect.
• Robustness is essential. Users have come to expect that Windows may crash or
that a program running under Windows may crash. (“This program has performed
an illegal operation and will be shut down”)
• However, users do not expect toasters to crash, or washing machines to crash.
• A design for consumer electronics has to be robust.
• Replacing pointers by references, and automating memory management was the
proposed solution.
It works with almost any operating system. Since it is run through what is called a
"virtual machine" regardless of what kind of computer you are using, chances are Java
will run smoothly.
It is secure. The same Virtual Machine that makes Java almost universally compatible
also keeps it secure. Since it is run in a self-contained program within your computer, any
glitches or errors that occur don't wreak havoc outside of the system.
With these features, Java has maintained its niche in the computer programming market.
It is estimated that as of 2006 there were over four billion devices worldwide that are
capable of running Java Virtual Machine. Java is still widely used to this day, and, as of
May 2007, almost all of Java's core code is available to download for free.
• The key considerations were summed up by the Java team in the following list of
buzzwords:
Simple
Secure
Portable
Object-oriented
Robust
Multithreaded
Architecture-neutral
Interpreted
High performance
Distributed
Dynamic
Its Simple
Java was designed to be easy for the professional programmer to learn and use
effectively. If you already understand the basic concepts of OOPS, learning Java
will be even easier.
Its Secure
The key that allows Java to solve security problems just described is that the
output of a Java compiler is not executable code. Rather, it is bytecode
Its Portable
The key that allows Java to solve and the portability problems just described is
that the output of a Java compiler is not executable code. Rather, it is bytecode.
Translating a Java program into bytecode helps makes it much easier to run a
program in a wide variety of environments. This reason is straightforward: only
the JVM needs to be implemented for each platform.
Object-Oriented
Robust
The ability to create robust programs was given high priority in the design of
Java. In a well written Java program, all run-time errors can and should be
managed by your program.
Multithreaded
Architecture-neutral
Interpreted
Distributed
Dynamic
Java programs carry with them substantial amounts of run time type information
that is used to verify and resolve accesses to objects at run time.
Data Types
The data types in the Java programming language are divided into two categories and
can be explained using the following hierarchy structure :
The primitive data types are predefined data types, which always hold the value of
the same data type, and the values of a primitive data type don't share the state with
other primitive values. These data types are named by a reserved keyword in Java
programming language.
There are eight primitive data types supported by Java programming language :
byte
The byte data type is an 8-bit signed two's complement integer. It ranges from -128
to127 (inclusive). This type of data type is useful to save memory in large arrays..
We can also use byte instead of int to increase the limit of the code. The syntax of
declaring a byte type variable is shown as:
byte b = 5;
short
The short data type is a 16-bit signed two's complement integer. It ranges from
-32,768 to 32,767. short is used to save memory in large arrays. The syntax of
declaring a short type variable is shown as:
short s =
2;
int
The int data type is used to store the integer values not the fraction values. It is a 32-
bit signed two's complement integer data type. It ranges from -2,147,483,648 to
2,147,483,647 that is more enough to store large number in your program. However
for wider range of values use long. The syntax of declaring a int type variable is shown
as:
int num =
50;
long
The long data type is a 64-bit signed two's complement integer. It ranges from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with
larger range of values. The syntax of declaring a long type variable is shown as:
long ln =
746;
float
The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from
1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use
a float (instead of double) to save memory in large arrays. We do not use this data type
for the exact values such as currency. For that we have to use java.math.BigDecimal
class. The syntax of declaring a float type variable is:
float f = 105.65;
float f =
-5000.12;
double
This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from
4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).
This data type is generally the default choice for decimal values. The syntax of
declaring a double type variable is shown as:
double d =
6677.60;
char
The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to
65,535. They are not integral data type like int, short etc. i.e. the char data type can't
hold the numeric values. The syntax of declaring a char type variable is shown as:
char caps = 'c';
boolean
The boolean data type represents only two values: true and false and occupy is 1-
bit in the memory. These values are keywords in Java and represents the two boolean
states: on or off, yes or no. We use boolean data type for specifying conditional
statements as if, while, do, for. In Java, true and false are not the same as True and
False. They are defined constants of the language. The syntax of declaring a boolean
type variable is shown as:
boolean result =
true;
The ranges of these data types can be described with default values using the
following table:
Default
Data Value Size (in
Minimum Range Maximum Range
Type (for bits)
fields)
Occupy
byte 0 8 bits in -128 +127
memory
Occupy
short 0 16 bits in -32768 +32767
memory
Occupy
int 0 32 bits in -2147483648 +2147483647
memory
Occupy
long 0L 64 bits in -9223372036854775808 +9223372036854775807
memory
Occupy
32-bit
IEEE 1.40129846432481707e-
float 0.0f 3.40282346638528860e+38
754 45
floating
point
Occupy
64-bit
IEEE 4.94065645841246544e-
double 0.0d 1.79769313486231570e+308d
754 324d
floating
point
Occupy
16-bit,
char '\u0000' unsigned 0 to 65,535
Unicode
character
Occupy
boolean false 1- bit in NA NA
memory
When we declare a field it is not always essential that we initialize it too. The compiler
sets a default value to the fields which are not initialized which might be zero or null.
However this is not recommended.
Integer Data Types
So far you would have been known about these data types. Now lets take an Integer
data type in brief to better understand:
As we have told that an integer number can hold a whole number. Java provides four
different primitive integer data types that can be defined as byte, short, int, and long
that can store both positive and negative values. The ranges of these data types can be
described using the following table:
Data Size (in
Minimum Range Maximum Range
Type bits)
Occupy 8
byte bits in -128 +127
memory
Occupy
short 16 bits in -32768 +32767
memory
Occupy
int 32 bits in -2147483648 +2147483647
memory
Occupy -
long 64 bits in 922337203685477580 +9223372036854775807
memory 8
Examples of floating-point literals are:
0
1
123
-42000
Floating-point numbers
A floating-point number represents a real number that may have a fractional values i.e.
In the floating type of variable, you can assign the numbers in an in a decimal or
scientific notation. Floating-point number have only a limited number of digits, where
most values can be represented only approximately. The floating-point types are float
and double with a single-precision 32-bit IEEE 754 floating point and double-
precision 64-bit IEEE 754 floating point respectively. Examples of floating-point
literals are:
10.0003
48.9
-2000.15
7.04e12
Declaring Variables
To use any variable in a Java program, you must first declare it. Variable declarations
consist of a type and a variable name:
int myAge;
String myName;
boolean isTired;
Variable definitions can go anywhere in a method definition (that is, anywhere a regular
Java statement can go), although they are most commonly declared at the beginning of
the definition before they are used:
int count;
String title;the
boolean isAsleep;
...
You can string together variable names with the same type:
int x, y, z;
You can also give each variable an initial value when you declare it:
int a = 4, b = 5, c = 6;
If there are multiple variables on the same line with only one initializer (as in the first of
the previous examples), the initial value applies to only the last variable in a declaration.
You can also group individual variables and initializers on the same line using commas,
as with the last example, above. Local variables must be given values before they can
be used (your Java program will not compile if you try to use an unassigned local
variable). For this reason, it's a good idea always to give local variables initial values.
Instance and class variable definitions do not have this restriction (their initial value
depends on the type of the variable: null for instances of classes, 0 for numeric
variables, '\0' for characters, and false for booleans).
• Variable names in Java can start with a letter, an underscore (_), or a dollar sign
($).
• They cannot start with a number.
• After the first character, your variable names can include any letter or number.
• Symbols, such as %, *, @, and so on, are often reserved for operators in Java,
so be careful when using symbols in variable names.
• In addition, the Java language uses the Unicode character set. Unicode is a
character set definition that not only offers characters in the standard ASCII
character set, but also several million other characters for representing most
international alphabets.
Java language is case-sensitive, which means that uppercase letters are different from
lowercase letters. This means that the variable X is different from the variable x, and a
pen is not a Pen is not a PEN. Keep this in mind as you write your own Java programs
and as you read Java code other people have written.
Scope determines the visibility of program elements with respect to other program
elements.
The variable is destroyed once the scope in which it is defined is closed or ended. The
scope starts once the main block of the program start or it can be started explicitly by
starting a block using open brackets({). The scope ends once we end the block by closing
the brackets(}).
Sample program
Class Scope
{
Public static void main(String args[])
{
Int x=10;
System.out.println( “x = “+x+” y = +y);
{
Int y=20;
System.out.println( “x = “+x+” y = +y);
}
// System.out.println( “x = “+x+” y = +y);
//y cannot be accessed because y is not known;
}