== method → comparitor
When you only need to check value, use .equals method
String name1 = new String("kunal");
String name2 = new String("kunal");
system.out.println(name1.equals(name2)); // true
here it does not care whether the reference variable are pointing to same object or not it just care about the value.
So, we have to use a method called charAt()
System.outprintln(name.charAt(0));
// here out is a variable of type printstream
// printstream is a class, it has method in it called println
// Eg
fun () {
// code
} // ✗ function overloading
fun () {
// code
}
// Eg 2
fun (int a) {
// code
}
// ✓ It is allowed having different arguments with same function / method name
fun (int a, int b) {
// code
}
At compile time it decides which function to run.
float a = 453.1234f;
System.out.printf(" formatted number is %.2f " + a);
// printf → formatted string
// %.2f → place holder
// For π
System.out.printf(Math.PI);
// For string
System.out.printf("Hello my is %s but everybody calls me %s", "Giovanni Georgio", "Georgio");
// o/p → Hello my name is Giovanni Georgio but everybody calls me Georgio
Operator + ('Overloaded for string type')
System.out.println('a' + 'b'); // 195 - both char get converted to int
System.out.println("a" + "b"); // ab - these are in string type
System.out.println('a' + 3); // 100
System.out.println((char) ('a' + 3)); // d
// Note → when you're doing the addition with characters
// it converts into its value into a number
// then it use that number to solve the problem
System.out.println("a" + 1);
// interger will be converted to Integer that will call toString()
// Note → when an integer is concatenated (added)
// with a string it gets converted to its wrapper callse interger
// this is same as after a few steps: "a" + "1"
System.out.println("kunal" + new ArrayList<>()); // kunal[]
// System.out.println("kunal" + new Integer(46));
// ↑ gives error
// operator '+' cannot be applied to integer and arraylist.
// Eg
sout(new Integer(56) + " " + new Arraylist<>(););
↓ ↓ ↓
complex obj string type complex object
// here the entire result will be of string type
// o/p 56[]
So, on string objects the plus operator is being overloaded, because it concatenate more than one string
sout("a" + 'a');
// o/p aa
// If one of the data type is string ans will be string
This is important for invterview
public class Performance {
public static void main(String[] args) {
String series = "";
for (int i = 0; i < 26; i++) {
char ch = (char) ('a'+i);
series+=ch;
// this is actually creating new string object in every iterations
}
// so as this have time complexity of O(N^2) this is not efficient
// what's the better way ??
// where we can easily modify the string -- is there any such data type ??
// this is where string builder comes in equations and it is different class
System.out.println(series);
}
}
public class SBuilder {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder();
// this one is mutable
for (int i = 0; i < 26; i++) {
char ch = (char) ('a'+i);
builder.append(ch);
}
// so this comes with many methods which are self explainatory
// like builder.reverse() etc
System.out.println(builder.toString());
}
}
1 → .toCharArray() → It converts strings into array character
String name = "Giovanni Georgio";
System.out.println(Arrays.toString(name.toCharArray()));
// o/p ['G', 'i', 'o', 'v', 'a', 'n', 'n', 'i', ' ', 'G', 'e', 'o', 'r', 'g', 'i', 'o' ]
// Even that space will be counted
// .length() → It will provide you the length
// .toLowerCAse() → It will convert into lowercase
// It's not actually going to convert the original object because of Immutability
// .indexof → It will give the index value
// .lastIndexOf → It will give the last index value
// .strip() → While spaces are removed.
// .split() → add a regex first - after adding regex it will split it over those
public class Palindrome {
public static void main(String[] args) {
String str = "abcba";
System.out.println(isPalin(str));
}
static boolean isPalin(String str) {
if( str == null || str.length() == 0) {
return true;
}
str = str.toLowerCase();
// converts the string into lowercase
for (int i = 0; i < str.length()/2; i++) {
char start = str.charAt(i);
char end = str.charAt(str.length()-1-i);
if(start != end)
return false;
}
return true;
}
}