-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDemo1.java
More file actions
183 lines (165 loc) · 3.98 KB
/
Demo1.java
File metadata and controls
183 lines (165 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
class IWantToPool{
int age;
String name;
static ArrayList<IWantToPool> pool = new ArrayList<>();
public static IWantToPool intern(int age, String name){
boolean isFound = false;
if(pool.size()==0){
IWantToPool obj = new IWantToPool(age,name);
pool.add(obj);
System.out.println("Pool is Empty and Now A Fresh Object is Added..");
return obj;
}
else
{
for(IWantToPool obj : pool){
if(obj.age==age && obj.name ==name){
System.out.println("This Object is Present in a Pool");
isFound=true;
return obj;
//break;
}
}
if(!isFound){
IWantToPool obj = new IWantToPool(age, name);
pool.add(obj);
System.out.println("New Object Added in a Pool");
return obj;
}
}
return null;
}
private IWantToPool(int age, String name)
{
this.age = age;
this.name = name;
}
}
final class MyImmutableClass{
private final int x;
private final int y;
public MyImmutableClass(int x , int y) {
this.x = x ;
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
public class Demo1 {
public static void main(String[] args) {
// LoadFactor 0.5
//IWantToPool pool[] = new IWantToPool[10];
MyImmutableClass obj = new MyImmutableClass(100, 200);
// it will create 0 or 1 Object in Memory
String name2 = "amit".intern();
String name = "amit"; // String Literal
// it will create 2 or 1 Object
String name3 = new String("ram");
String name4 = new String("amit");
name = "shyam";
Date date = new Date();
System.out.println(date);
String e = "amit".intern();
String e1 = "amit".intern();
if(e==e1){
System.out.println("Same Ref");
}
else
{
System.out.println("Not Same Ref");
}
String e2 = "ram".intern();
if(e==e2){
System.out.println("Same Ref");
}
else
{
System.out.println("Not Same Ref");
}
//IWantToPool obj2 = new IWantToPool(21,"Ram");
System.out.println("************************************");
IWantToPool obj3 = IWantToPool.intern(21, "Ram");
IWantToPool obj4 =IWantToPool.intern(21, "Ram");
IWantToPool obj5 =IWantToPool.intern(22, "Ramesh");
if(obj3 == obj4){
System.out.println("Same Ref");
}
else
{
System.out.println("Not Same Ref");
}
if(obj3==obj5){
System.out.println("Same Ref");
}
else
{
System.out.println("Not Same Ref");
}
/*
* String is a final class in Java
* String is Immutable Class
* String maintain String Pool
* String Internally as a Character Array
* String Methods
*/
String t = "amit";
System.out.println(t.length());
String t2 = t.toUpperCase();
System.out.println(t2 + " "+t);
System.out.println(t.toLowerCase());
if("amit".equals(t)){
System.out.println("Same Amit");
}
if("AMIT".equalsIgnoreCase(t)){
System.out.println("Same Amit");
}
String y = "Dr Ram Kumar";
if(y.startsWith("Dr")){
System.out.println("Hello Doctor ");
}
else
{
System.out.println("Hi");
}
if(y.endsWith("Kumar")){
System.out.println("Hello Kumar");
}
String email = "a_mi_t@gmail.com";
int index = email.indexOf("@"); // email.lastIndexOf("_");
if(index>=0){
System.out.println("Valid Email");
}
else
{
System.out.println("Invalid Email");
}
boolean result= email.contains("gmail");
email = email.replace("gmail", "yahoo");
index = 2;
int pos = 4;
String subStringValue = "amit".substring(index, pos);
System.out.println(subStringValue);
String myname = " amit srivastava ";
myname = myname.trim();
char array [] = myname.toCharArray();
Arrays.sort(array);
myname = new String(array);
System.out.println("Now After Sort "+myname);
char t3 = myname.charAt(0);
String r = ""+t3;
r.concat(String.valueOf(t3));
String msg = "Hello How are You";
String ar[] = msg.split(" ");
System.out.println("Word Count "+ar.length);
/*if(t!=null && t.equals("amit")){
System.out.println("Same Amit");
}*/
}
}