0% found this document useful (0 votes)
17 views10 pages

Class 38

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Class 38

Uploaded by

M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Inner classes

==============
Sometimes we will declare a class inside another class such concept is called inner
class.

ex:
class Outer_class
{
class Inner_class
{
-
-
-
}
}

Inner classes introduced as a part of event handling to remove GUI bugs.

But due to powerful features and benefits of inner classes. Programmers started to
use inner classes in regular programming.

Inner class does not allow static members.

Accessing inner class data from static area of outer class


----------------------------------------------------------
class Outer
{
class Inner
{
public void m1()
{
System.out.println("Inner-M1 Method");
}
}

public static void main(String[] args)


{
Outer.Inner i=new Outer().new Inner();
i.m1();
}
}

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");
}
}

public static void main(String[] args)


{
new Outer().new Inner().m1();
}
}

Accessing inner class data from non-static area of outer class


---------------------------------------------------------------
class Outer
{
class Inner
{
public void m1()
{
System.out.println("Inner-M1 Method");
}
}

public void m2()


{
Inner i=new Inner();
i.m1();
}

public static void main(String[] args)


{
Outer o=new Outer();
o.m2();
}
}

class Outer
{
class Inner
{
public static void m1()
{
System.out.println("Inner-M1 Method");
}
}

public void m2()


{
Inner i=new Inner();
i.m1();
}

public static void main(String[] args)


{
Outer o=new Outer();
o.m2();
}
}
o/p:
C.T.E : Illegal static declaration in inner class

Types of objects in java


=========================
We have two types of objects in java.
1) Immutable object

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.

It is used for reference comparision or address comparision.

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.

It is used for content comparision and it is a case sensitive.

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.

SCP objects will destroy automatically when JVM shutdowns or terminated.

Diagram: class38.3

Interning of String object


==========================
With the help of heap object reference if we need corresponding SCP object
reference then we need to use intern() method.

Diagram: class38.4

String important methods


========================
Q) Write a java program to find out length of the string?

input:
hello

output:
5

ex:

class Test
{
public static void main(String[] args)
{
String str="hello";
System.out.println(str.length());
}
}

Q) Write a java program to display string character by character?

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));
}
}
}

Q) Write a java program to concatinate two strings?

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));
}
}

Q) Write a java program to check both strings are equal or not?


input:
ihub
ihub
output:
Both are equals

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");
}
}

Q) Write a java program to check both strings are equal or not?

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");
}
}

Q) Write a java program to convert uppercase string to lowercase ?

input:
IHUBTALENT

output:
ihubtalent

ex:

class Test
{
public static void main(String[] args)
{
String str="IHUBTALENT";

str=str.toLowerCase();

System.out.println(str);
}
}

Q) Write a java program to convert lowercase string to uppercase ?

input:
ihubtalent

output:
IHUBTALENT

ex:

class Test
{
public static void main(String[] args)
{
String str="ihubtalent";

str=str.toUpperCase();

System.out.println(str);
}
}

Q) Write a java program to remove special characters from given string?

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);
}
}

Q) Write a java program to remove the spaces from string?

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);
}
}

Q) Write a java program to concatinate two strings?

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]",""));

String word = word1 + word2;

int num = num1 + num2;

System.out.println(word+num);
}
}

Q) Write a java program to display the string in reverse order?

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

You might also like