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 8066067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:51:44+00:00 2026-06-05T11:51:44+00:00

I am new to Java and I have some questions in mind regarding object

  • 0

I am new to Java and I have some questions in mind regarding object assignment. For instance,

Test t1 = new Test();
Test t2 = t1;
t1.i=1;

Assuming variable i is defined inside Test class, am I right to assume both t1 and t2 point to the same object where the modification t1.i=1 affects both t1 and t2? Actually I tested it out and seems like I was right. However when I try the same thing on String, the modification happens only on one side where the other side is unaffected. What is the reason behind this?

Edit: The case I tried with String.

String s1 = "0";
String s2 = s1;          
s1 = "1";
System.out.println(s1);
System.out.println(s2);

I realise my mistake by testing the cases on String since it is immutable. The situation where I thought s1="1" modify the string is in fact returning the reference of “1” to the s1. Nevertheless, my question remains. Does Test t2 = t1; cause both t2 and t1 point to the same object or each now have their own objects? Does this situation applies on all objects on Java?

  • 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-06-05T11:51:47+00:00Added an answer on June 5, 2026 at 11:51 am

    You are right, but Strings are a special case; they are immutable and act like primitives in this case.

    @newacct

    I quote http://docs.oracle.com/javase/tutorial/java/data/strings.html :

    Note: The String class is immutable, so that once it is created a
    String object cannot be changed. The String class has a number
    of methods, some of which will be discussed below, that appear to
    modify strings. Since strings are immutable, what these methods really
    do is create and return a new string that contains the result of
    the operation.

    This is what makes strings a special case. If you don’t know this, you might expect the methods discussed in the quote not to return new strings, wich would lead to unexpected results.

    @user1238193

    Considering your following question: “Does Test t2 = t1; cause both t2 and t1 point to the same object or each now have their own objects? Does this situation applies on all objects on Java?”

    t1 and t2 will point to the same object. This is true for any java object (immutable objects included)

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

Sidebar

Related Questions

I have some general questions regarding the java.util.zip library. What we basically do is
I have some questions regarding ScalaTest: How can I ensure test-execution ORDER with ScalaTest,
I have some doubts/questions regarding the object creation. I have heard that objects should
I am new to java and I have some problem on Casting. I have
I'm new to Java, but have some OOP experience with ActionScript 3, so I'm
I am a bit new with android but I have some java experience. So
I am used to Java and new to QT/C++, and I have some problems
I'm new to Java EE and have been struggling with some basic middleware concepts
I am kind of new to C (I have prior Java, C#, and some
I have some C# experience, but im completly new to java. So i have

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.