Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6069043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:46:47+00:00 2026-05-23T09:46:47+00:00

class A { public void someMethod() { String var = Abhi; var = Abhishek;

  • 0
class A {



  public void someMethod() {
         String var = "Abhi";
         var = "Abhishek";
  }
}

Will the var variable store Abhi and Abhishek in different memory location or Abhishek overwrites Abhi ??

If i use this expression String var= new String();, then what?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:46:48+00:00Added an answer on May 23, 2026 at 9:46 am

    Given this method:

    public void someMethod() {
        String var = "Abhi";
        var = "Abhishek";
    }
    

    In this example there are 3 relevant things:

    • There’s a variable called var. This variable is stored on the stack (because it’s a local variable) and can hold the reference to some object.
    • There’s the String object with the content Abhi: it’s the first thing that var references right after its declaration.
    • There’s the String object with the content Abhishek: var is modified to reference this object in the second line where var is mentioned.

    So in respect to memory: the two String objects are stored in distinct spaces, unrelated to each other. var however, only ever occupies the same place (during a single invocation of foo, that is). So the reference to Abhi is overwritten with a reference to Abhishek.

    If you add the line var= new String(); at the end of that method, then that line would do two things:

    1. create a new String object representing the empty String and
    2. assign a reference to that new String object to the variable var.

    Note that there is almost never a reason to use that String constructor, because "" has (almost) the exact same effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public abstract class Vehicle { protected void SomeMethod<T>(String paramName, ref T myParam, T val)
public class BusinessObjects<O> where O : BusinessObject { void SomeMethod() { var s =
I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =
class MyClass { public: void method2() { static int i; ... } }; Will
class SimpleDelegate { public delegate void LogHandler(string message); public void Process(LogHandler logHandler) { if
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
import java.net.URL; import java.net.URLConnection; import java.sql.*; public class searchlink{ public static void main(String args[])
public class SomeClass { public void SomeMethod() { GridView gv = new GridView(this); gv.setOnClickListener(new
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
class test <T> where T : class { public void Write<T>() { Console.Write(typeof(T).FullName); }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.