File tree 4 files changed +122
-0
lines changed
4 files changed +122
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class EqualsImplementation {
2
+ public boolean equals (Object anObject ) {
3
+ if (this == anObject ) {
4
+ return true ;
5
+ }
6
+ if (anObject instanceof String ) {
7
+ String anotherString = (String ) anObject ;
8
+ int n = value .length ;
9
+ if (n == anotherString .value .length ) {
10
+ char v1 [] = value ;
11
+ char v2 [] = anotherString .value ;
12
+ int i = 0 ;
13
+ while (n -- != 0 ) {
14
+ if (v1 [i ] != v2 [i ])
15
+ return false ;
16
+ i ++;
17
+ }
18
+ return true ;
19
+ }
20
+ }
21
+ return false ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ class FinalizeMethod {
2
+ public static void main (String [] args ) throws Throwable {
3
+ FinalizeMethod obj = new FinalizeMethod (); // 1st object
4
+ obj = null ;
5
+ // obj.finalize(); // Uncaught checked exception
6
+ // obj = new FinalizeMethod(); // 1st object becomes orphan and 2nd object is
7
+ // created
8
+ // try {
9
+ // obj.finalize(); // Uncaught checked exception
10
+ // } catch (Throwable e) {
11
+ // e.printStackTrace();
12
+ // }
13
+ System .out .println (obj ); // Print 2nd object details
14
+ System .gc (); // Ignores all unhandled exceptions in finalize()
15
+ System .out .println ("Main completed" );
16
+ // obj = null; // 2nd object becomes orphan
17
+ }
18
+
19
+ void disp () {
20
+ System .out .println ("My message" );
21
+ }
22
+
23
+ @ Override
24
+ protected void finalize () throws Throwable {
25
+ // throw new java.io.IOException();
26
+ // System.out.println(10 / 0); // ArithmeticException (unchecked)
27
+ try {
28
+ throw new java .io .IOException (); // IOException (checked)
29
+ // System.out.println(10 / 0); // ArithmeticException (unchecked)
30
+ } catch (Exception e ) {
31
+ System .out .println (e );
32
+ // e.printStackTrace(); // Creates an uncaught exception (error)
33
+ } finally {
34
+ System .out .println ("Finally inside finalize" );
35
+ }
36
+ System .out .println ("Finalize method of FinalizeMethod class invoked" );
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ public class IslandOfIsolation {
2
+ IslandOfIsolation i ;
3
+
4
+ public static void main (String [] args ) {
5
+ IslandOfIsolation obj1 = new IslandOfIsolation ();
6
+ IslandOfIsolation obj2 = new IslandOfIsolation ();
7
+
8
+ obj1 .i = obj2 ;
9
+ obj2 .i = obj1 ;
10
+
11
+ obj1 = null ;
12
+ obj2 = null ;
13
+
14
+ System .gc ();
15
+
16
+ System .out .println ("Main completed" );
17
+ }
18
+
19
+ protected void finalize () throws Throwable {
20
+ System .out .println ("Finalize method invoked" );
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ public class StringIntern {
2
+ public static void main (String [] args ) throws Throwable {
3
+ String s1 = new String ("Java" );
4
+ String s2 = "Java" ;
5
+ String s3 = "Java" ;
6
+ String s4 = s2 .intern (); // String s4 = "Java";
7
+
8
+ System .out .println (s1 == s2 );
9
+ System .out .println (s3 == s2 );
10
+ System .out .println (s3 == s1 );
11
+ System .out .println (s4 == s2 );
12
+ System .out .println (s4 .equals (s2 ));
13
+
14
+ s2 = "C++" ;
15
+ System .out .println (s2 );
16
+ s4 = "C++" .intern (); // s4 = s2;
17
+ System .out .println (s4 );
18
+ System .out .println (s4 == s2 );
19
+
20
+ String str = "null" ;
21
+ if (str != null ) {
22
+ if (!str .isEmpty ()) {
23
+ System .out .println (str );
24
+ } else {
25
+ System .out .println ("Invalid email" );
26
+ }
27
+ } else {
28
+ System .out .println ("Please provide email address" );
29
+ }
30
+
31
+ String s = String .format ("|%10d|" , 1234 );
32
+ System .out .println (s );
33
+
34
+ byte [] b = s2 .getBytes ();
35
+ for (int i = 0 ; i < b .length ; i ++) {
36
+ System .out .println (b [i ]);
37
+ }
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments