Interview Question: How many String Objects will be created? - String Objects & String Constant Pool

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024
  • ~~~Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
    www.youtube.co...
    Follow me on my Facebook Page:
    / naveenqtpexpert
    Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:
    t.me/joinchat/...
    Naveen AutomationLabs Paid Courses:
    Java & Selenium:
    naveenautomatio...
    Java & API +POSTMAN + RestAssured + HttpClient:
    naveenautomatio...

ความคิดเห็น • 90

  • @sanghamitrabhowmicklive3013
    @sanghamitrabhowmicklive3013 3 ปีที่แล้ว +13

    How can someone explain with this level of clarity. I am amazed by your explanation skills, you are just the best👏

  • @SarangHoley
    @SarangHoley 3 ปีที่แล้ว +3

    With 10 mins a very important question got covered and for the same people will take sometimes ½ hour session, Thanks Naveen for the video 👍😊

  • @rukhma9307
    @rukhma9307 2 ปีที่แล้ว +3

    By far the best explanation of String objects!

  • @barkhachetwani9735
    @barkhachetwani9735 3 ปีที่แล้ว +2

    Hello naveen... Thanku very much... I have worked as a manual tester... But only becoz of u i got a job in mnc as a automation tester.....

  • @pachimalaanu1302
    @pachimalaanu1302 2 ปีที่แล้ว +1

    Able to understand very clear. Really appreciating for your efforts. Thank you sir

  • @spaininsights
    @spaininsights 3 ปีที่แล้ว +4

    These interview questions series is really very helpful. Thanks a lot sir.

  • @sivaswamy6923
    @sivaswamy6923 2 ปีที่แล้ว

    Thanks Naveen....... People with zero knowledge on Java can understand your explanation from basix.. You are different

  • @deepchand517
    @deepchand517 2 หลายเดือนก่อน

    Very best explanation very nice concept I got concept from this video thanks..

  • @JainNamit
    @JainNamit 3 ปีที่แล้ว +4

    Love these interview question series.

  • @kavishautomationfactory170
    @kavishautomationfactory170 3 ปีที่แล้ว +1

    New series is started. Thanks Naveen. Please upload more videos.

  • @RobinsonMartinezEngineer
    @RobinsonMartinezEngineer ปีที่แล้ว

    Hello Naveen! In min 5:00. There is something that makes me confused. After creating the 3 objects using new key word you create 2 literals that already exist in String pool then will not be necessary to create 2nd "Hello World" in the pool so at the end it will be 3 objects no 4 in the entire program as you mentioned. In 2 different programs the total of objects are 4 but not in only. Actually, in your next example you dont create a extra "Hello World" because already exist in the pool. Please, correct me if I am wrong.

  • @namitharose9231
    @namitharose9231 2 ปีที่แล้ว

    Such a beautiful explanation...sir,please can you explain how many objects will be created if we concat 2 strings

  • @jayantamandal9732
    @jayantamandal9732 3 ปีที่แล้ว +2

    Thanks a Lot for taking up this topic. Concept is clear now

  • @MadirajuKChaitanya
    @MadirajuKChaitanya 3 ปีที่แล้ว +1

    Naveen Ji, Thank you for sharing this information...👍👍🙏🙂

  • @artimandale8286
    @artimandale8286 7 หลายเดือนก่อน

    Amazing explanation, Naveen 😊 really hats off to you

  • @dp_prajapati
    @dp_prajapati 3 ปีที่แล้ว

    Hi Naveen, I really love the way you explain the stuff and concepts.
    Wanted to clarify one thing with you..
    When we create an object using new keyword, it will create an object in heap but does it create the object in String pool as well?
    Since there is no reference pointing to object created in StringPool then why will it create the object in StringPool?
    Will it not lead to poor memory management?
    As per my understanding, intern method takes care of doing that if it is not already available in scp.

  • @vivekguptacs
    @vivekguptacs 3 ปีที่แล้ว +1

    Sir
    Have a question.
    In java program
    If ( n1.hascode()==n2.hascode()) return true
    n1.equals(n2) true
    Then why n1==n2 return false. ( Its satisfy hascode and equals contract)

  • @RajeshChilkamari
    @RajeshChilkamari 3 ปีที่แล้ว +1

    Great series again Naveen!! Very helpful!! Thanks a lot!!

  • @shankarcheerala3089
    @shankarcheerala3089 2 ปีที่แล้ว

    it will create 2 objects
    one in heap area and
    another one is string constant pool area i.e SCP area(if the object is not there with the same content if object is already with same content i won't create new object) and string ref n1 is pointing to heap area object

  • @dowlathbashag65
    @dowlathbashag65 3 ปีที่แล้ว +3

    Naveen 4:47 you said 4 objects created ...the same but here 8:49 you telling 3 objects. It quite confuse. Please clarify. Your videos are awesome thanks for your effort....

    • @naveenautomationlabs
      @naveenautomationlabs  3 ปีที่แล้ว

      Both were different examples.

    • @dowlathbashag65
      @dowlathbashag65 3 ปีที่แล้ว

      @@naveenautomationlabs Thanks Naveen for the prompt response.

    • @RobinsonMartinezEngineer
      @RobinsonMartinezEngineer ปีที่แล้ว

      ​​@@naveenautomationlabs Naveen, even though are different examples. On the first example I guess only 1 "Hello World" should be created in the pool. Isn't? Why create another "Hello World" in pool if already exist? The second example makes me more sense. Please, let me know why you create 2 "Hello World in the pool in the first example and only 1 in the second?

  • @bhavikapatel7421
    @bhavikapatel7421 3 ปีที่แล้ว +1

    Thank u Naveen Sir. Its's really cleared and helpful. :)

  • @snehaparekh8502
    @snehaparekh8502 3 ปีที่แล้ว +1

    Incredible job naveen, thank you very much 🙏

  • @atulchavan4330
    @atulchavan4330 3 ปีที่แล้ว +1

    Wow ! that was an amazing explanation. Nobody does it better ;)

  • @codewithcuriosity-cwc3155
    @codewithcuriosity-cwc3155 2 ปีที่แล้ว

    Thanks a lot it's very helpful

  • @deepchand517
    @deepchand517 หลายเดือนก่อน

    Thanks sir I got more clarification from this video❤❤❤

  • @ritikajoura5995
    @ritikajoura5995 3 ปีที่แล้ว +1

    Hi Naveen, 1 Query, On creating 2 string objects with new keyword and same string (ex: new ("Hello") and new ("Hello"), it will create 2 objects in Heap but how many will be there in SCP ? 1 or 2 ? Also and if there is already a string "Hello" present in SCP then will these statements create new String in SCP or will point to the existing one only?

    • @ipsitapriyadarshini5707
      @ipsitapriyadarshini5707 3 ปีที่แล้ว

      Only 1 Hello object create in SCP.. .. and not pointing in SCP object until literals not given... (Without new operator) ex - string s1 = "hello"
      On this time object is pointing in SCP... as s1.

    • @AshishDubeyash4you
      @AshishDubeyash4you ปีที่แล้ว

      It will create only one in SCP. But if in scp there is already "Hello" present then it won't create in SCP

  • @jenishmoses
    @jenishmoses 3 ปีที่แล้ว +2

    String st = new String("Hello");
    String s = "Hello";

    System.out.println(st == s); //false
    Can you please clarify this, if the first statement creates an object on heap and also in string pools, then when why st == s is false, instead of true?
    also can you please explain the use of String.intern() method.

    • @boyapatipavankumar2088
      @boyapatipavankumar2088 ปีที่แล้ว

      when you create Sting by using New keyword it create a new Sting in Non-Constant pool memory(Heap) ,where as by using Literal it create a sting in constant pool memory(Heap). so that references of both stings are different that the reason you get "false".instead of using == operator use equals() you will get true

  • @vinith2320
    @vinith2320 3 ปีที่แล้ว +1

    Thanks Man👍
    I really appreciate your effort
    Keep uploading more like this

  • @SachinSharma-fr1vb
    @SachinSharma-fr1vb 2 ปีที่แล้ว

    @2:10 one string object with literal as "new world" gt created without reference inside SCP. string objects present in SCP if not have reference variable are not applicable for garbage collection because a reference internally maintained by JVM.Is my understanding correct or not? Please clear my doubt.

  • @MyKalps
    @MyKalps 3 ปีที่แล้ว +1

    thank you very much naveen ...

  • @chinmaypawtekar4054
    @chinmaypawtekar4054 3 ปีที่แล้ว +1

    Subscribed and liked.... Informative video... 👍

  • @shankardayal6928
    @shankardayal6928 3 ปีที่แล้ว

    @naveen , is it possible to count number of objects created in program? This is an question asked from me

  • @thalapathyprasad9511
    @thalapathyprasad9511 3 ปีที่แล้ว +1

    Great explanation 👏

  • @rohitkumark2087
    @rohitkumark2087 3 ปีที่แล้ว

    Thank you ,awesome explanation..

  • @rizwanshahid1447
    @rizwanshahid1447 3 ปีที่แล้ว

    thanks Naveen

  • @drhattyxd-s_15fps2
    @drhattyxd-s_15fps2 3 ปีที่แล้ว +1

    brother can you make a video on how to build graphic library in c/c++
    from scratch

  • @sreevidyarejeesh8695
    @sreevidyarejeesh8695 3 ปีที่แล้ว +1

    Love this series👍

  • @metronadsouza506
    @metronadsouza506 3 ปีที่แล้ว +4

    String str1 = new String("Hello World");
    String str2 = "Hello World";
    if(str1 == str2)
    System.out.println("Strings str1 and str2 are equal");
    else
    System.out.println("Strings str1 and str2 are not equals");
    //output : Strings str1 and str2 are not equals
    /* If the new String causes the string to be added to the string pool (as you claim) then the next if statement
    * should return true...which it does not ...
    * When you create a string using new ...yes its created in the heap but it does not get
    * added to the string intern pool unless actually added
    *
    * Look at the next e.g. and checkout the difference for yourself
    */
    String str3 = new String("Blue Sky").intern();
    String str4 = "Blue Sky";
    if(str3 == str4)
    System.out.println("Strings str3 and str4 are equal");
    else
    System.out.println("Strings str3 and str4 are not equals");
    //output : Strings str3 and str4 are equal
    /* In order to add a string created using new to the string intern pool is to specifically add it...as in case of str3
    * str4 is a string literal and has the reference of str3 - since str3 was in the string intern pool
    *
    */

  • @vivekmit
    @vivekmit ปีที่แล้ว

    String s = new String("New World").intern(); what will be the difference after adding intern() with new String? How many object created and what is the use case or purpose to use intern()

  • @adeebatariq6200
    @adeebatariq6200 2 ปีที่แล้ว

    Amazing!! Loved it!

  • @luckykodam6615
    @luckykodam6615 3 ปีที่แล้ว +2

    Awesome Naveen! Thank you for the interview series helping a lot..👍

  • @sureshisoft4555
    @sureshisoft4555 3 ปีที่แล้ว

    Super Naveen

  • @nishikantwairkar6866
    @nishikantwairkar6866 3 ปีที่แล้ว

    Amazing series

  • @SumitSharma-qq4hk
    @SumitSharma-qq4hk 3 ปีที่แล้ว +1

    Very helpful 👍...

  • @sridharlanka2721
    @sridharlanka2721 3 ปีที่แล้ว

    Please Clarify the Sequence of Memory allocation for Object and Reference in
    SPM , HEAP and STACK

  • @vishalavishala1883
    @vishalavishala1883 3 ปีที่แล้ว +4

    Total 3, from string literals 1object + 2more objects.

  • @tsara1395
    @tsara1395 3 ปีที่แล้ว +1

    Could u make interview questions on business analysts junior.

  • @visualart8609
    @visualart8609 2 ปีที่แล้ว

    Thank you sir. I have given wrong answer in an interview, now i realised what is the actual concept

  • @sphanindra5981
    @sphanindra5981 2 ปีที่แล้ว

    Navven Please tell me how to solve this type of problems like input String s="Tommorow", output="T$mm$$r$$$w". another one is input=2**7***9, and output is =23578279

  • @nishigandhasawant2632
    @nishigandhasawant2632 3 ปีที่แล้ว

    It will be create 2 objects .one create in heap memory and other create in string pool

  • @hareeshc9732
    @hareeshc9732 3 ปีที่แล้ว

    Hi Naveen API automation Testing using bdd cucumber framework please upload the videos

  • @ilavarasansriraman4140
    @ilavarasansriraman4140 3 ปีที่แล้ว +1

    Add these Java programs in new playlist

  • @mayankvarshney6014
    @mayankvarshney6014 3 ปีที่แล้ว

    while using string literals no Object is Create, but at video playback of 9:20 you are using String literal and saying one Object will be created. in the SCP. Since we are not using new keyword how can the Object be created. Correct me if I am wrong.?

    • @naveenautomationlabs
      @naveenautomationlabs  3 ปีที่แล้ว

      Well this understanding of yours is wrong. When you create string with literals, it will be created inside the string pool only. When you use with new keyword, it will create two objects, one is in pool and one is in heap.

    • @mayankvarshney6014
      @mayankvarshney6014 3 ปีที่แล้ว

      @@naveenautomationlabs So if I create a single line String S1 = "new world". Will this line create any object it not.

    • @naveenautomationlabs
      @naveenautomationlabs  3 ปีที่แล้ว +1

      It will create one string object as "new world" in string pool referred by S1 variable.

  • @gowrisankarsonti809
    @gowrisankarsonti809 3 ปีที่แล้ว

    0:29 -- one object is created

  • @PAVANKS-ct1iw
    @PAVANKS-ct1iw 3 หลายเดือนก่อน

    what if String n1 = new String("Hello World");
    String n2=new String("My World");
    Now how many objects are created

  • @vicsgarg6127
    @vicsgarg6127 3 ปีที่แล้ว +1

    why in case when we declare object first and then string literal it create total three objects not 2 in your first expression

  • @debaprasadsahoo3707
    @debaprasadsahoo3707 3 ปีที่แล้ว

    public class StrObjectCreation {
    public static void main(String[] args) {
    String s1 = new String("You cannot change me");
    String s2 = new String("You cannot change me");
    System.out.println(s1 == s2);

    String s3 = "You cannot change me";
    System.out.println(s1 == s3);

    String s4 = "You cannot change me";
    System.out.println(s3 == s4);

    String s5 = "You cannot "+"change me";
    System.out.println(s4 == s5);

    String s6 = "You cannot ";
    String s7 = s6+"change me";
    System.out.println(s4 == s7);

    final String s8 = "You cannot ";
    String s9 = s8+"change me";
    System.out.println(s4 == s9);
    }
    }
    Please check.

  • @talog4102
    @talog4102 2 ปีที่แล้ว +1

    Why String doesn't use scp only

  • @shambhand5556
    @shambhand5556 2 ปีที่แล้ว

    Could you please prove how two objects are created when we just write String n1 = new String("Hello World");`

  • @syedafarzana960
    @syedafarzana960 3 ปีที่แล้ว

    Ossom sir

  • @riteshranjanmishra967
    @riteshranjanmishra967 3 ปีที่แล้ว

    Two object. One is Heap and other in SCP
    For second case 3 objects

  • @sriyanshjain6645
    @sriyanshjain6645 3 ปีที่แล้ว

    Your telegram invite link expire in About section of your channel can you please send me invite link please

  • @gametimewitharyan6665
    @gametimewitharyan6665 3 ปีที่แล้ว

    0:22
    2 Objects As The 'String' Class Is Used 2 Times In The Line,
    1st in String n1,
    2nd in new String("Hello World")

  • @rockyrajeshyt6658
    @rockyrajeshyt6658 3 ปีที่แล้ว +1

    1st comment good afternoon sir

  • @himanshupokhriyal6883
    @himanshupokhriyal6883 ปีที่แล้ว

    Output->Only 1 string object

  • @sriyanshjain6645
    @sriyanshjain6645 3 ปีที่แล้ว

    Naveen can you please send me your telegram channel link

  • @vivekguptacs
    @vivekguptacs 3 ปีที่แล้ว

    Anybody help me to give answer of below scenario.
    If first statement is
    String str1 = new String(" hello");
    String str2 = " hello"
    In this case how many literal will be present in stringpool
    If answer is 2 .then why str2 didn't use already existing hello which was created first time.

    • @naveenautomationlabs
      @naveenautomationlabs  3 ปีที่แล้ว

      Only one literal in pool. And str2 will refer to "hello" from string pool. str1 will point to new object in heap.

    • @naveenautomationlabs
      @naveenautomationlabs  3 ปีที่แล้ว

      @UCM5nE03DyluGKZwJ5w75Nzw there are no two "Hello World" in string pool in the video at 3:45. One is "New World" and another one is "Hello World". Please watch it once again.

  • @nishantlaxkar7451
    @nishantlaxkar7451 2 ปีที่แล้ว

    At 5:45 3 object will be created

  • @raushantiwari2865
    @raushantiwari2865 3 ปีที่แล้ว +1

    2 one in heap and one in scp

  • @fareensayyad3147
    @fareensayyad3147 3 ปีที่แล้ว +1

    2 object

  • @yogender8094
    @yogender8094 3 ปีที่แล้ว +1

    It will create 2 object

  • @pedurujagadish5048
    @pedurujagadish5048 2 ปีที่แล้ว

    One object

  • @ashutoshsingh-uh4io
    @ashutoshsingh-uh4io 3 ปีที่แล้ว

    2

  • @sivashankar-ix4df
    @sivashankar-ix4df 3 ปีที่แล้ว

    1 object

  • @rockyrajeshyt6658
    @rockyrajeshyt6658 3 ปีที่แล้ว

    2 objects

  • @aadeshtanekar2059
    @aadeshtanekar2059 3 ปีที่แล้ว

    one

  • @naveenkumarsingh1900
    @naveenkumarsingh1900 3 ปีที่แล้ว +1

    2 object