* It is a predefined class used for storing group of characters similar to string class.
* The string buffer objects can be created only in one mechanism i.e., by using new operator.
Syntax: String Buffer sb = new StringBuffer(“welcome”);
* The string buffer class objects are immutable objects i.e., the string buffer object content can be modified.
String Buffer sb = new StringBuf fer(“hai”);
sb = new StringBuffer(“bye”);
Methods of String Buffer:
1. int length():
This method will return the count of no of characters available in string buffer.
Ex:
String sb = new StringBuffer(“welcome”);
System.out.println(sb);
System.out.println(sb.length());
O/P: welcome 7
2. StringBuffer append(xxx):
This method can be used for appending the specified existing string buffer content.
Ex:
String sb = new StringBuffer(“java”);
System.out.println(sb.append(1.6);
System.out.println(sb.append("program"));
3. StringBuffer deleteCharAt(int index):
This method will delete available at the specified index position
4. StringBuffer delete(int index, int offset):
This method will delete group of characters beginning from the specified index position upto the specified offset.
Ex:
String sb = new StringBuffer(“java1.6program”);
System.out.println(sb.deleteCharAt(5);
System.out.println(sb.delete(4,10);
O/P: java16program javaram
5. String substring(int index):
This method can be used for accessning or retrieving a part of string buffer beginning from index upto the end of the string buffer.
6. String substring(int index,int offset):
This method can be used for retrieving a part of string beginning and upto specified offset.
Ex:
String sb = new StringBuffer(“java1.6program”);
System.out.println(sb.substring(4));
System.out.println(sb.substring(3,7);
O/P: 1.6program a1.6
6. StringBuffer insert(int index, int xxxx):
This method can be used for inserting specified content in specified index position.
Ex:
String sb = new StringBuffer("program”);
System.out.println(sb.insert(0,"java"));
System.out.println(sb.insert(4,1.6);
O/P: javaprogram java1.6program
8. StringBuffer replace(int index, xxxx):
This method can be used to replace group of characters beginning with specified index position upto the specified offset with specified string.
Ex:
String sb = new StringBuffer("java1.6 program”);
System.out.println(sb.replace(0,4"oracle"));
O/P: oracle1.6program
9. StringBuffer reverse():
This method can be used for reversing contents of string buffer.
Ex:
String sb = new StringBuffer("java1.6 program”);
System.out.println(sb.reverse());
O/P: margorp6.1avaj
OOPS
Object Oriented Programming Concept:
An application developed in any language has to use one of the following concepts.
1. Procedure Oriented Programming Concepts 2. Object Oriented Programming Concepts 1. Procedure Oriented Programming Concepts:
The applications that are developed by using the concept of procedure or functions are said to use procedure oriented programming concepts.
Examples of using POPC:
C, COBOL, FORTRAN, PASCAL follow popc.
Drawbacks (or) Limitations for Procedure Oriented Language:
* The application that are developed by using popc are difficult to maintain and debugging of the application will take more time.
* The procedure oriented programming language gives more importance to the functions than to the data.
* The application that are developed by using popc does not provide security to the data.
* The data available in the application developed by popc is open and they are accessible to the entire application which is not suitable for developing distributed applications.
* The application that are developed by using popc are difficult to enhance.
* The procedure & functions used in procedure oriented language are the fundamental entities for developing the application. The design of these fundamental entities is very week & they are not suitable for developing complex & real time applications.
Note: The procedure oriented programming languages are also called as structured programming languages.
2. Object Oriented Programming Concepts:
The applications that are developed with the help of classes and objects are said to use object programming concepts.
Examples of using OOPC:
Java, C++, Python, .Net etc., Object:
Any entity that exists physically in the real world which requires some memory is called an object.
* Every object contains some properties & actions. The properties will describe the object and they are represented by variables.
* The actions are the tasks performed by the object and they are described by the methods.
Class:
A class is a specification of common properties & common actions of group of objects.
* A class can be considered as model or a plan or blueprint for creating the object.
* Without a class object creation is not possible.
* For every class we can create any no of objects. Even though C++ is an object oriented programming language according to the programming language experts it is called partial object
oriented programming language because of following reasons.
i. In a C++ application we can write some code inside the class & outside the class.
ii. In a C++ language we can develop application without using any of the object oriented programming concepts.
iii. In a C++ application using the concept of friend function we can violate any level of security.
iv. All the object oriented programming concepts are derived from the real world from the human being lives so that the programmer understands the concept easily & implementation of that concept will not be difficult.
v. The design of the oopc is very strong & they can be used for developing real time complex application.
The various object oriented programming concepts are 1. Encapsulation
2. Abstraction 3. Inheritance 4. Polymorphism
1.Encapsulation:
The process of binding the variables & methods into a single entity is called as encapsulation.
* We can achieve encapsulation in java program by using a class.
* Using encapsulation we can improve maintainance of application.
* Using encapsulation we can also achievedata hiding.
* Using encapsulation we can separate or isolate members from one class to another class.
2. Abstraction:
The process of hiding some information & presenting the required information is called as abstraction.
* Using abstraction we achievesecurity i.e., we can protect the data from unauthorized usage.
3. Inheritance:
The process acquiring the members from one class to another class is called as inheritance.
* Using inheritance we can achieve reusability, which will reduce the development time of the application.
4. Polymorphism:
If a single entity shows multiple behaviors then it is called po lymorphism.
* Using polymorphism we can achieve flexibility where a single entity can perform different operations according to the required.
Class:
A class is a user defined data type using which we can store any no of values.
Syntax for declaring a class:
Class<className>{
datatype variable1;
datatype variable2;
datatype variable3;
returntype methodName1(parameters){
statements }
returntype methodName1(parameters){
statements }
: }
* A class is a collection of variable & methods.
* A class can contain any no of variables & methods.
Student.java:
Class Student{
int rollno;
double marks;
string name;
void display(){
System.out.println(“rollno:”+rollno);
System.out.println(“marks:”+marks);
System.out.println(“name:”+name);
}
Public static void main(String[] args) Student S=new student();
S.display();
} }
* When we compile a java program the compiler will check whether the java code is valid (or) not, if valid the compiler will generate a (.class) file.
* The (.class) file generated by the compiler will be given to the JVM for execution.
* The JVM will begin the execution from “main” method.
* In the above program student s=new student(); will create an object of student class.
* Creating an object means allocating memory for the instance variables of the class.
Instance Variables:
If a variable is declared inside the class & outside the methods, then those variables are called as instance variables.
* If an instance variable is declared & not initialized then they will initialize automatically with default values of that type.
Default Values
byte - 0 short - 0 int - 0 long - 0 float - 0.0 double - 0.0 char - space boolean - false
string - null student - null Any class – null
It is the responsibility of the JVM for calling the main method & execution begins from main method.
* If the class contains other methods then it is the responsibility of the programmer to call other methods.[s.display()]
O/P:rollNo : 0 marks : 0.0 name : null
* Every class in java language will be represented in the form of a class diagram. A class diagram will be represented in rectangular shape, divided into multiple partitions (3) as follows.
* Every class can contain variables & methods which are called as members of a class.
Variables:
The purpose of variable is to store (or) hold some data (or) value. A java program can contain any number of variables.
* The declaration of the variables in a java program is dynamic i.e.,we can declare the variables anywhere in the class.
* The java language is said to be strongly typed language. Any language can be said to be strongly typed language if the variables are first declared & then used & the compiler for type compatibility.
int x = 12; //valid
int x = true; //invalid incompatable
Methods:
The purpose of a method is to perform some task/operation.
* A java program can contain any no of methods. Inside the method, we can specify any no of statements.
Syntax: returntype methodName(list of parameters){
Statements }
* Every method available in a program will be divided into two parts.
1. Method Declaration/Header/Prototype 2. Method Definition/Body/Implementation 1. Method Declaration/Header/Prototype:
The method declaration includes three parts & they are
1 2 3
returntype methodName (list of parameters)
* The returntype & methodName is mandatory whereas the list of parameters is optimal. We can pass any number of parameters & they can be of any type separated by a comma symbol.
* The return type of a method must be specified just before the method name.
* If we do not want the method to return any value then the return type of the method must be specified as void. If we want the method to return a value then we should not specify the return type as void. In that location, we can specify any datatype.
2. Method Definition/Body/Implementation:
The method definition includes a group of statements specified within the flower braces.
{
Statements }
* The method definition can contain any no of statements.
* If the method definition doesn’t contain any statements, then we call such kind of methods as empty definition (or) null implementation.
* If the method declaration contains return type as void, then we must not specify return statement in the method definition. But if the return type is not void, then we must specify return statement in the method definition.
Syntax: return value;
Ex: return 123;
return 12.3;
return true;
return ‘d’;
return “hello”;
* The return statement will be generally specified as the last statement in the method definition but it can be specified anywhere in the definition.
* A method can return atmost one value.
Types of methods:
1. Methods without returntype & without parameters:
class Addition { void add { int x = 12;
int y = 13;
int z = x+y;
System.out.println(“ sum of ” +x+ ”and” +y+ “is:” +z);
}
Public static void main(String[] args){
Addition a = new Addition();
a.add(); //method invocation (calling add method) }
}
2. Methods without returntype & with parameters:
class Addition{
void add(int x, int y){
System.out.println("sum of"+x+ "and" +y+ "S:" +(x+y));
}
Psum(String[] arr){
Addition a = new Addition();
a.add(2,3);
(or)
Addition a = new Addition();
int m = Integer.parseInt(arr[0]);
int n = Integer.parseInt(arr[1]);
a.add(m,n);
} }
3. Methods with returntype & without parameters:
class Addition { int add() {
int x = 10;
int y = 20;
int z = x+y;
}
public static void main(String[] args){
Addition ad = new Addition();
int res = ad.add();
System.out.println("sum is:"+res);
} }
4. Methods with returntype & with parameters:
class Addition{
int add(int x, inty){
return(x+y);
}
Psum(String[] arr){
Addition ad = new Addition();
int res = ad.add(3,4);
System.out.println("sum is:" +res);
res = ad.add(5,6);
} }
Why should we write logic in separate methods?
When we write the logic in separate methods we will be able to achieve Modular Approach. Using the modular approach we can achieve reusability & reduce the development time.
Recursion:
Recursion means calling the same method itself multiple times then we call it as recursion & that method is called as recursive method.
class Factorial{
int fact(int n){
if(n == 1)
return 1;
int f=1;
f = n * (fact(n-1));
return f;
}
psum(String[] ar){
factorial f = new factorial();
for(int i=1;i<=10;i++){
int res = f.fact(i);
System.out.println(i+"!=" +res);
} }
}
Java stack if n = 6 (6!)
Program to pass an array as a parameter:
class Sample{
void display(int[] nos){
for(int x: nos)
System.out.println(x);
}
Psum(String[] arr){
Sample S = new Sample();
int[] arr1 = {10,20,30,40,50};
S.display(arr1);
int[] arr2 = {100,200,300,400};
S.display(arr2);
} }
Program to pass an object as a parameter to a method:
class Student{
int rollNo = 12;
double marks = 99;
}
class Display{
psum(String[] arr){
Display d = new Display();
Student S = new Student();
d.Show(S);
}
void show(Student Stu){
System.out.println(Stu.rollNo);
System.out.println(Stu.marks);
} }
Constructors:
If a variable is declared inside the class & outside the methods then such kind of variables are called as instance variables.
* If any instance variable is declared & not initialized then it will be automatically initialized with default values.
* If we do not want the default values, then we can initialize the instance variables with our own values in the following two locations.
1. At the time of declaration.
2. By using the constructor.
Constructor:
A constructor can be considered as a special method whose purpose is to initialize the instance variables.
Rules:
* The name of the constructor name must be same as that of class name. (i.e., classname &
constructor name must be exactly same).
* A constructor should not contain any return type, not even void.
Note: If we specify a return type before the constructor then the code is valid but it will be considered as a method not a constructor.
* A constructor can be executed at the time of object creation.
* A constructor will be executed one time for one object. (It create 10 objects, 10 times constructor will be executed)
* A constructor can have parameters based on the no of parameters, constructors are classified into two types.
1. Zero parameterized Constructor/Default Constructor.
2. Parameterized Constructor.
1. Zero parameterized Constructor:
If a constructor doesn’t contain any parameters, then we call it as zero parameterized Constructor.
Syntax: class<ClassName>{
<className>{
} }
2. Parameterized Constructor:
If a constructor contains some parameters, then we call it as parameterized constructor.
Syntax: class<ClassName>{
<className>(list of parameters){
} }
* To a parameterized constructor, we can pass any no of parameters which can be of any type separated by comma.
** Every class in java will contain a constructor whether we specify (or) not.
** If the programmer does not specify any constructor then the compiler will provide a zero parameterized constructor.
Note: compiler can never provide a parameterized constructor.
class Sample{
int x; //declared but not initialized
int y=10; //declared & initalized at the time of declaration int z; //declared & intialized by using constructors Sample(){
z=10;
}
void Show(){
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
Psum(String[] ar){
Sample S=new Sample();
S.show();
} }
Psum(String[] ar){
Sample S=new Sample();
S.Show();
} }
O/P: 0 10 20
int add(int []) //add all elements in array int min(int []) //smallest element in array int max(int []) //biggest element in array
Instance Variables:
If a variable is declared inside the class & outside the methods, then it is called as instance variable.
* For every class, we can create any number of objects and every object will contain its own copy of the instance variable.
* If an instance variable is declared & not initialized, then it will be automatically initialized with default values.
* If we do not want the default values, then we can initialize the instance variables with our own values, either at the time of declaration (or) by using any constructor.
* If we want all the instances(objects) to contain instance variables with same values then initialize the instance variable either at the time of declaration (or) by using a zero parameterized constructor.
* If we want all the instances (objects) to contain instance variables with different values then initialize the instance variables by using a parameterized constructor.
“this” Keyword:
“this” keyword can be used to refer to the current instance (object) of the class.
* Using “this” keyword we can access the instance members (Variables & Methods) of the class.
Local Variables:
The variable which is declared inside the class & inside the blocks (Methods and Constructors), then it is called as local variables.
* If there is no confusion between instance variable & local variable then specifying “this”
keyword is optional. In such case, if we do not specify “this” keyword before the variable so that it refers to the instance variable.
* If there is confusion between instance variables & local var iables then specifying “this”
keyword is mandatory. In such case, if we do not specify “this” keyword, compiler will not specify “this” keyword then it gives preference to local variables.
* If we want to refer to the instance variable then the programmer has to explicitly specify “this”
keyword before the variable so that it refers to the instance variable.
class Student{
int rollNo;
Student(int rollNo){
this.rollNo = rollNo;
}
void display(){
System.out.println(this.rollNo);
}
Psum(String[] ar){
Student S1 = new Student(123);
S1.display();
Student S2 = new Student(456);
S2.display();
Student S3 = new Student(789);
S3.display();
} }
Static Keyword:
* If an instance variable is declared in a class, then the memory for that instance variable will be allocated one time for each object.
* If we do not want to allocate memory for a variable one time for each object then declare that variable as “Static” i.e., preceed the instance variable declaration by “Static” keyword.
** If a static variable is declared then the memory for that variable will be allocated at class loading time.
** The memory for the static variable will be allocated in method area.
** The memory for the static variable will be allocated only one time for the entire class.
All the objects of the class will share the same copy of the static variable. We can declare both the variables & methods of a class as “Static” and they are together called as “Static
members”. The static members are also called as class members.
Syntax for Static Variable:
Static datatype varname;
Syntax for Static Method:
Static returntype methodName(list of parameters) {
Statements }
* The instance members(instance variables & static methods) of the class can be accessed only by using reference variable.
* The static members(static variables & static methods) of the class can be accessed either by using the reference variable (or) by using a classname.
* It is recommended always access the static members by using class name because we cannot guarantee the existence of an object.
class Employee{
int empNo = 1234;
static int compcode = 2222;
psum(String[] args){
Employee e = new Employee();
System.out.println(e.empNo);
System.out.println(e.compcode);
System.out.println(Employee.compcode);
} }
Rules:
* An instance methods can access both instance variables & static variables.
* A static method can access only static variables.
Ex: class Sample{
int x = 11;
Static int y = 22;
void show(){
System.out.println(x);
System.out.println(y);
}
Static void display(){
System.out.println(x);
System.out.println(y);
}
Psum(String[] ar) {
Sample S = new Sample();
S.Show();
Sample.display(); // (or) S.display();
* If the instance variables & static variables are not initialized, then they will be initialized automatically with default values.
* For a static variable if we do not want the default value then we can initialize the same variable without own value only at the time of decalaration.
Ex: class Sample{
int x;
Static int y;
Psum(String[] args){
Sample S1 = new Sample();
S1.x++;
S1.y++;
System.out.println(S1.x+” ”+S1,y);
Sample S2 = new Sample();
S2.x++;
S2.y++;
System.out.println(S2.x+” ”+S2,y);
Sample S3 = new Sample();
S3.x++;
S3.y++;
System.out.println(S3.x+” ”+S3,y);
} }
O/P: 1 1 2 2 3 3
class Student { int m1, m2, m3;
Student(int int int) main()
}
class result{
int calculatetotal(Student S) }
Rules for accessing instance & static members:
* An instance method can access members (Instance variables & Instance methods) directly provided if they belong to same class otherwise they must be accessed only by using reference
variable(object).
* An instance method (or) access static members (static variables & static methods) directly provided they belong to same class otherwise they must be accesed either by using reference
variable (or) classname.
* A static method can access static members (static variables & static methods) directly provided if they belong to same class otherwise they must be accessed either by using reference varaiable (or) class name.
* A static method can access instance members (instance variables & instance methods) only using reference variable object whether they belong to the same class (or) different class.
Ex: Class Access1{
int x = 11;
static int y=22;
void m1(){
System.out.println("Instance method m1");
}
void m2(){
System.out.println(x); //Rule1
m1(); //Rule1
System.out.println(y); //Rule2
m3(); //Rule2
System.out.println("Instance method m2");
}
Static void m3(){
Static void m3(){