String - Yash-777/LearnJava GitHub Wiki
ASCII Codes Table ascii.cl
ASCII (Decimal) | Hex | ASCII Character |
---|---|---|
65 | 41 | A |
66 | 42 | B |
97 | 61 | a |
98 | 62 | b |
String[Aa], Hash[2112], System Hash[366712642]
String[BB], Hash[2112], System Hash[1829164700]
intern() « Before saving in String Constant Pool(SCP) it invokes intern() method to check object availability with same content in pool using equals method. If String-copy is available in the Pool then returns the reference. Otherwise, String object is added to the pool and returns the reference.
hash() « String @override hash() of Object class to generate same hash code for sequence of characters.
equals and ==
« ==
operator is to check the hash code where as equals()
method is to check the content of a String.
immutable «
What's the difference between length and length()
int[] myArray = new int[10];
String myString = "hello world!";
List<int> myList = new ArrayList<int>();
myArray.length //gives the length of the array
myString.length() //gives the length of the string
myList.size() //gives the length of the list
String reverse program.
public static String reverseStr(String str) {
//String str = "Yashwanth"; //https://stackoverflow.com/a/59166517/5081877
String collect = IntStream.range(0, str.length())
.boxed().sorted(Collections.reverseOrder())
.map(i -> {
System.out.println(i);
return String.valueOf(str.charAt(i));
})
.distinct().collect(Collectors.joining("")); //htnawsY
//.collect(Collectors.joining("")); //htnawhsaY
//.collect(Collectors.joining(".")); //h.t.n.a.w.h.s.a.Y
System.out.println("Reverse Str:"+collect);
return collect;
}
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
public String intern()
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
public class ReverseObject {
public static void main(String[] args) {
// String is immutable Object, once created we cannot change the object
//The SCP is an area inside the heap memory. It contains the unique strings.
//In order to put the strings in the string pool, one needs to call the intern().
String s1_scp = "Yash";
System.out.println("SCP intern():"+s1_scp.intern());
String s2_scp = "Yash";
System.out.println("SCP intern():"+s2_scp.intern());
String s3_h = new String("Yash");
String s4_h = new String("Yash");
System.out.println("Memory Address & Content :"+ (s1_scp == s2_scp));
System.out.println("Memory Address & Content :"+ (s3_h == s4_h));
System.out.println("Content :"+ s1_scp.equals(s2_scp) );
System.out.println("Content :"+ s3_h.equals(s4_h));
String str = "Yashwanth";
stringReverse(str, false);
stringReverse(str, true);
int num = 1234;
reverseNumberString(num);
reverseNumber(num);
StringBuffer buff = new StringBuffer(str);
System.out.println("String Buffer : "+ buff.reverse());
// Use List interface to reverse string.
char[] ch = str.toCharArray();
List<Character> reverse = new LinkedList<>();
for (int i = ch.length - 1; i >= 0; i--) {
reverse.add(ch[i]);
}
System.out.println("Reverse of a String with LIST : "+ reverse);
}
public static String stringReverse(String str, boolean isDistinct) {
char[] ch = str.toCharArray();
String reverse = "";
for (int i = ch.length - 1; i >= 0; i--) {
if ( isDistinct && !(reverse.indexOf(ch[i]) > -1) ) // Remove duplicates
reverse += ch[i];
if (!isDistinct) reverse += ch[i];
}
System.out.println("Reverse of a String: "+ reverse +", DISTINCT:"+isDistinct);
return reverse;
}
public static int reverseNumberString(int num) {
String str = new Integer(num).toString();
char[] ch = str.toCharArray();
String reverse = "";
for (int i = ch.length - 1; i >= 0; i--) {
reverse += ch[i];
}
System.out.println("Reverse of a String Number : "+ reverse);
return Integer.parseInt(reverse);
}
public static int reverseNumber(int num) {
int temp = num;
int rev = 0;
int sum = 0; // sumOfDigits
while (temp > 0 ) {
rev = (rev * 10) + temp % 10;
sum += temp % 10;
temp = temp / 10;
}
System.out.println("Reverse of a Number : "+ rev);
System.out.println("Sum of Digits of a Number : "+ sum);
if(rev == num) {
System.out.println("Polyndrome Number : [121 = 121]");
}
return rev;
}
}