Before going into programs a small
introduction on Strings
STRINGS:-
1.String is a class in Java
2.String means collection of characters
3.String is a sequence of characters enclosed
in double Quotes.
4.Java Strings are sequence of Unicode
characters
5.String class produces Immutable objects.
6.String class does not have a methods that
let you change a characters in an existing String.
7.It is like number 5 , the String
"HELLO" we cannot change it.
Sting Objects can be created in two ways
1. without using new operator
String str1="javainchennai";
2. with using new operator
String str2=new String("javainchennai");
The memory space is divided in to two parts
1.constant area
2.non-constant area
If you create object without using new
operator this will be saved in constant area
(e.g)
String str1="javainchennai";
If you create object with using new operator
this will be saved in non-constant area
(e.g) String str2=new String("javainchennai");
Here Constant area will not allow duplicates
where as non-constant area will allow
duplicates.
Do Remember inside a String class the
toString() method is overided. Here if you try to print reference
variable
String s1="javainchennai";
System.out.println(s1);
The output
is javainchennai instead of String@ff2201(classname @ hexadecimal value)
equals() method also overrided to check 2
objects based on the value(content) it contains
Observe the program carefully and read the
explanation:-
1.package com.vinodh;
2.public class StringTest {
3. public static void
main(String[] args) {
4. String
str1="javainchennai";
5. String
str2="javainchennai";
6. String str3=new
String("javainchennai");
7. String str4=new
String("javainchennai");
8. System.out.println(str1==str2);
9.
System.out.println(str1==str3);
10. System.out.println(str2==str3);
11. System.out.println(str3==str4);
12. System.out.println(str1.equals(str2));
13. System.out.println(str1.equals(str3));
14. System.out.println(str2.equals(str3));
15. System.out.println(str3.equals(str4));
16. }
17. }
output:-
true
false
false
false
true
true
true
true
Explanation:-
If you say 5==5 is true, here two are
primitive literals so == operator(checks content) prints true.
If you use == operator to compare two object
references it will see whether both references are pointing to same object or
different.
If both references are pointing to same
object it(== operator) returns true otherwise false.
observe line4 and line5 ,here actually I
created String object without using new operator so, str1 and str2 both
references are pointing
to same object (javainchennai). (constant
area will not allow duplicates)
so, str1==str2 returns true.
observe line4 and line5 ,here actually I
created String object by using new operator so, str3 and str4 both references
are pointing to
different objects (javainchennai).(non
constant area allows duplicates).
So, str3==str4 returns false.
Observe the same effect with equals() method
it returns true all time .(because it is overrided to compare based on content
inside object)
PROGRAMS:-
1.public class StringTest {
2. public static void
main(String[] args) {
3.
System.out.println(3+5);
4.
System.out.println("3"+5);
5.
System.out.println(3+"555");
6.
System.out.println(3+2+5+"4444");
7.
System.out.println("35"+111+"99");
8.
System.out.println("42"+11+32+"77");
9.
System.out.println("100"+"222");
}
}
OUTPUT:-
8
35
3555
104444
3511199
42113277
100222
Explanation:-
In Java there is no operator overloading
concept.
+ is the only one operator overloaded to add
2 integers as well as 2 strings
In the above example we added 2 primitives
that is common ,
when we added String with any primitives only
String object is produced.
observe the output of lines 6,7,8,9.
No comments:
Post a Comment