You are on page 1of 46

String and StringBuffer

String
String is a class in "java.lang" built-in package. String is declared as final class String implements marker interfaces "Serializable", "Comparable", "CharSequence". The compiler makes sure that each String constant actually results in a String object. String object can be created in two ways

1. Using 'new' Operator Syntax: String s=new String("javasree"); 2. without using 'new' operator Syntax: String s="javasree";

Constructor List
String() : String(String) : String(char[]) : String(char[], int, int) : String(byte[], int, int, int) : String(byte[], int) : Constructs a new empty String. Constructs a new String that is a copy of the specified String. Constructs a new String whose initial value is the specified array of characters. Constructs a new String whose initial value is the specified sub array of characters. Constructs a new String whose initial value is the specified sub array of bytes. Constructs a new String whose initial value is the specified array of bytes.

Strings are special, in that they are the only object with an overloaded operator. When you use '+' with at least one String argument, both arguments have String conversion performed on them, and another String (not guaranteed to be unique) results. String is special-cased when doing data serialization - rather than listing the fields of this class, a String object is converted to a string literal in the object stream. String objects are immutable (ie) once string object is created it cannot be changed but we can change string reference variable. Because String objects are immutable they can be shared. Hash code of Strings will be calculated as follows: Ex: String s="abcde" Hash code of 's' is calculated = a*31^4+b*31^3+c*31^2+d*31+e When a string is concatenated, every time a new string object will be created as shown below :

Ex: String st= "javasree"; st=st + ".com"; The last statement in above example a new String object "javasree.com" will be created and assigned to reference variable 'st'. Now st point to "javasree.com" not "javasree" This always consumes lot of memory each time a new object is created. To solve this problem sun has provided concept called "String constant Pooling" or "String object Pooling"

String Constant Pool


String Constant Pool is some memory which contains String constants (or) a Pool of String Constants Ex: case (I) String s1=new String("abc"); String s2=new String("abc"); case (II) String s="abc"; String st="abc"

Uses of String Constant Pooling Mechanism Under case(I) For the objects created for string with 'new' operator, JVM won't verify pool, directly it create a new string object. String Constant pool is not eligible for Garbage Collection. Under case(II) For objects created for string without 'new' operator, first JVM will verify pool to whether the object is available in the pool or not. If available, that object reference will be assigned to reference variable. It won't create a new object. JVM will maintain String Constant Pool, Pool list until the system is turned off.

will

check

Example illustrating difference between "==" & "equals()": class St { public static void main(String as[]) { String s1=new String("abc"); String s2=new String("abc"); if(s1==s2) System.out.println("equal"); else System.out.println("not equal"); if(s1.equals(s2)) System.out.println("equal"); else System.out.println("not equal"); } } Output: not equal equal

String Buffer
Introduction : * StringBuffer is a class in "java.lang" build-in package. * StringBuffer is declared as final class * StringBuffer implements marker interfaces "Serializable", "CharSequence". * A String buffer implements a mutable sequence of characters. * String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved. * Every string buffer has a capacity. Every new String Buffer object created default 2 bytes of length will be allocated as capacity of the object. As long as the length of the character sequence contained in the StringBuffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger as follows: New capacity=old capacity * 2

Constructor List
public StringBuffer(); public StringBuffer(int length); public StringBuffer(String str);

Example using String Buffers


class Sb { public static void main(String as[]) { StringBuffer sd1=new StringBuffer(); StringBuffer sd2=new StringBuffer("sri"); System.out.println(sd1.length()); System.out.println(sd1.capacity()); System.out.println(sd2.capacity()); System.out.println(sd2.length()); } } Output : 0, 16, 19, 3.

Differences between String & StringBuffer :


String 1.String objects are immutable 2.String objects don't have default capacity StringBuffer 1.StringBuffer objects are mutable 2.Every string buffer has a default capacity

3.Implemented interfaces : Serializable, Comparable, 3.Implemnted interfaces : Serializable, Char Sequence. Char Sequence 4. String is special-cased when doing data serialization 4.String buffers are safe for use by multiple threads.

INNER CLASSES
A class that is declared and defined inside some other class is called an Inner class. Sometimes it is also called a nested class. In Java it is a feature added since jdk1.1.The inner classes give additional functionality to the program and make it clear. Let us look at the basic construction of the inner class.Like variables and methods, we can write classes also inside a class or interface. We have four types of inner classes.

Non-static Inner classes Static Inner classes Method-Local Inner classes Anonymous Inner classes

Non Static Inner Classes:


class Outer { int x; void m1() { System.out.println(x); Inner i=new Inner(); i.m2(); } class Inner { int y; void m2() { System.out.println(x); System.out.println(y); } } } class InnerClassDemo { public static void main(String arg[]) { Outer out=new Outer(); out.m1(); } }

Outer class members can be accessed inside the inner class directly. Inner class members can not be accessed inside the outer class directly but we can access by creating object of inner class. The scope of non-static inner class is the outer class where it is defined. Inner classes have access to their enclosing class's scope. ] The accessibility of the members of the enclosing class is crucial and very useful. The access to enclosing class's scope is possible because the inner class actually has a hidden reference to the outer class this reference.

Static Inner Classes:


Unlike an outer class an inner class may be private, static or abstract. Marking an Inner class static has some interesting effects with regards to accessing the fields of the enclosing class. The effect of marking it as static means there is only one instance of any variable no matter

how many instances of the outer class are created. In this situation how could the static inner class know which variables to access of its non-static outer class? The answer is that it could not know, and thus a static inner class cannot access instance variables of its enclosing class. The methods of a static inner class can of course access any static fields of its enclosing class, as their will only be one instance of any of those fields.
class Outer { static int x; void m1() { System.out.println(x); Inner i=new Inner(); i.m2(); } static class Inner { int y; void m2() { System.out.println(x); System.out.println(y); } } } class InnerClassDemo { public static void main(String arg[]) { Outer out=new Outer(); out.m1(); Outer.Inner outin=out.new Inner(); outin.m2(); } }

Method-Local Inner Classes


When you define a class inside a method that is called as method-local inner class. The scope of this class is within the method itself where it is defined. Below is a example of the method-local inner classes.

class InnerDemo { void m1() { System.out.println("Hello"); class Hello {

void m2() { System.out.println("I am in method-local inner class"); } } Hello h=new Hello(); h.m2(); } }

Anonymous Inner Classes


A class without a name defined inside a method is called an Anonymous Class. Anonymous Classes are defined in the place they are constructed. In certain situations an inner class need to be defined without name. This type of inner class declarations is called anonymous inner classes. You will see this type of inner class declaration in GUI event handling mechanisms of Java.

Declaring of Anonymous class:


Anonymous inner classes are defined by coke in a block following the use of new operator and an associated constructor. new may be used with the name of a class or the name of an interface. If an anonymous inner class is defined in a block following the application of new on class X then it defines a class that extends class X. If an anonymous inner class is defined in a block following the application of new to interface Y then it defines a class which implements interface Y.

interface Animal { void eat(); void sleep(); } class AIDemo { Animal ani=new Animal(); { void eat() { System.out.println("eating"); } void sleep() { System.out.println("sleeping");

} } }

*Important Tips on Anonymous


Following are a few important points that should be kept in mind while writing Anonymous classes: Anonymous inner class cannot have constructor. The declaration and instantiation of anonymous inner classes should be done at the same place. An anonymous class can either explicitly extend a class or explicitly implement an interface, but you cannot do both extending and implementing of an interface.

An anonymous class cannot have a constructor. Since you do not specify a name for the class, you cannot use that name to specify a constructor.

PACKAGES
Packages are java's way of grouping a variety of classes and/or interfaces together. The grouping is usually done according to functionality. In fact, packages act as "containers" for classes. By organizing our classes into packages we achieve the following benefits.

1. The classes contained in the packages of other programs can be easily reused. 2.In packages, classes can be unique compared with classes in other packages. That is, two classes in two different packages can have same name. They may be referred by their fully qualified name, comprising the package name and the class name.

3. Packages provide a way to "hide" classes thus preventing other programs or packages from accessing classes that are meant for internal use only.

4.Packages also provide a way for separating "design" from "coding". First we can design classes and decide their relationships, and then we can implement the java code needed for the methods. It is possible to change the implementation of any method without affecting the rest of the design.

5. Package is a collection of classes,which is a folder to store ".class" files.Sun has given some packages which are built in packages.

Java API Packages: Java API provides a large number of classes grouped into different package according to functionality. Most of the time we use the packages available with the java API. The below figure shows the functional breakdown of packages that are frequently used in the programs.

Frequently used API packages

Java System Packages and their Classes: Package.name Contents Java. lang Java.util Java.io Java.net Language support classes. These are classes that java compiler itself uses and therefore they are automatically imported. They include classes for primitive types, strings, math functions, threads and exceptions. Language utility classes such as vectors, hash tables, random numbers, date, etc. Input/Output support classes. They provide facilities for the input and output of data. Classes for networking. They include classes for communicating with local

computers as well as with internet servers. Java.awt Java. applet Set of classes for implementing graphical user interface. They include classes for windows, buttons, lists, menus and so on. Classes for creating and implementing applets.

Creating Packages: We must declare the name of the package using the package keyword followed by a package name.

this must be the first statement in a java source file. Then we define a classjust as we normally define a class.

Example:package firstPackage; public class FirstClass { ------------------} Here the package name is firstPackage. Naming Conventions: Packages can be named using the standard java naming rules. By convention, however, packages begin with lowercase letters. This makes it easy for users to distinguish package names from class names when looking at an explicit reference to a class. We know that all class names, again by convention, begin with an uppercase letter. For example, look at the following statement: //package declaration //class definition

Double y= java.lang.Math.sqrt(x); This statement uses a fully qualified class name Math to invoke the method

sqrt(). Note that methods begin with lowercase letters. Accessing a Package: It will be recalled that we have discussed earlier that a java system package can be accessed either using a fully qualified class name or using a shortcut approach through the import statement. We use the import statement when there are many references to a particular package or the package name is too long and unwieldy.

The same approaches can be used to access the user-defined packages as well. The import statement can be used to search a list of packages for a particular class. The general form of import statement for searching a class is a follows:

import firstpackage.secondpackage.MyClass; After defining this statement, all the members of the class My Class can be directly accessed using the class name or its objects (as the case may be) directly without using the package name: We can also use another approach as follows:

import packagename.*;

Here, package name may denote a single package or a hierarchy of packages as mentioned earlier. The star (*) indicates that compiler should search this entire package hierarchy when it encounters a class name. This implies that we can access all classes contained in the above package directly.

Using a Package: Let us now consider some simple programs that will use classes from other packages. The listing below shows a package named package1 containing a single class

Package package1; public class ClassA {

public void display() { System.out.println("Class A"); } } This source file should be named classA. Java and stored in the subdirectory package1 as stated earlier. Now compile this java file. The resultant classA.class will be stored in the same subdirectory.

Importing classes from other packages: import package1.ClassA; import package2.*; Class PackageTest2 { public static void main(String as[]) { ClassA objectA= new ClassA(); ClassB objectB= new ClassB(); objectA.displayA(); objectA.displayB(); } } Compiling the packages: Compilation of the PackageTest2 is shown below.

Javac -d . PackageTest2.java; * Here -d indicates folders will be created with the same pattern as declared in the package . * "." Indicates compiled .class file will be stored within the same drive. * We can specify the path instead of '.'

Running the package: Running of the package "Package package1.PackageTest2" shown below. * Java package1.PackageTest2;

Access specifiers (or) Visibility modifiers


We have four types of Access specifiers :

1. private 2. default 3. protected 4. public Private members can be accessed within the class only. Default members can be accessed within the class, subclass and non-subclass of within the package. Protected members can be accessed within the class, inside subclass and non-subclass of same package and subclasses of different packages. Public members can be accessed anywhere.

Order of access specifiers :

The table given below will make the concept of access modifiers more clear : Access Modifiers Accessible inside the class Accessible within the subclass inside theSame package. Accessible outside the package Accessible within the subclass outside the package default yes yes no no Private yes no no no Protected yes yes no yes Public yes yes yes yes

Method Overriding : Implementing the super class method is a subclass with the same-method signature, return type and access specifiers is called method overriding.

Rules for method overriding : Superclass method signature must be same as subclass method signature. Subclass method 'return type' must be same as superclass method return type.

When superclass method has the access specifier then subclass method must have same access specifier (or) any other which has highest privileges than super class specifier.

When super class method is throwing method level exception, in subclass the following is legal : 1. We can omit the method level exception. 2. We can throw the same exception. 3. We can throw any other exception which is subclass to superclass method exception. 4. Don't throw superclass of superclass method exception. Note : private methods can be overridden. static methods can be overridden. static methods can't be overridden as non-static methods. We can't override main method with string array as arguments.

Java Language
Java Character set : Alphabets (A---Z),(a---z) Digits (0--------9) Special Symbols (+,-,/,{,},[,],*.....etc) Identifiers : Identifiers are the name which you can form using character and character set we can use identifiers for following : Variable name Method name Class name Interface name Package name Constants Following are the rules we need to follow to form legal identifiers : a) We have to use the characters from java character set only. b) First character must be alphabets.Digits & special symbols are not allowed except "_" and "$"

c) Key words cannot be used as identifiers. d) Identifiers cannot be keywords. Keywords : Keywords are the words which have predefined meaning. You can not change the meaning of the keywords it is called as reserve words. We have 49 key words. Access Specifiers Key words : private public protected Variable, Method & Class keywords : class interface static abstract final volatile transient native strictfp instanceOf new synchronized Data types : int float char double long short byte boolean

Package : package import Reference variable key words : this super Control statement keywords :

if else for while do switch break continue default case

Exception handling key words : try catch throw throws finally assert Depricated list key words : goto const MISC : implements extends void return

DATA TYPES
We have 8 primitive data types in java : No 1. 2. 3. 4. 5. Data types byte short int long float Size in byte 1 2 4 8 4 0 0 0 0 0.0f Default value -128 to +127 -32,768 to +32,767 -2,147,483,648 to +2,147,483,647 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 1.40129846432481707e-45 to Range

3.40282346638528860e+38 6. 7. 8. double char boolean 8 2 not specified 0.0d '\u0000' false 4.94065645841246544e-324d to 1.79769313486231570e+308d '\u0000' (or 0) to '\uffff' (or 65,535) not require

Note : byte, short, int, long, float & double are number types in Java. All the number's are signed i.e we can store both +ve & -ve values . For example, in integer 1 bit is signed bit & remaining 31 bits are data bits. Variables : Variable is a name which is given to memory location. (or) Variable is name which is used to access the data in the program. Declaration of variables : Data type var1, var2, var3......; Ex: int a,b,sum; float f,g; char c,d; Before using the variable in the program, we should declare that first other wise it will give syntax error. The statement which you are using to declare the variable is called declaration statement. Initialization variables at declaration time: Data type var1=value1, var2=value2.......; Ex: int x= 10, y=20; Simple program : Class Hello { public static void main(string as[]) { System.out.println("welcome to Java@javasree.com"); } } Compiling java program:javac Hello.java Run java program:java Hello Class Vardemo {

static int I; static short s; static byte b; static long l; static float f; static double d; static char ch; static boolean b; public static void main(String args[]) { int a=99; System.out.println(a); System.out.println(s); System.out.println(i); System.out.println(b); System.out.println(l); System.out.println(f); System.out.println(d); System.out.println(ch); System.out.println(b); }} Syntax : find data type <identifier>=value; Ex: final int x=20; Constants are called final variables whose values cannot be changed. Literals : Literal is nothing but a value .we have 4 types of literals : 1.Integer literal. 2.floating point literal. 3.character literal. 4.String literal. 1.Integer literal: Integer literal is nothing but collection of digits without a decimal point .We have 3 types of integer literal in JAVA : a)decimal Integer literal(o....9). b)octal Integer literal(0...7). c)Hexa decimal Integer literal(0...9, A...F, a....f). 2.Floating point literal :It is collection of digits with decimal points(or) exponential notation Ex:12.56, 123e-7 3.character literal: single character which is enclosed between single quotation is called character literal. Ex: 'A','a','+' Character literals uses ASCII character set to store the values internally. i.e. every character which is enclosed between single quotation marks will be same as ASCII value. 4.String literal : one or more character enclosed between double quotation marks is called a String literal. Ex:"srinivas"; "ab+12999"; Operators

1.Arithmetic Operator(+,-,*,/,%) 2.Assignment Operator(=,+=,-=,*=,/=,%=) 3.Relational Operator (<,<=,>,>=,==,!=) 4.logical operator(--,++) 5.ternary operator(?:) 6.Bitwise Operator(>>,<<,&,|,!,^) 7.instanceOf operator 8.new operator Logical Operators : logical operators are used to combine two (or) more relational expressions. The result of relational expression is Boolean value and logical Expression also. The result of logical operator will be determined using following both tables. Unary operators :
Ex: Postfix: a++ a-prefix : --a ++a int x=16; int a1=x++; int a2=x++; int a3=++x; int a4=--x; int a5=x--;

Ternary operator : It is used to do some simple conditions. Syntax: var = (condition)? exp1:exp2; First condition will be evaluated ,if the condition is true value of exp1 will be assigned to var else exp2 value. Ex: max = (a>b)?a:b; Bitwise Operators : << left shift >> right shift & bit wise logical and | bit wise logical or ^ bitwise logical xor ~ bit wise logical not (negation) Bit wise operators operator on bits of given values.

Java Language
JAVA's encoding technique is 1's compliment. Left shift(<<) : Tthis operator shifts the bits of a given value to left with specified number of times Syntax : 16<<2 Right shift(>>) : This operator shifts the bits of a given value to right a specified number of

times. Syntax : 16>>2


class BitWise{ public static void main(String as[]){ int a=16; System.out.println(a>>2); System.out.println(a<<2); System.out.println(a&2); System.out.println(a|2); System.out.println(a^2); } }

instanceOf operator : This operator is used to check whether the given object is instanceOf given class or not. new : This operator is used to allocate the memory for objects(i.e allocate the memory for instance variables in the class). Control Statements :
1.if-else 2.switch 3.while 4.do-while 5.for 6.continue 7.break

If-else :
Syntax : If(condition) { Statement 1; Statement 2; ............ Statement n; } else { Statement 1; Statement 2; ............ Statement n; }

if Statement is used to select one particular block between two blocks based on the condition. first condition will be evaluated .if the condition is true. True block (if block) will be executed otherwise false block will be executed.
Ex : Write a program to find whether the given number is odd or even? class OddEven { public static void main(String as[]) { int a=Integer.parseInt(as[0]);

if(a%2==0) { System.out.println("Even number"); } else { System.out.println("Odd number"); } } }

Switch :
Syntax: Switch(expression) { case val1:statement1;break; case val2:statement2;break; ............ ............ default:statement1; }

Switch statement is used to select one set of Statement's among multiple sets.First the expression will be evaluated and that value will be compared against different values provides in different cases. If any case value matches with that value the statement which belongs to that case will be executed and control will be transferred to next statement of switch using break. When no case value found default case will be executed. All cases are executed first and then goto default .we can Place default any where.

Ex: Write a program to accept the number .if a number is between 1 and 7 display the corresponding day other wise display invalid number?
class switch1 { public static void main(String as[]) { int n=Integer.parseInt(as[0]); switch(n) { case 1: System.out.println("Monday");break; case 2: System.out.println("tueday");break; ............. ............. case 7: System.out.println("sunday");break; default: System.out.println("invalid number"); } }

while : while statement is used to execute set of statement for specified number of times.First we need to initialize the variable. when while statement encountered, condition will be evaluated.If condition is true statement inside the loop will be executed.if condition is false control comes out of the loop.

do-while :
do { Increment (or) decrement; .......... .......... }while(condition);

for statement :
for(initialization;condition;increment/decrement) { .......... .......... }

First initialization will be executed and then condition will be evaluated .If the condition is true the statement inside the loop will be executed,after that increament/decrement will happen and then condition will be evaluated. If condition is true above thing will happen else control comes out of the loop.

Ex: Write a program to print the even number's from 1 to 100? class Demo { public static void main(String as[]) { for(int i=1;i<=100;i++) { If(i%2==0) System.out.println(i); } } }

Break statement : It is unconditional control statement which is used to transfer the control to end of the break (or)loop.
while(condition)

{ ........ ........ Break; } while(condition) { ....... ....... continue; }

Continue statement : It is also unconditional control statement which is used to transfer the control to begining of the block(or)loop. Arrays : Collection of similar type is called as an Array. Arrays are object in JAVA.
Syntax: Data type array name[]=new data type[size]; (or) Data type Array name []; Array name =new Data type[sze]; Ex: int a[]; a=new int[9]; The default value of object in java is null. new is used to construct/allocate object,memory. 2-D arrays: 2-D array is also called as Array of Arrays. Ex : int x[][]=new int [3][3]; It will allocate reference variable 'x' which null. Int a[][]=new int [3][]; A[0]=new int[2]; A[1]=new int [3]; A[2]=new int[4];

When you are using index which is not available JVM throws.ArrayIndexOutOfBoundException
class ArrayDemo { public static void main(String as[]) { int x[]=new int[3]; int a[]=new int[][99,88]; int b[]=[10,20]; System.out.println(b.length); Int z[][]=new int [][]{10,20,30}; Int d[][]=new int[4][]; for(int i=0;i<d.length;i++) { System.out.println(d[i]); d[0]={10,20}; d[2]=new int[2];

for(int i=0;i<d.length;i++) { System.out.println(d[i]); } //d[6]=new int [3]; } }}

Type casting : converting one data type to another data type is called type casting. There are 2 types : 1.widening (or) implicit casting. 2.narrowing (or) explicit casting.
Ex : class Castdemo { public static void main(String as[]) { byte b=10; short s=20; int i=30; //float f=88.9; float f1=88.9f; double d=99.9; long l=40; boolean bo=false; char ch='a'; b=i;//possible lose of precision ,narrowing error. B=(byte)I; i=s;//widening i=bo;//incompatable types i=(int)bo;//in compatable type l=ch; byte b1=10; byte b2=b1*2; byte b2=(byte)b1*2; } }

Garbage Collection
With respect to the internals of Java Virtual Machine (JVM) it is known that heap stores all the objects created while execution of a java program. Objects are created by using new, new array etc. instructions but these are never freed explicitly by the code.

Garbage collection is the mechanism or process that automatically frees the memory occupied by the objects that are no longer referenced by the program.

Garbage collector (gc) is a thread, which is running behind the scenes, which de-allocates memory of unused objects in the heap.

JVM watches your heap memory when you are running out of memory it invokes gc which deallocates memory of unused objects in the heap.

Garbage collector will be invoked by the JVM. As a programmer if you want to call the gc, it can be done by following code: System.gc(); Where system is a class in java.lang.package and gc() is a static method in System class.

We cannot force the garbage collector i.e. when we call System.gc( ), JVM may or may not invoke garbage collector.

JVM identifies unused objects with the following criteria: 1. Void x () { Hello e1=new Hello () e1.m1 (); e1.m2 (); ----------------------------}

When a reference variable reaches out of scope. Then object, which is referred by the particular reference variable is unused and is eligible for garbage collection.

2.

void x () { Hello c1 = new Hello(); Hello c2 = new Hello(); c1=c2; c1.m1();

c2.m2(); } When we assign one reference variable to another reference variable with equals operator, then object which is referred by a reference variable left side to equals operator is unused and is eligible for garbage collector.

3.

void x () { Hello c1= new Hello(); c1.display(); c1= null; }

When we assign a null to a reference variable is unused and eligible for garbage collection. When JVM invokes garbage collector, gc will try to reclaim the memory of unused objects. Sometimes some unused objects may refuse to reclaim the memory, because of those unused objects still holds some resources. So first we have to release the resource then only we can reclaim the memory from those objects. For this what JVM will do is:

1. It calls System.runFinalization () method before invoking the garbage collector. 2. System.runFinalization () it invokes the overridden finalize methods (programmers overridden finalize method) of existing object in the heap. For this as a programmer you have to implement the finalize method of the superclass.

Garbage Collection Advantages :


It helps ensure program integrity. The programmer is freed from deallocating memory in the programs. Garbage collection is an important part of java's security strategy. Java programmers are unable to accidentally or purposely crash the JVM by incorrectly freeing memory.

Garbage Collection Disadvantages :


It adds an overhead that can affect program performance.

The java Virtual machine has to keep track of which objects are being referenced by the executing program, and finalize and free unreferenced objects on the fly. This activity will likely required more CPU time than would have been required if the program explicitly freed unnecessary memory.

Wrapper classes
Introduction :
There may be many situations where you need to use the primitive datatypes as objects or viceversa. Java uses simple datatypes such as int, float,etc. These types are not part of object hierarchy. You may encounter situations where you will need to create object representation of these simpletypes.

For example, if you want to store primitive datatypes along with other objects in an array.Inx collection framework we have a lot of collectionclasses like ArrayList,Vector,Hashtable,HashMap,etc,. To address this need, java provides classess that corresponds to each of the simple datatypes.These classes are called Wrapper classes.

Java provides "Wrapper" to manipulate primitive data elements as objects.Each primitive datatype has a corresponding wrapper class.Theseclasses are as follows:

Primitive datatype Wrapper class boolean byte char short int long float double Boolean Byte Char Short Integer Long Float Double

How to convert primitive to wrapper object?

There are two ways to do the above conversion: 1. By passing the primitive as parameter to the corresponding wrapper class constructor. 2. a. First convert the primitive to string object. b. Now convert the above string object to corresponding wrapper object.
Example 1:Conversion of int a=10 (primitive) to wrapper object 1.Integer obj=new Integer(a); 2.String str =Integer.toString(a); Integer obj=Integer.valueOf(str);

How to convert wrapper object to primitive?


There are two ways to do the above conversion:1) By calling the xxxValue () method. 2. a. First convert the wrapper object to string object. b. Now convert the above string object to corresponding primitive.
Example 1:Conversion of Integer object obj (in example1) to primitive 1) int a= obj.intValue(); 2) String str =obj.toString(); int a = Integer.parseInt(str);

Some Conversions:
How to convert String to primitive? Use parsexxx() method. How to convert primitive to String? Use toString(xxx); How to convert wrapper object to String? Use toString(); How to convert String to wrapper object? Use valueOf(); How to convert Not possible. boolean to other datatypes?

Note: In the above conversions "xxx" may be int, short, byte, float, double, long.

Overloading

Method Overloading : Writing more than one method with the same name by changing the parameters is called Method Overloading.

Rules : 1. Method name must be same. 2. We must change the parameters as follows. Number of parameters. Order of parameters. Type of parameters. 3. Return type may be anything. Constructors : 1. Constructors are "special methods" whose name is same as class name. 2. Constructors doesn't have any return type even void also. 3. Constructors will be invoked by the JVM when you are creating object. 4. Constructors can be overloaded. 5. Constructors is used to initialize objects.
Example : class Hello { int a,b; Hello() { System.out.println("Iam a constructor"); a=88; b=99; } Void m1() { System.out.println ("Iam a method m1"); System.out.println ("a value is"+a); System.out.println ("b value is"+b); } } class Ex1 { public static void main(String as[]) { Hello h= new Hello(); h.m1(); } } Output : Iam a constructor Iam a method m1 a value is 88 b value is99

Desc : Hello h=new Hello(); Memory will be allocated for a reference variable (h).

Memory will be allocated for instance variables of the class.

Execution of static blocks. Default constructor (constructor without arguments) will be invoked.

Object address will be assigned into reference variable 'h'.

Constructor Overloading : We can do the following by overloading the constructors : 1. I want to initialize different objects of a class with different set of values. 2. I want to provide different values dynamically when I'm creating the object. Note : JVM inserts default constructor when there is no other constructor. JVM doesn't insert default constructor when the class has overloaded constructors. this operator : This is a reference variable which contains object of current class. When you have same name for the local variables and instance variables, local variables hides the instance variables.This is called as shadowing. To refer the instance variable qualify the variable with "this". i.e. "this.a".

Different Types of Variables


Any variable which is declared by class is called reference variable. Default value of any reference variable is null. JVM allocates the memory for all variables with this new operator.

Difference between local variables and instance variables


Local variables 1. Variables declared inside the method are called as local variables. Instance variables 1. Variables declared inside the class and outside the method are called as Instance variables. 2. Scope of the instance variable is within the class(in all the methods). 3. JVM initializes the instance variables.

2. Scope of the local variables is within that method where it is declared 3. JVM doesn't initialize local variables. We have to initialize the localvariables explicitly. 4. Memory will be allocated for local variables when the method is invoked. 5. JVM allocates the memory for local variables in the stack frame.

4. Memory will be allocated for instance variables when object is created. 5. JVM allocates the memory for instance variables in the heapframe.

Stack memory and Heap memory : JVM uses heap memory to allocated memory for instance variables and objects. Instance variables may be primitive variables or reference variables. JVM uses stack memory to construct the stack. JVM creates one stack for class. Stack will be divided into number of stack frames based on the no. of methods available in that class. Memory will be allocated for local variables within the stack frame in which the corresponding method definition is available.

Blocks : Block is a set of statements defined between curly brasses '{ }'. We can write the blocks inside the class and also inside the methods. Blocks defined inside the class are called as instance blocks, blocks defined inside the methods are called as local blocks.

Static keyword:

We can use the static keyword for variables, methods and blocks. Static variables : Memory will be allocated for static variables at class loading time. Static blocks will be executed at class loading time. Static methods will be executed when you call it. The main advantage of static method is that no object or reference variable is required to invoke the static method .with the class-name, we can invoke the static method. Local variables cannot be static and local blocks cannot be static. If we declare a variable or a block as static inside a method, compiler gives the following error." illegal start of expression".

Difference between Static variables and Instance variables


Static variables 1. Variable declared inside a class with the static keyword is called static variable. 2. Memory will be allocated for static variable when we are loading class into the memory. 3. Only one copy of static variable will be created for 'n' number of objects. 4.static variables are allowed to use inside static blocks, instance blocks,static method and non-static methods. Instance variables 1. Variable declared inside a class without the static keyword is called instance variable. 2. Memory will be allocated for instance variable when we are creating a object. 3. 'n' copies of instance variables will be created for 'n' number of objects. 4.Instance variables are allowed to use inside instance blocks and instance method.These are not allowed to use inside static blocks and static method.

Static methods : A method which is declared with the static keyword is called static method. A method without static keyword is called non-static method or instance method.

Note : 1. Inside non-static method both static and non-static variables are allowed.

2. Inside a static method only static variables are allowed. Static Blocks : Blocks declared with static keyword called static blocks and blocks declared without static keyword is called non-static blocks.

Inside the static block only static variables are allowed. Inside the non-static block both static and non-static variables are allowed. Static block will be executed when we are loading class into memory. Non-static block will be executed when we are creating object.

OOPS IN JAVA
There are following oops Concepts : Abstraction Encapsulation Inheritance Polymorphism To describe object, there are two important rules in java : Properties , also called Variables. Operations , also called Methods. Abstraction : Providing necessary operations and properties of certain object by hiding internal details is called Abstraction. Encapsulation : Placing the data and operations which are going to operate on data in a single entity is called Encapsulation. Inheritance : Creating a new class by using the functionality of existing class.This new class is called subclass or child class or derived class .The existing class is called super class or parent class or base class. Main use of interitance is reusability.

Polymorphism : A process of behaving one form differently in different situation is called polymorphism.It is to create multiple definitions for operations. These are two types : 1. Compile time polymorphism : (early binding) This can be achieved with the help of method overloading. 2. Runtime polymorphism : (late binding)

This can be achieved with the help of method overriding. Objects & Classs : Class is a logical entity which is a template or plan or application form for an object.Object is a physical entity which is instance of the class.
Declaring the class : syntax: class classname { Datatype variable1,variable2,? return type method name(parameters) { ------------------------} }

Variables and methods are called as members of the class.


Ex: class Student { int sno=3; String sname="sri"; Void display() { System.out.println("Student id no is:"+sno); System.out.println("Student name is:"+sname); } } Creating object : Syntax: class-name reference variable = new class-name(); Ex: Student s= new Student(); One object will be created for Student class. In the above example 's' is the reference variable . Student () is the constructor of Student class. Calling the members of the class : Syntax: Object-name. member name Ex: s.display(); Example: class Student { int sno=3; String sname="sri"; Void display() { System.out.println("Student id no is:"+sno); System.out.println("Student name is:"+sname); }

} class Demo { public static void main(String as[]) { Student s= new Student(); s.display(); } } s- is a reference variable.

OOPS IN JAVA-II
Abstract class :
Example :
abstract class Animal { abstract void eat(); void show() { System.out.println("This is Animal show"); } } class Demo { public static void main(String as[]) { Animal a=new Animal(); //not allowed. a.show(); } }

Explanation : When you are unable to implement the method (Without body) declare the method as abstract. When you declare one (or) more methods as abstract you must declare the class also abstract. When class is abstract it can't be instantiated. In an abstract class we can have both abstract method and concrete methods. In abstract class we can have only concrete methods also. When class is abstract we should write a subclass for that (or)subclass is responsible to implement the abstract class. With subclass object only we can call the subclass methods as well as abstract class concrete methods. When you extend any abstract class we should override all the abstract methods in subclass, otherwise declare the subclass as abstract.

Polymorphism :
There are two types of polymorphism. 1.Compile time polymorphism (or) static polymorphism 2.Runtime polymorphism (or) Dynamic polymorphism We can achieve Compile time polymorphism with method overloading. We can achieve Runtime polymorphism using method overriding. At Compile time, compiler verifies method overloading rules and to invoke these methods it won't use object. It will use parameters of the method. At Run time JVM decides which method it has to invoke (super class method or subclass method) based on object which you are using to call the method. Note : We can assign subclass object to super class reference variable but we can't assign super class object to subclass reference variable. When class is abstract we can't create object for that but we can declare the reference variable. If we want to achieve the Runtime polymorphism, we must declare super class as abstract.

Interfaces :
Need for Interfaces : The benefits of using interfaces are much the same as the benefits of using abstract classes.Interfaces provide a means to define the protocols for a class without bothering about the implementation details.This seemingly simple benefit can make large projects much easier to manage.once interfaces have been designed, the class development can take place without bothering about communication among classes.

Another important use of interfaces is the capacity for a class to implement multiple interfaces.The major difference between inheriting multipleinterfaces and true multiple inheritance is that the interface approach enables you to inherit only method descriptions,implementations.If a classimplements multiple interfaces, that class must provide all the functionality for the methods defined in the interfaces.

Interface is a fully abstracted class. Interface can have only final static variables and public abstract methods. Syntax :
interface interface-name { datatype variable1, variable2?

------------------------------------------------------------return type method name (argument1,argument2,..); ---------------------------------}

Difference between abstract class and interface : Abstract class interface 1.Abstract class contains constants, variables, 1.Interface contains constants and public constructors,concrete methods and abstract methods. abstract methods. 2.Use extends keyword to extend the abstract class. 2.Use 'implements' keyword to implement the interface.

3.Abstract class doesn't support multiple inheritance 3.Interface support multiple inheritance i.e. after extends keyword more than one class name i.e. after implements keyword more than is not allowed. one class name is allowed. 4.When you are writing abstract methods in abstract class we should write abstract keyword. 5.If you want to declare final static variable and public abstract methods in abstract class, we should declare them explicitly. 4.When you are writing abstract methods in interface, no need to write abstract keyword. 5.By default all the variables are final and static and all the methods are public and abstract.

Using super Keyword


The base class constructor can be called in a derived class or subclass using super() in the constructor. Example :
class A { int a; A() { System.out.println ("This is A default constructor"); } A (int a) { this. a=a; System.out.println ("This is A one arg constructor"+a); } } class B { B() { super(); System.out.println ("This is B default constructor"); }

B (int a,int b) { super (a); this.b=a; System.out.println ("This is B one arg constructor"+b); } } class ConstructorOverloading { public static void main (String as[]) { B b1= new B (); B b2= new B(99,88); } } Output: This is A default constructor. This is A one arg constructor 99 This is B default constructor This is B one arg constructor 88.

When you have super class and subclass, with subclass object we can call both subclass members and super class members. Super class object is not required, but when you create super class object then only superclass constructor will be invoke.

Using super keyword ,we can invoke the super class constructor without creating superclass object.

We can write super with different signatures like : super(a); super(a,b); super(a,b,c..); When you are not writing any super() JVM insert default super(). When you are writing super(), JVM won't insert default super(). Super() must be the first line in the constructor. More than one super() is not allowed inside a constructor. Both this() and super() are not allowed inside a constructor at a time. Constructor invoking order is from bottom to top (subclass to super class). Constructor execution order is from top to bottom (super class to subclass).

Inheritance : Deriving a new class from existing class is called Inheritance. The main use of Inheritance is reusability. Syntax:

Class newclass-name extends existing class { Datatype variable1,variable2,?? returntype method() { ------------------} }

Inheritance
Example :
class CD { String title="inheritance"; String author="javasree"; void display() { System.out.println ("title is:"+ title); System.out.println ("author is:"+author); } } class VCD extends CD { String newfeature="SD SOFT"; void show() { System.out.println ("new feature is: "+newfeature); } } class Demo { public static void main(String as[]) { VCD c = new VCD (); c.show (); c.display (); } }

Output :
new feature is: SD SOFT title is: inheritance author is: javasree

Explanation : When you extend any class all the superclass members becomes members of subclass. With subclass object we can call subclass members as well as superclass members also. With superclass object we can call only superclass members. We can't call subclass members. Types of Inheritance : 1. Simple Inheritance

2. Multilevel Inheritance 3. Hierarchal Inheritance 4. Multiple Inheritance 5. Hybrid Inheritance 1.Simple Inheritance : In this process we can have only one super class and one subclass.

2.Multilevel Inheritance : The process of deriving of new class from already derived class is called multilevel inheritance.In this ,A is a superclass and two subclasses B,C. C is a subclass and has superclass called A and B.

3. Hierarchal Inheritance : In this process we have onesuperclass and many subclasses. In this one superclass and many subclasses. In the following diagram, A is superclass and B, C are subclasses.

4.Multiple Inheritance : (not allowed in java) In this process we have one subclass and many super classes.

Note: Java doesn't support multiple Inheritance using classes. In this one subclass can have many direct super classes. 5.Hybrid Inheritance :(not allowed in java) It is combination of multilevel and multiple and herarical inheritance.

Note: Java doesn't support hybrid Inheritance using classes.

MULTITHREADING
Introduction to Threads :A thread is a single independent path of execution with in a program. When a program runs, it starts executing the initial code and the subsequent methods and then continues the processing until the program is exited. This program uses a single thread where the thread is a single focus of control for the program.

Definition of a Thread :-

"Thread is a part of a program which has separate path of execution."

Difference between Process Based Multitasking And Thread Based Multitasking :Process Based Multitasking 1.Different Memory spaces will be allocated for different process. 2.Context-switching between processes is very expensive. Thread Based Multitasking 1.Only one Memory space will be allocated for different threads. 2.Context-switching is not expensive

3.Communication between processes 3.Communication between threads is very expensive. is inexpensive. 4.Executing more than one process (or) program concurrently is called process based multitasking. 4.Executing more than one thread concurrently is called thread based multitasking.

Creating Threads in Java : We can create the thread in java by two ways. 1.By implementing the Runnable interface. 2.By Extending the Thread class. Threads what you have creating using these two ways are called as child threads. Child threads must be started from main thread. Main thread is a thread which created and started by JVM when you are running the java program with main method. Example for Thread :class ThreadDemo { public static void main(String as[]) { Thread t=Thread.currentThread(); System.out.println(t); System.out.println(t.getName()); System.out.println(t.getPriority()); for(int i=0;i<5;i++) { System.out.println("Thread"); } try { t.sleep(500); } catch(Exception e)

{} } }

Creating a thread by implementing Runnable Interface :Any class whose instances are intended to be executed by a thread should implement the Runnable interface. The class implementing the Runnable interface must define run( ) method. The only method in Runnable interface is public void run(), which need to be overridden. All the tasks that the thread is supposed to execute is defined inside the run( ) method. For example :class MyThreadClass implements Runnable { int x; String name; Public MyThreadClass(String n) { name=n; } public void run() { for(x=0;x<10;x++) { System.out.println(name); } } } public class MainThreadClass { public void main(String args[]) { MyThreadClass tc1=new MyThreadClass("Hai?"); MyThreadClass tc2=new MyThreadClass("Hello?"); Thread t1=new Thread(tc1); Thread t2=new Thread(tc2); t1.start(); t2.start(); } }

Creating a thread by extending Thread Class :The second way of creating threads is by extending the Thread class. The following program extends the Thread class to create thread.
public class MyThread extends Thread { public void run() { System.out.println("Thread is running now??."); } public void start()

{ run(); } public static void main(String args[]) { MyThread t=new MyThread(); t.start(); } }

Thread Priorities :Priority of a thread is an integer number, ranging from 1 to 10 .Where '1' is lowest and 10 is highest. Sun has given three constant related to these thread class.
1.MIN_PRIORITY 2.NORM_PRIORITY 3.MAX_PRIORITY Minimum priority of thread is '1'. Normal priority of thread is '5'. Maximum priority of thread is '10'.

We can modify the priority of thread and we can get available priority of thread using the following methods.
void setPriority(int x) int getPriority()

THREAD LIFE-CYCLE :-

Thread life-cycle :As a programmer you have to create a thread and start.The thread will be in "ready to run" state. JVM call run method on thread by using scheduling algorithms which means that when CPU time is allocated for the thread, JVM calls the run() method on that thread. After calling the run( ) method, thread will be in running state. When we call sleep on running thread by specifying some amount of time, thread will be entered into sleeping state. After the elapsed time is over thread will be moved from sleeping state to ready to run state. When we call wait() on running thread, thread will be entered into wait state,to send the waiting thread to ready to run state we have to call notify( ) or notifyAll( ).System uses LRE(Least Recently Used) algorithm to move the thread from waiting state to ready to run state. When the thread is waiting for a resource which is not available. The thread will be moved to blocked state, when that resource is available latter the thread will be entered into ready to run state. When we call stop( ) or destroy( ) on thread, it will enter into dead state.

Deadlock : Consider two threads A and B. A is holding resource R1 and waiting for resource R2. B is holding resource R2 and is waiting for resource R1. A will release R1 After getting R2 and B will release R2 after getting R1 only. A and B waiting for R1 and R2 mutually. This situation is called deadlock.

Daemon Threads : Daemon threads are service threads, these threads depends on child threads and provide the service to child threads as long as child threads are running, daemon threads will run and will provides service. We can check whether the thread is daemon thread or not by using the following method. boolean isDaemon(); We can set a thread as a Daemon thread with the following method.

void setDaemon();

SYNCHRONIZATION :In multi-threaded environment, you want to allow only one thread to access the given object i.e. you don't want to allow to accept the given object concurrently to achieve this we need to apply synchronization. We have two ways to apply the Synchronization. 1.Method level Synchronization. 2.Block level Synchronization. Example :- For Method level
public synchronized void getData() { x++; }

Example :-For Block level


public void getData() { synchronized(this) { x++;

} }

You might also like