String
is a group of characters we can create the object of a string class as usual by
using ‘new’ operator.
class StringDemo {
public
static void main(String[] args) {
String s = new String();
String s1 = new String("itargetit");
String s2 = new String("itargetit");
String s3 = "mahesh";
} }
|
We can create the object
of string without using ‘new’ operator
also. To create the string object by using double quotes (“ “).
When JVM encounters double
quotes, it will understand that it has to create an object of a string class
with the content inside “ “.
Each character inside the “
“ will be assigned with a unique index value.
Difference b/w creating a new string object & creating string
object with “ “:-
when new operator is used
for creating str1 and str2 two different objects will be created by the JVM
with same content, because content is same in str1 and str2.
Str.equals(str2) àgives “true” as a result.
equals() is used to compare
the contents inside two different objects.
In the below case of
str1==str2 is giving a “false” as a result, because hashcodes are not same,
that means they are not referring same object. ‘==’ is used to compare hashcode of
objects.
In below program str4 and
str5 are string objects created using “ “ with same content
When string object is
created with “ “, an object for a string class is created inside RAM by JVM
which is known as “String constraint pool”.
When we create str5 object
with “ “ again JVM will check whether the content inside “ “ is already exist
in string constraint pool or not.
If yes, the str5 variable
will refer same object. if no, new constraint pool will be create.
Here “rani” is same content
that is refered by str4 and str5, so their hashcodes are equal.
Str4==str5
à
give true result .
Similarly str4.equals(str5) gives true as a result.
class A { }
class StringDemo {
public
static void main(String[] args) {
String str = new String();
String str1 = new String("itargetit");
String str2 = new String("itargetit");
A a = new A();
System.out.println(str);
System.out.println(str1);
System.out.println(str2); // content
System.out.println(a); // address of a Object
System.out.println("equals
:"+str1.equals(str2));
System.out.println("==
"+(str1= =str2));
String str4 = "rani";
String str5 = "rani";
System.out.println(str4);
System.out.println("equals
:"+str4.equals(str5));
System.out.println("==
"+(str4= =str5)); }
}
|
Explanation:-
System.out.println(str2);
When this statement is
executed the content of string object is printed.
System.out.println(a)
this statement is executed, address of the object is printed.
(Q) Since str2 and ‘a’ are
both are objects but why content is printed once and address is printed once ?
Println method is defined
with 2 different parameters in a printStream class.
One method accepting object as an argument,
prints the address of an object.
The other method accepting
string as an argument as a functionality implemented to print the content of a
string object.
Here they have used static
polymorphism in case of println().
String is immutable:-
Once the string object is
created the content inside that object can’t be changed. So, string object is
known as immutable object and string class is known as immutable class(class
supporting to create an immutable objects known as immutable class).
Program:-
class Immutable {
public
static void main(String[] args) {
int x = 10+13 ; // + arithemetic operator
String str = "jani";
System.out.println(str.hashCode());
str = str+10 ; // + acts as concatenation operator
System.out.println(str.hashCode());
String s = "kiran" ;
s = s+"is boy";
System.out.println(x);
System.out.println(str);
System.out.println(s); } }
|
Program:-
class Example {
public
static void main(String[] args) {
String myString = " iaman Employee" ;
String cde = "cde";
String c = "abc".substring(2,3); // String in
between indexes 2 and 3
String d = cde.substring(1,
2);
int length = myString.length();
System.out.println(length);
String sub = myString.substring(2,9);
System.out.println(sub);
String sub2 = myString.substring(6);
System.out.println(sub2);
System.out.println(c);
System.out.println(d);
System.out.println(myString.length());
String trString = myString.trim();
System.out.println(trString.length());
System.out.println(trString);
} }
|
String Buffer:-
String buffer is a
class variable in java.lang package. If we create an object of a StringBuffer
we can change the content inside string buffer. So, string buffer object is set
to be a mutable object and string buffer class is set to be mutable class.
Program:-
class Mutable
{
public static void main(String args[])
{
StringBuffer buff = new StringBuffer("examination hall");
StringBuffer str="examination hall";
System.out.println(str);
System.out.println(str.hashCode());
str=str+”today”;
System.out.println(str);
System.out.println(str.hashCode());
System.out.println(buff);
System.out.println(buff.hashcode());
buff.append(“maths”);
System.out.println(buff);
System.out.println(buff.hashcode());
}
}
|
Java Beans:-
·
In
a class if variables are declared as private and public methods are provided to
set the values to those variables and get the values from those variables,
which are known as setter,getter methods. Then that java class is known as
"javaBeans".
·
"Encapsulation" is the concept of binding the data along with
their corresponding functionalities.
·
we
achieve the concept of encapsulation only by declaring all the variables as
private and providing public setter,getter methods.
Note:-
javaBeans
are used to transfer the data. so, java beans are also known as "data
transfer objects" (DTO).
this
is used to set the value and get the values.so,it is known as "value
object"(VO)
Program:-
class
Employee
{
private
int age ;
private
double sal ;
private
String name ;
private
String phone ;
private
String email ;
public
void setAge(int empAge) {
this.age = empAge ;
}
public
int getAge() {
return
age ;
}
public
void setSal(double
sal)
{
this.sal = sal;
}
public
double getSal() {
return
sal ;
}
public
void setName(String
name)
{
this.name = name ;
}
public
String getName() {
return
name;
}
public
void setPhone
(String phone) {
this.phone = phone ;
}
public
String getPhone() {
return
phone ;
}
public
void setEmail(String
email)
{
this.email = email ;
}
public
String getEmail() {
return
email ;
} }
|
class
Organization {
private
Employee employee ;
public
void setEmployee(Employee employee) {
this.employee = employee ;
}
public
Employee getEmployee() {
return
employee;
} }
|
class
Main
{
public static void
main(String args[]) {
Employee
emp = new Employee();
emp.setAge(25);
emp.setSal(20000);
emp.setName("Ramana
Reddy");
emp.setPhone("9808878787");
Organization
org = new Organization();
org.setEmployee(emp);
Employee
ramana = org.getEmployee();
double
sal = ramana.getSal();
System.out.println(sal);
String
empName = ramana.getName();
System.out.println(empName);
} }
|
No comments:
Post a Comment