CoreJava Notes_personal
CoreJava Notes_personal
Primitive datatypes are stored in STACK itself. Objects are stored in heap
Everything that starts with a capital letter is called Class. String s=”Pavani”;
String s1=”Pavani”;
s==s1 gives true.
s1=”Reddy”; works but s1 is now pointing to Reddy object not “Pavani”; we did not change Pavani to Reddy here
Strings are immutable because in string pool two or more ref variables can point to the same object and if one of them
changes the object the value would be changed. This can be against security so they made strings immutable in java.
String pool: Separate Memory Structure inside heap so that similar strings do not have to be recreated again.
When you only want to compare values then we can use .equals method .
a.equals(b)true
charAt(index)gives character.
System.out.println(56)o/p:56
System.out.println(“pavani”)o/p:pavani
this is
happening because of below toString function
So we use
we are telling java to use toString method in arrays . Now o/p would be [2,3,4,5] we did
function overriding here
O/P: 56
56 even though we did not use
num.toString just using num also it prints 56 because it calls valueOf function which futher
calls toString
we should use printf
here not println
o/p- :d
o/p:100
o/p:- Kunal[]
o/p:- Kunal56
error
because + operator only works for primitive datatypes and if atleast one of them is a string
In Java operator overloading is not supported unlike in C++ and python.
Since Strings are immutable in java if we change the value then the ref variable points to the
new object. We can use String Builder to avoid creation of new object and can make
changes to old object.
StringBuilder
No new object will be created every time we
add a new character.
Note that name.toLowerCase() does not modify the original object.
o/p:-[Kunal, Kushwaha]