Skip to main content
Second iteration [<https://en.wiktionary.org/wiki/modify#Verb> <https://en.wiktionary.org/wiki/you#Pronoun>]. Removed historical information (that is what the revision history is for)—the answer should be as if it was written right now; see e.g. <meta.stackexchange.com/a/131011> (near "Changelogs").
Source Link
Peter Mortensen
  • 31.5k
  • 22
  • 110
  • 134

As far as I know, Java only knows call by value. This means, for primitive data types, you will work with ana copy, and for objects you will work with an copy of the reference to the objects. However, I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello,");
    StringBuffer s2 = new StringBuffer("World!");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate "Hello, World!" and not "World!" "Hello," because in the swap function you use copies which don't have any impact on the references in the main. But if your objects are not immutable, you can change it. For example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World!");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello,");
    appendWorld(s);
    System.out.println(s);
}

This will populate "Hello, World!" on the command line. If you change StringBuffer into String, it will produce just "Hello, " because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World!";
}

public static void main(String[] args) {
    String s = new String("Hello,");
    appendWorld(s);
    System.out.println(s);
}

However, you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World!";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello,");
    appendWorld(s);
    System.out.println(s.value);
}

Edit: I believe this is also the reason to use StringBuffer when it comes to "adding" two Strings, because you can modifiemodify the original object which uyou can't with immutable objects like String is.

As far as I know, Java only knows call by value. This means, for primitive data types, you will work with an copy, and for objects you will work with an copy of the reference to the objects. However, I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello,");
    StringBuffer s2 = new StringBuffer("World!");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate "Hello, World!" and not "World!" "Hello," because in the swap function you use copies which don't have any impact on the references in the main. But if your objects are not immutable, you can change it. For example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World!");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello,");
    appendWorld(s);
    System.out.println(s);
}

This will populate "Hello, World!" on the command line. If you change StringBuffer into String, it will produce just "Hello, " because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World!";
}

public static void main(String[] args) {
    String s = new String("Hello,");
    appendWorld(s);
    System.out.println(s);
}

However, you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World!";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello,");
    appendWorld(s);
    System.out.println(s.value);
}

Edit: I believe this is also the reason to use StringBuffer when it comes to "adding" two Strings, because you can modifie the original object which u can't with immutable objects like String is.

As far as I know, Java only knows call by value. This means, for primitive data types, you will work with a copy, and for objects you will work with an copy of the reference to the objects. However, I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello,");
    StringBuffer s2 = new StringBuffer("World!");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate "Hello, World!" and not "World!" "Hello," because in the swap function you use copies which don't have any impact on the references in the main. But if your objects are not immutable, you can change it. For example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World!");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello,");
    appendWorld(s);
    System.out.println(s);
}

This will populate "Hello, World!" on the command line. If you change StringBuffer into String, it will produce just "Hello, " because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World!";
}

public static void main(String[] args) {
    String s = new String("Hello,");
    appendWorld(s);
    System.out.println(s);
}

However, you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World!";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello,");
    appendWorld(s);
    System.out.println(s.value);
}

I believe this is also the reason to use StringBuffer when it comes to "adding" two Strings, because you can modify the original object which you can't with immutable objects like String is.

Active reading [<https://en.wiktionary.org/wiki/data_type#Noun> <https://en.wikipedia.org/wiki/%22Hello,_World!%22_program> <https://en.wiktionary.org/wiki/copy#Noun>]. In English, the subjective form of the singular first-person pronoun, "I", is capitalized.
Source Link
Peter Mortensen
  • 31.5k
  • 22
  • 110
  • 134

As far as I know, Java only knows call by value. This means, for primitive datatypesdata types, you will work with an copy, and for objects you will work with an copy of the reference to the objects. However, I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello""Hello,");
    StringBuffer s2 = new StringBuffer("World""World!");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate Hello"Hello, World!" and not World Hello"World!" "Hello," because in the swap function you use copyscopies which don't have noany impact on the references in the main. But if your objects are not immutable, you can change it for. For example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World"World!");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello""Hello,");
    appendWorld(s);
    System.out.println(s);
}

This will populate Hello"Hello, World!" on the command line. If you change StringBuffer into String, it will produce just Hello"Hello, " because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World";World!";
}

public static void main(String[] args) {
    String s = new String("Hello""Hello,");
    appendWorld(s);
    System.out.println(s);
}

However, you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World";World!";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello""Hello,");
    appendWorld(s);
    System.out.println(s.value);
}

editEdit: iI believe this is also the reason to use StringBuffer when it comes to "adding" two Strings, because you can modifie the original object which u can't with immutable objects like String is.

As far as I know, Java only knows call by value. This means for primitive datatypes you will work with an copy and for objects you will work with an copy of the reference to the objects. However I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello");
    StringBuffer s2 = new StringBuffer("World");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate Hello World and not World Hello because in the swap function you use copys which have no impact on the references in the main. But if your objects are not immutable you can change it for example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello");
    appendWorld(s);
    System.out.println(s);
}

This will populate Hello World on the command line. If you change StringBuffer into String it will produce just Hello because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World";
}

public static void main(String[] args) {
    String s = new String("Hello");
    appendWorld(s);
    System.out.println(s);
}

However you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello");
    appendWorld(s);
    System.out.println(s.value);
}

edit: i believe this is also the reason to use StringBuffer when it comes to "adding" two Strings because you can modifie the original object which u can't with immutable objects like String is.

As far as I know, Java only knows call by value. This means, for primitive data types, you will work with an copy, and for objects you will work with an copy of the reference to the objects. However, I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello,");
    StringBuffer s2 = new StringBuffer("World!");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate "Hello, World!" and not "World!" "Hello," because in the swap function you use copies which don't have any impact on the references in the main. But if your objects are not immutable, you can change it. For example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World!");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello,");
    appendWorld(s);
    System.out.println(s);
}

This will populate "Hello, World!" on the command line. If you change StringBuffer into String, it will produce just "Hello, " because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World!";
}

public static void main(String[] args) {
    String s = new String("Hello,");
    appendWorld(s);
    System.out.println(s);
}

However, you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World!";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello,");
    appendWorld(s);
    System.out.println(s.value);
}

Edit: I believe this is also the reason to use StringBuffer when it comes to "adding" two Strings, because you can modifie the original object which u can't with immutable objects like String is.

Post Made Community Wiki by Eng.Fouad
deleted 104 characters in body
Source Link
kukudas
  • 5k
  • 5
  • 47
  • 65

As far as I know, Java only knows call by value. This means for primitive datatypes you will work with an copy and for objects you will work with an copy of the reference to the objects. However I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello");
    StringBuffer s2 = new StringBuffer("World");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate Hello World and not World Hello because in the swap function you use copys which have no impact on the references in the main. But if your objects are not immutable you can change it for example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello");
    appendWorld(s);
    System.out.println(s);
}

This will populate Hello World on the command line. If you change StringBuffer into String it will produce just Hello because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World";
}

public static void main(String[] args) {
    String s = new String("Hello");
    appendWorld(s);
    System.out.println(s);
}

However you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello");
    appendWorld(s);
    System.out.println(s.value);
}

edit: i believe this is also the reason to use StringBuffer when it comes to "adding" two Strings because you can modifie the original object which u can't with immutable objects like String is. So i guess if you want to add two Strings with "+" a new object will be made "merging" the two Strings.

As far as I know, Java only knows call by value. This means for primitive datatypes you will work with an copy and for objects you will work with an copy of the reference to the objects. However I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello");
    StringBuffer s2 = new StringBuffer("World");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate Hello World and not World Hello because in the swap function you use copys which have no impact on the references in the main. But if your objects are not immutable you can change it for example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello");
    appendWorld(s);
    System.out.println(s);
}

This will populate Hello World on the command line. If you change StringBuffer into String it will produce just Hello because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World";
}

public static void main(String[] args) {
    String s = new String("Hello");
    appendWorld(s);
    System.out.println(s);
}

However you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello");
    appendWorld(s);
    System.out.println(s.value);
}

edit: i believe this is also the reason to use StringBuffer when it comes to "adding" two Strings because you can modifie the original object which u can't with immutable objects like String is. So i guess if you want to add two Strings with "+" a new object will be made "merging" the two Strings.

As far as I know, Java only knows call by value. This means for primitive datatypes you will work with an copy and for objects you will work with an copy of the reference to the objects. However I think there are some pitfalls; for example, this will not work:

public static void swap(StringBuffer s1, StringBuffer s2) {
    StringBuffer temp = s1;
    s1 = s2;
    s2 = temp;
}


public static void main(String[] args) {
    StringBuffer s1 = new StringBuffer("Hello");
    StringBuffer s2 = new StringBuffer("World");
    swap(s1, s2);
    System.out.println(s1);
    System.out.println(s2);
}

This will populate Hello World and not World Hello because in the swap function you use copys which have no impact on the references in the main. But if your objects are not immutable you can change it for example:

public static void appendWorld(StringBuffer s1) {
    s1.append(" World");
}

public static void main(String[] args) {
    StringBuffer s = new StringBuffer("Hello");
    appendWorld(s);
    System.out.println(s);
}

This will populate Hello World on the command line. If you change StringBuffer into String it will produce just Hello because String is immutable. For example:

public static void appendWorld(String s){
    s = s+" World";
}

public static void main(String[] args) {
    String s = new String("Hello");
    appendWorld(s);
    System.out.println(s);
}

However you could make a wrapper for String like this which would make it able to use it with Strings:

class StringWrapper {
    public String value;

    public StringWrapper(String value) {
        this.value = value;
    }
}

public static void appendWorld(StringWrapper s){
    s.value = s.value +" World";
}

public static void main(String[] args) {
    StringWrapper s = new StringWrapper("Hello");
    appendWorld(s);
    System.out.println(s.value);
}

edit: i believe this is also the reason to use StringBuffer when it comes to "adding" two Strings because you can modifie the original object which u can't with immutable objects like String is.

some additinal notes
Source Link
kukudas
  • 5k
  • 5
  • 47
  • 65
Loading
tidied up formatting
Source Link
Michael Myers
  • 192.6k
  • 47
  • 301
  • 297
Loading
Source Link
kukudas
  • 5k
  • 5
  • 47
  • 65
Loading