Jump to content

In java String is a class, which means that it object are called by references unlike that of java's primitive types. Can anyone explain why when you set a string to another string the process is like that of other primitve tyoes and not like that of other classes.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, DarkDragon2K04 said:

In java String is a class, which means that it object are called by references unlike that of java's primitive types. Can anyone explain why when you set a string to another string the process is like that of other primitve tyoes and not like that of other classes.

A String is a class likely because a string is really treated as an array of characters.

 

However its treated as a primitive type for assignment and likely other basic operations because we naturally expect a word plus a word equals those two words together. But a number plus another number doesn't equal ... well as an example 123 + 123 = 123123 makes no sense but "hello" + " " + "world!" = "hello world!" does.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, DarkDragon2K04 said:

Can anyone explain why when you set a string to another string the process is like that of other primitve tyoes and not like that of other classes.

It is the same as other classes. Given a simple custom class

class Vec2 {
  private int x;
  private int y;
  
  public Vec2(int x, int y) {
    this.x = x;
    this.y = y;
  }
  
  public int getX() { return x; }
  public int getY() { return y; }
}

you could write code like

Vec2 v = new Vec2(4, 3);
Vec2 otherV = v;
int resultV = doSomethingWithVec(v);

This is exactly like the ways that you use Strings

String s = "A string"; // sort of syntactic sugar for new String("A string"), although it is more nuanced than that to improve performance
String otherS = s;
int resultS = doSomethingWithString(s);

 

For some classes, you might use setters or mutator methods rather than creating a new instance, such as

  public void setX(int x) {
    this.x = x;
  }

This works well for some classes, but it can also introduce a bunch of extra issues if you're not careful. The Java designers, as well as the designers of most other languages, therefore decided to make strings immutable, which means that once you create a string there is no way for you to modify it without creating a new string. This allows for a bunch of optimisations to be performed by the JVM (Java backend), and prevents a bunch of accidental issues that could occur with mutable classes if you're not careful.

 

Strings do have a few syntactic sugar things, like string literals and using + to concatenate them, and these operations are not as simple as just calling regular methods behind the scenes because they take advantage of performance optimisations, but you can think of "a" + "b" as being the same as "a".concat("b").

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

In java, strings are called string literals. They are half way betweeb primitives and objects. From what i understand, JVM just keep resuing the same string object for efficeinty reasons. 

 

You do not need to worry about how JVM handles strings jsut as you do not need to worry about how Java garbage colletor works. The only thing important and relevant to you as a java programmer is this....

 

1) String are delcare like a primitive and are passed by value

e.g. 

 

Spoiler
 
 
 
Spoiler
o
Spoiler

public class Example {

    public static void main(String[] args){
        String x = "example";
        change(x);
        System.out.println(x);
    }

    public static void change(String x){
            x = "changed string";
    }
}

 

 

The above when being printed out will remain as "example"

 

 

2) Always use .equals as comparison operator. 

Edited by wasab
why is the spoiler tag so glitchy? never had this issue before.

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

On 5/22/2019 at 12:54 AM, wasab said:

From what i understand, JVM just keep resuing the same string object for efficeinty reasons. 

Yes, if you have two strings with the exact same data, both are references to the same object on the heap. But if you change a string, a new object is created and the reference is altered to that object.

String str = "ABC"; //Basic String
str += "A"; //New Object is created, old object exists until garbage collection

 

Link to comment
Share on other sites

Link to post
Share on other sites

On ‎5‎/‎21‎/‎2019 at 11:54 PM, wasab said:

In java, strings are called string literals. They are half way betweeb primitives and objects. From what i understand, JVM just keep resuing the same string object for efficeinty reasons. 

 

You do not need to worry about how JVM handles strings jsut as you do not need to worry about how Java garbage colletor works. The only thing important and relevant to you as a java programmer is this....

 

1) String are delcare like a primitive and are passed by value

e.g. 

 

  Reveal hidden contents
 
 
 
Spoiler
o
Spoiler


public class Example {

    public static void main(String[] args){
        String x = "example";
        change(x);
        System.out.println(x);
    }

    public static void change(String x){
            x = "changed string";
    }
}

 

 

The above when being printed out will remain as "example"

 

 

2) Always use .equals as comparison operator. 

Thank you very much. So thankful.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×