Class 38
Class 38
==============
Sometimes we will declare a class inside another class such concept is called inner
class.
ex:
class Outer_class
{
class Inner_class
{
-
-
-
}
}
But due to powerful features and benefits of inner classes. Programmers started to
use inner classes in regular programming.
If we compile above program we will get two .class files i.e Outer.class and
Outer$Inner.clas.
ex:
----
class Outer
{
class Inner
{
public void m1()
{
System.out.println("Inner-M1 Method");
}
}
class Outer
{
class Inner
{
public static void m1()
{
System.out.println("Inner-M1 Method");
}
}
2) Mutable object
1) Immutable object
--------------------
After object creation if we perform any changes then for every change a new object
is will be created such type of object is called immutable object.
ex:
String and Wrapper classes
2) Mutable object
-----------------
After object creation if we perform any changes then all the required changes will
be done in a same object such type of object is called mutable object.
ex:
StringBuffer and StringBuilder
String
======
It is a collection of characters which is enclosed in a double quotation.
case1:
------
Once if we create a String object we can't perform any changes.If we perform any
changes then for every change a new object will be created such behaviour is called
immutability of an object.
Diagram: class38.1
case2:
-----
What is the difference between == and .equals() method?
==
----
It is a comparision operator which returns boolean value either true or false.
ex:
class Test
{
public static void main(String[] args)
{
String s1=new String("ihub");
String s2=new String("ihub");
System.out.println(s1==s2); // false
}
}
.equals()
----------
It is a predefined method present in String which returns boolean value either true
or false.
ex:
---
class Test
{
public static void main(String[] args)
{
String s1=new String("ihub");
String s2=new String("ihub");
System.out.println(s1.equals(s2)); // true
}
}
case3:
-----
Once if we create a String object.Two objects will be created one is on heap and
another is on SCP area.But 's' always points to heap area.
Diagram: class38.2
Object creation in SCP area is always optional.First JVM will check is there object
is created with same content or not. If it is not created then JVM will create a
new object.If it is created then JVM Won't create any new object.Hence there is no
chance of having duplicate objects in SCP area.
SCP area objects do not have any object reference even though garbage collector
can't access them.
Diagram: class38.3
Diagram: class38.4
input:
hello
output:
5
ex:
class Test
{
public static void main(String[] args)
{
String str="hello";
System.out.println(str.length());
}
}
input:
hello
output:
h
e
l
l
o
ex:
class Test
{
public static void main(String[] args)
{
String str="hello";
for(int i=0;i<str.length();i++)
{
System.out.println(str.charAt(i));
}
}
}
input:
ihub
talent
output:
ihubtalent
ex:
---
class Test
{
public static void main(String[] args)
{
String str1="ihub";
String str2="talent";
System.out.println(str1.concat(str2));
}
}
ex:
class Test
{
public static void main(String[] args)
{
String str1="ihub";
String str2="ihub";
if(str1.equals(str2))
System.out.println("Both are equals");
else
System.out.println("Both are not equals");
}
}
input:
IHUB
ihub
output:
Both are equals
ex:
class Test
{
public static void main(String[] args)
{
String str1="IHUB";
String str2="ihub";
if(str1.equalsIgnoreCase(str2))
System.out.println("Both are equals");
else
System.out.println("Both are not equals");
}
}
input:
IHUBTALENT
output:
ihubtalent
ex:
class Test
{
public static void main(String[] args)
{
String str="IHUBTALENT";
str=str.toLowerCase();
System.out.println(str);
}
}
input:
ihubtalent
output:
IHUBTALENT
ex:
class Test
{
public static void main(String[] args)
{
String str="ihubtalent";
str=str.toUpperCase();
System.out.println(str);
}
}
input:
I_h@ub$Tale#nt1
output:
IhubTalent1
ex:
---
class Test
{
public static void main(String[] args)
{
String str="I_h@ub$Tale#nt1";
str=str.replaceAll("[^A-Za-z0-9]","");
System.out.println(str);
}
}
input:
I hub Tale nt
output:
IhubTalent
ex:
---
class Test
{
public static void main(String[] args)
{
String str="I hub Tale nt";
str=str.replaceAll("\\s","");
System.out.println(str);
}
}
input:
Ihub12
Talent18
output:
IhubTalent30
ex:
---
class Test
{
public static void main(String[] args)
{
String str1="Ihub12";
String str2="Talent18";
String word1=str1.replaceAll("[^A-Za-z]","");
int num1=Integer.parseInt(str1.replaceAll("[^0-9]",""));
String word2=str2.replaceAll("[^A-Za-z]","");
int num2=Integer.parseInt(str2.replaceAll("[^0-9]",""));
System.out.println(word+num);
}
}
input:
hello
output:
olleh
class Test
{
public static void main(String[] args)
{
String str="hello";
for(int i=str.length()-1;i>=0;i--)
{
System.out.print(str.charAt(i));
}
}
}
Assignment
----------
Q) Write a java program to find out given string is palindrome or not?
input:
racar
output:
It is a palindrome string