Is this true? I read the same somewhere else. Iam confused now. If by using new keyword also it refers to string pool or if the value is stored in string pool,then what is the purpose of using string intern() method.
Hai Navin Reddy, Please give us proper answer for String memory management and String is Immutable, In interview I told answer for string immutable As you taught in this video They rejected me in first round itself.
Creating New Strings Earlier we promised to talk more about the subtle differences between the various methods of creating a String. Let's look at a couple of examples of how a String might be created, and let's further assume that no other String objects exist in the pool: String s = "abc"; // creates one String object and one // reference variable In this simple case, "abc" will go in the pool and s will refer to it. String s = new String("abc"); // creates two objects, // and one reference variable In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.
Hi, what if we do not create str1 object and change str to "Reddy". Will it also create new string in string pool or will it change/replace Navin with Reddy? If first case is true then what will happen with Navin in string pool?
Due to string immutability you can safely return them from functions. If strings were mutable anyone could have changed your return value. Thats why serialization is done also.
owwhoo ! Telusko is always been great ! But I don't know how he had mistaken at 4:57 his new String("navin"); will create a new object on heap outside of the String Pool where 'navin' exists and this 'navin' is not that 'navin' from String Pool !
I've searched a little and I think the stuff goes like that: when you have String a = new String ("abc") it will be two objects created, one that is abc in string pool and one from new keyword in heap, the object from heap will point to the pool object for his value...i hope i understood well
string s1=new string("navin"); will this create a s1 variable inside main frame of stack? correct me if i'm wrong. what i have in mind is that { we have s1 inside stack filled with the memory location of heap memory now "new string" we have 4Bytes in heap memory filled with the address of "navin" in pool string am i right?
Sorry for saying.this is wrong when ever we create string using new keyword it will never pointing to string pool it will create new string in heap .if u want to put that value into sting pool there is a method called intern()
String s3 = new String("Shiva"); String s4 = "Shiva";
System.out.println(System.identityHashCode(s1) + " : "+System.identityHashCode(s2) + " : " + System.identityHashCode(s3) + " : " +System.identityHashCode(s4)); System.out.println(s3.equals(s2)); System.out.println(s3.equals(s4)); System.out.println(s3 == s4); System.out.println(s2 == s4); maybe this code can you help you get a bit of clarification, here both s3 has different memory location than s2 and s4 but its value is same.
class StringPoolTest1{ public static void main(String[] args){ String s0 = "hello ".concat("world"); } } "hello " and "world" are located in the String Pool is "hello world" located in it as well? If not, where is it? Only in the Heap? What happens between the Heap and the String Pool when s0.intern();
"Hello World" will be located in heap as you are using concat which is a runtime operation + both "hello" and "world" will be stored in string pool as well because both of them are constant values
Hello Navin, i come across the sentence that strings being immutable avoids race condition. I am not able to visualize this. Can you please explain it?
If String str="Navin" and let's say it has an address in heap as 100. Now as you said, If I do this like str="Reddy" this will assign a new address to str lets say 101. Now the question is both values are present in the heap memory but how can I now point the old str which has value "Navin"?
Hi sir, Nice explanation, but you have not clearly explained about, String ss = "navin"; ss="java"; then it will print "java" as string is immutable that all we know. but if we want ss="navin" then how to print because we have modified the ss="java"
I'm soo confused. If I can't change the value then, explain: string str = "navin"; string str = "freddy"; print(str); Why does it print "freddy" and not "navin". The value of the first string has been changed from navin to freddy, right? I need someone to clear me up.
Hi Navin, Can you explain the difference between primitive datatype int and String . as i see both works the same way except the fact they hold different content
Hi Navin, first of all thanks for this video but need more explanation on this topic. As string literal and new String both are creating immutable then what's difference , why we really need to use new String if String literal is creating immutable object?
what will happen in the below case String s1= "Navi"; s1+="n"; String s2 = "Navin"; Will s1 and s2 be sharing same reference or different. If it is different, does it mean + operator creating new Object in the heap.
Synchronized basically means thread-safe. A synchronized method or datatype is the one which will function as expected in cases of execution of multiple threads at a time. Now you'll ask that, then why there are non-synchronized methods. So, remember that non synchronized ones are faster than the synchronized ones. So when you know that your program does not involve multithreading, you can be sure to use a non-synchronized method or datatype..
@Satish Kavre bt here in question neither value nor reference are same.str1 and str2 both different references and point to different addresses ,we concatenate both and put the result in str1. Try to dry run the code!
There is no difference between this two: public static void main(String args[]){....} public static void main(String[] args ){....} you can even change the name of "args", you can write like this if you want to. public static void main(String mike[]){...} This is not correct, however: public static void main(String [args]){}
I was looking for a job since last month or I had been looking for a job from last two months. Hope that advertising girl in the end doesn't make any grammar mistakes next time.
I might be wrong but I don't thing "String" is colored in any IDE because it is a non-primitive data type and a Class and most IDEs either have primitive data types or keywords as colored
public class Example { public static void main (String[] args) { stringMatch("Haritha","Haritha"); } public static int stringMatch(String a, String b) { int len = Math.min(a.length(), b.length()); for (int i=0;i
both are same bro , infact both give you a reference variable of type str pointing to the string object that is "abc" both do same work with new or without it.
Main difference is when you create String str="abc"; it will create "abc" object in the String pool. but when you create String str = new String("abc"); it will create two objects one in String pool as "abc" and one in heap memory as (new String("abc"); this new String("abc") again its refers to the object "abc" in the string pool. that's why output will be the same for both the cases.
Based on what is explained, is there a difference? String str="abc"; creates an object in String Pool and returns a memory address to a reference. String str=new String("abc") creates an object in the Heap, but what kind of object it is, if it is holding only a memory address from the Heap ? Can you kindly explain? Thank you
Correct explanation, copied from below comment. Please refer this for String object creation with new keyword. whenever we create string using new keyword it will never pointing to string pool it will create new string in heap .if u want to put that value into sting pool there is a method called intern(). Please pin this answer to understand people correctly. String myString = new String( "old String" ); String myCache = myString; System.out.println( "equal: " + myString.equals( myCache ) ); System.out.println( "same: " + ( myString == myCache ) ); output: equal: true same: true String myString = new String( "old String" ); String myCache = myString.intern(); System.out.println( "equal: " + myString.equals( myCache ) ); System.out.println( "same: " + ( myString == myCache ) ); output: equal: true same: false
Both the objects "Navin" and "Reddy" exits in the string pool.when ever you assign str = "Reddy" its just map the str reference to "Reddy" object it won't delete "Navin" object from string pool.lets assume if you create a one more new reference variable as str1="Navin". it will not create "Navin" again its just find the "Navin" object in the string pool if its there its simply maps that reference to str1.
String s1 = "Hello"; String s2 = "Hello"; String s3 = new String ("Hello Java"); String s4 = new String("Hello Java"); How many objects will be created in this lines..?
Hi, Mahesh Meena I am also a beginner. But 3 will be my guess. // sop(a==b); //true Sop(a.equals(b));//true Sop(a.equals(c));//true Sop(a==c);//false Sop(c==d);//false Sop(c.equals(d));//true
Try it by your own is simple. Print in console the hashCode of each string. System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode()); System.out.println(s4.hashCode()); The output is: 69609650 69609650 387417328 387417328
The answer is 6, and hash codes will be equal if the values are equal, but it doesnt mean that those stored in the same object, two objects in different places in the heap can have the same value.
You are the best Java Instructor
I think when you do new String ("Navin") it will create a new object 103 - Navin in the heap memory. In the video it say it will refer to the same one
same doubt will the object be point to the memory address of string pool which having the string literal as "Navin"???
You are absolutely Right...
@@seanjesuharun1784 if any string is created using new keyword then for which a new string is created in heap memory , not in string pool..
Is this true? I read the same somewhere else. Iam confused now. If by using new keyword also it refers to string pool or if the value is stored in string pool,then what is the purpose of using string intern() method.
My question exactly.
String s=new String ("syam"); if you use s.intern(); then only it points to string pool until then it points to heap.
thank you sir fr teaching us . u r one of the great teacher ever@ navin reddy
Hai Navin Reddy, Please give us proper answer for String memory management and String is Immutable, In interview I told answer for string immutable As you taught in this video They rejected me in first round itself.
What they have asked you in interview regarding string can you tell me?
maybe its the Grammer!
@@namratasanger5745 same experience for me also..
Why String is immutable ?
Difference between
String str=String("abc") and
String str1="abc"
@@withlovesandeep4424 they both differ because of how they point and store the data isn't that's the right answer?
ajithran rox 😂😂
"Welcome back aliens" is immutable
I watch complete adds on your videos , so that you can earn some money .
😂
Hell is wa8ting
🤣🤣🤣🤣🤣
🤣
Good job man 😂😂😂
Amazing Telusko you are such a good teacher
Hi navin, whenever u teach any concept could you give a program code example it will useful to understand the concept easily
I cant find the app in play store. Wonderful videos. Crisp and clear explanation
@0:36 When Navin says "mutability is a crime"
Me: "It's true..Corona should have been a String(immutable)"
😂🥲
April: 2021
Funny
its "Navin"
@@anshsarin19 lol ok bigger problems here.. I'll correct that using String buffer 👍
😂
Creating New Strings Earlier we promised to talk more about the subtle differences between the various methods of creating a String. Let's look at a couple of examples of how a String might be created, and let's further assume that no other String objects exist in the pool:
String s = "abc"; // creates one String object and one // reference variable In this simple case, "abc" will go in the pool and s will refer to it.
String s = new String("abc"); // creates two objects, // and one reference variable In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.
Will a new 'abc' literal be placed in pool after calling new, or the previous 'abc' will be referenced?
Okay, 'abc' will be placed in pool if it doesn't exist. Else the previous 'abc' is referenced
Good work my dude
Sir can you please make a video on heap and stack memory along with a separate video on a roadmap for desktop application developers
UHR THE BEST TEACHER EVER
Thank you that makes a lot of sense
Hi, what if we do not create str1 object and change str to "Reddy". Will it also create new string in string pool or will it change/replace Navin with Reddy? If first case is true then what will happen with Navin in string pool?
Looking you drawing on board and now all animations... That's mutable... 😁
Due to string immutability you can safely return them from functions.
If strings were mutable anyone could have changed your return value.
Thats why serialization is done also.
I also faced name issue. People used to send or write to wrong Naveen. Nice video
Thanks for the video!
Consider getting a better mic!!
owwhoo ! Telusko is always been great ! But I don't know how he had mistaken at 4:57 his new String("navin"); will create a new object on heap outside of the String Pool where 'navin' exists and this 'navin' is not that 'navin' from String Pool !
He's a person, he can do a mistake, wright? ;) He learned us a lots of things for free, we should thank him...
Ofcourse. that's beyond dispute ! @toramihai
I've searched a little and I think the stuff goes like that: when you have String a = new String ("abc") it will be two objects created, one that is abc in string pool and one from new keyword in heap, the object from heap will point to the pool object for his value...i hope i understood well
@@toramihai Yes right ....i was also wondering the same ! and if we want to put a into pool we have to use a.intern() method.
True new keyword makes a new object
string s1=new string("navin");
will this create a s1 variable inside main frame of stack? correct me if i'm wrong.
what i have in mind is that { we have s1 inside stack filled with the memory location of heap memory
now "new string" we have 4Bytes in heap memory filled with the address of "navin" in pool string
am i right?
Well explained sir Thank you...!👌✨
something special with 101 i see it many times....
please correct that part sir about new String("Navin") creating a new object..:)
Sir I have been listening to your videos "Java Tutorial For Beginners".Will these videos cover the entire core java syllabus?
String constant pool is created in Method Area not heap area. We access it by using intern () method which create object in heap area of that string.
People were saying that was only true until JDK 7 in other videos. I haven't checked yet...
Sorry for saying.this is wrong when ever we create string using new keyword it will never pointing to string pool it will create new string in heap .if u want to put that value into sting pool there is a method called intern()
.if u want to put that value into heap than this method is called intern()
String myString = new String( "old String" );
String myCache = myString;
System.out.println( "equal: " + myString.equals( myCache ) );
System.out.println( "same: " + ( myString == myCache ) );
output:
equal: true
same: true
String myString = new String( "old String" );
String myCache = myString.intern();
System.out.println( "equal: " + myString.equals( myCache ) );
System.out.println( "same: " + ( myString == myCache ) );
output:
equal: true
same: false
Thank you Navin !!
Amazingly explained
5:19 What kind of object is that, that have a memory address. I thought only references could hold memory addresses.
String s1 = "Shiva";
String s2 = "Shiva";
s1 = "Neelkantha";
String s3 = new String("Shiva");
String s4 = "Shiva";
System.out.println(System.identityHashCode(s1) + " : "+System.identityHashCode(s2) + " : " + System.identityHashCode(s3) + " : " +System.identityHashCode(s4));
System.out.println(s3.equals(s2));
System.out.println(s3.equals(s4));
System.out.println(s3 == s4);
System.out.println(s2 == s4);
maybe this code can you help you get a bit of clarification, here both s3 has different memory location than s2 and s4 but its value is same.
Hmm good 🙂.. But in different videos, blogs different informations 😶 which confusing me 😐. This video make sense for me a bit more. Goog.
Hello sir, Can you please tell me wat does synchronization means in terms of stringbuilder and stringbuffer classes? An exmple wud be appricaited.
class StringPoolTest1{
public static void main(String[] args){
String s0 = "hello ".concat("world");
}
}
"hello " and "world" are located in the String Pool
is "hello world" located in it as well? If not, where is it? Only in the Heap?
What happens between the Heap and the String Pool when
s0.intern();
"Hello World" will be located in heap as you are using concat which is a runtime operation + both "hello" and "world" will be stored in string pool as well because both of them are constant values
I really really need this answer where did u learn java or string concept....pls reply..
Hello Navin, i come across the sentence that strings being immutable avoids race condition.
I am not able to visualize this. Can you please explain it?
naveen...sir
u r a dynamic beak...awesome.
This man is awesome.
Please create a video on stack and heap memory
Thanks Navin !
best ever explanation
If String str="Navin" and let's say it has an address in heap as 100. Now as you said, If I do this like str="Reddy" this will assign a new address to str lets say 101. Now the question is both values are present in the heap memory but how can I now point the old str which has value "Navin"?
I think it's not possible to point old str as reference updated with new one ...
same doubt to me
Hi Navin, I've one doubt. Is it possible to declare and define methods in java separately like C?
Easy to understand
Hi sir,
Nice explanation, but you have not clearly explained about,
String ss = "navin";
ss="java";
then it will print "java" as string is immutable that all we know. but if we want ss="navin" then how to print because we have modified the ss="java"
Are all primitive data types immutable or only String is immutable?
best explanation dude
Thanks Naveen
Nicely done
Thanks Naveen))
Thank you!
I'm soo confused. If I can't change the value then, explain:
string str = "navin";
string str = "freddy";
print(str);
Why does it print "freddy" and not "navin". The value of the first string has been changed from navin to freddy, right? I need someone to clear me up.
Hello Sir, if i create String s=new String("SVREDDY"); how many objects will create in memory.
2
2. One in heap and one in string pool. If this string already exists in string pool, it will not create new one.
navin sir are u able to speak telugu language??
Does it mean that if we create String object using 'new' keyword then it doesn't make any entry in Stack memory?
Please clarify this..
Hi Navin,
Can you explain the difference between primitive datatype int and String .
as i see both works the same way except the fact they hold different content
Both are not the same, int is a data type and String is class
@@gavravdhongadi9824 okay.. but the way they both store seems same.. except the fact one stores in stack and the other in heap.
So got the qn
@@gavravdhongadi9824 int is a struct.
int does not have methods, you can't find a size of an int primitive for example.
String, however, has methods.
I have some doubts in string immutablity
Hi Navin, first of all thanks for this video but need more explanation on this topic. As string literal and new String both are creating immutable then what's difference , why we really need to use new String if String literal is creating immutable object?
I think new string has some additional functionalities which string literal does not have like integer wrapper class has parseInt ect .
there is no app found???
in play store
Sir can u pls make a video on immutable and mutable objects I got this question in interview
what will happen in the below case
String s1= "Navi";
s1+="n";
String s2 = "Navin";
Will s1 and s2 be sharing same reference or different. If it is different, does it mean + operator creating new Object in the heap.
I also have the same doubt
great sir
salute you sir...love u
Can we take a string array without initialization nd then give inputs nd then calculate it's length?
okay. reference changed in stack(str 101 to 103) then now str is 103 . 103 is reddy. then it is changed so , how it is immutable in your explanation
what happen to string "navin" is it accssesible or it goes in garbage???
In above video, if i did not create another variable "str1", then i changed the"str = reddy" mean,, how can i access the "navin"
How string immutability boost the performance of a system?
What is synchronized & non-synchronized in java? For e.g Array List is not synchronized.
Synchronized basically means thread-safe. A synchronized method or datatype is the one which will function as expected in cases of execution of multiple threads at a time. Now you'll ask that, then why there are non-synchronized methods. So, remember that non synchronized ones are faster than the synchronized ones. So when you know that your program does not involve multithreading, you can be sure to use a non-synchronized method or datatype..
Lets say If I hv millions of text values are there. Don't you think it will create an hotspot in memory??
thank you
How string is secure???
If str1="hello" ;str2="hi";str1=str1+str2;sop(str1);what's the output?
Wrong questions Urmila patil mam pls check ur questions first
@Satish Kavre why you said question is wrong ,i think output will be hellohi
@abhishek singh I think u don't know if value of variables is same but there reference must be different
@Satish Kavre bt here in question neither value nor reference are same.str1 and str2 both different references and point to different addresses ,we concatenate both and put the result in str1.
Try to dry run the code!
@@AbhishekSingh-rc1st I think string is immutable Nd how can the value of str1 be changed? Am I wrong ?
What is the difference between
main(String args[ ]) , main(String[ ] args) & main(String [args]) ?
There is no difference between this two:
public static void main(String args[]){....}
public static void main(String[] args ){....}
you can even change the name of "args", you can write like this if you want to.
public static void main(String mike[]){...}
This is not correct, however: public static void main(String [args]){}
I was looking for a job since last month or
I had been looking for a job from last two months. Hope that advertising girl in the end doesn't make any grammar mistakes next time.
this is my interview question we cant manipulate string but we can assign a new value
Please explain that how the system knows or find to duplicate values assign to string in heap memmory
I searched this... Compiler will create list of all string literals at compile time with their respective address
sir would i like to improve your app...so plz give me Chance... I have 2 plus experience in android....just bcz it's Helpfull for students
Why " String " is not colored in java in eclipse editor....?
I might be wrong but I don't thing "String" is colored in any IDE because it is a non-primitive data type and a Class and most IDEs either have primitive data types or keywords as colored
This is not y ans can u give ans of why string is immutable not his benefits
How new string ("Naveen'); use string pool value explain
when declaring string like this
String str="navin";
whether str is an variable or an object
it is a reference variable not object ...object is navin
variable
It's call object reference variable which can be used to refer to a particular object
Its a variable...
public class Example {
public static void main (String[] args) {
stringMatch("Haritha","Haritha");
}
public static int stringMatch(String a, String b) {
int len = Math.min(a.length(), b.length());
for (int i=0;i
what is the difference between String str="abc"; and String str=new String("abc"),is it only that the later uses heap memory and the former does not?
both are same bro , infact both give you a reference variable of type str pointing to the string object that is "abc" both do same work with new or without it.
Main difference is when you create String str="abc"; it will create "abc" object in the String pool. but when you create String str = new String("abc"); it will create two objects one in String pool as "abc" and one in heap memory as (new String("abc"); this new String("abc") again its refers to the object "abc" in the string pool. that's why output will be the same for both the cases.
First one will be stored in code memory and 2nd one in heap
Based on what is explained, is there a difference? String str="abc"; creates an object in String Pool and returns a memory address to a reference. String str=new String("abc") creates an object in the Heap, but what kind of object it is, if it is holding only a memory address from the Heap ? Can you kindly explain? Thank you
I have one doubt please fix it sir
String s1="super"
String s2="cat"
S1=s1+s2
Is this immutable object
Pls reply
it would be better if you structure - I was not sure if you were explaining immutable or mutable.
If I ignore the str1 obj then how will the memory allocation happen
thanks
Sir u forgot to teach about constant pool and non constant pool...
kachak packk odkd
This is incomplete.Why didnt you tell how to make a class immutable?
string n=new string("arun");
string n1=new string("arun");
how many objects is created
3
still watchin in 2022
Wrong,Sir the new keyword will create object in heap area and String literal not equal or point to the String object
Correct explanation, copied from below comment. Please refer this for String object creation with new keyword.
whenever we create string using new keyword it will never pointing to string pool it will create new string in heap .if u want to put that value into sting pool there is a method called intern().
Please pin this answer to understand people correctly.
String myString = new String( "old String" );
String myCache = myString;
System.out.println( "equal: " + myString.equals( myCache ) );
System.out.println( "same: " + ( myString == myCache ) );
output:
equal: true
same: true
String myString = new String( "old String" );
String myCache = myString.intern();
System.out.println( "equal: " + myString.equals( myCache ) );
System.out.println( "same: " + ( myString == myCache ) );
output:
equal: true
same: false
You don't explain in detail when we create two string with new keyword then why there address are same and equels to return false
String str="Navin";
str="Reddy";
In this case String object (Navin) will be replaced By Reddy Or it will exist?
Both the objects "Navin" and "Reddy" exits in the string pool.when ever you assign str = "Reddy" its just map the str reference to "Reddy" object it won't delete "Navin" object from string pool.lets assume if you create a one more new reference variable as str1="Navin". it will not create "Navin" again its just find the "Navin" object in the string pool if its there its simply maps that reference to str1.
Does string object in Heap hold the reference of the object in String pool as navin said?
Sir how can I class learn tupple and array?
// an array
1)
String[] names = {"Bob", "John", "Mike"};
2)
String[] names = new String[3];
names[0] = new String("Bob");
names[1] = new String("John");
names[2] = new String("Alex");
gr8 m8
luv java
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String ("Hello Java");
String s4 = new String("Hello Java");
How many objects will be created in this lines..?
Hi, Mahesh Meena
I am also a beginner.
But 3 will be my guess.
//
sop(a==b); //true
Sop(a.equals(b));//true
Sop(a.equals(c));//true
Sop(a==c);//false
Sop(c==d);//false
Sop(c.equals(d));//true
Try it by your own is simple. Print in console the hashCode of each string.
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
System.out.println(s3.hashCode());
System.out.println(s4.hashCode());
The output is:
69609650
69609650
387417328
387417328
The answer is 6, and hash codes will be equal if the values are equal, but it doesnt mean that those stored in the same object, two objects in different places in the heap can have the same value.