• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

string buffer

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
query1:

class MWC202 {
public static void main (String[] args) {
StringBuffer sb1 = new StringBuffer("ABC");
StringBuffer sb2 = new StringBuffer("ABC");
System.out.print((sb1==sb2)+","+sb1.equals(sb2));
}}
answer is:false,false.

but string buffer is does not override equals , so it should have given a compile time error

please explain


Query 2:

class GFC304 {
static void m1(int[] i1, int[] i2) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}

will it swap the value ?if not then please explain
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question #1:


but string buffer is does not override equals , so it should have given a compile time error



StringBuffer inherits equals(...) method from the Object class as all
other Java classes do.


Question #2:

Changing the reference inside the called method would have local impact.
Instead if you change the value, or say it object state, it causes global impact.


Thanks,
[ July 08, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use StringBuilder instead of StringBuffer.
It is not synchronized & faster.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anshi kohli:


but string buffer is does not override equals , so it should have given a compile time error



Here Objetc's equal() method is being overriden. It will result in true when any non-null references x and y point to same object.
(x==y has the value true).

So, if you try to assign sb1 to sb2 then you will see the difference.


Query 2:

will it swap the value ?if not then please explain



No, it will not swap values. The changes are only local to method.
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
under what condition it will swap....??

class GFC304 {
static void m1(int[] i1, int[] i2) {
int[] i3 = i1; i1 = i2; i2 = i3;
}
public static void main (String[] args) {
int[] i1 = {1}, i2 = {3}; m1(i1, i2);
System.out.print(i1[0] + "," + i2[0]);
}}


can anyone explain
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


under what condition it will swap....??



It is actually swapping the references but since Java does work on "pass-by-value", the changes are NOT reflected back.

For further and strong information about Pass-by-value, please read the following links.

  • Pass By Value story - Read this first!
  • PBValue or PBRef Comprehensive Detail - Explanation by Chandra Bhatt in JavaRanch Thread
  • More on Java Pass-by reference, but not really! - Excellent article
  • Another JavaRanch thread with an example



  • HtH.
    [ July 08, 2007: Message edited by: Raghavan Muthu ]
     
    Ranch Hand
    Posts: 86
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    HI Anshi kohli
    for your query 2:
    you are passing the reference of i1 and i2 (main method variables) to the m1 method.
    so, now for the same array object({1}) two references are there. one is main method i1 and
    another one is m1 method i1(this is same for the i2 also).
    Theory is:
    One object ({1}) two refeneces (main method i1 and m1 method i1).
    In m1(),by the swaping, local i1 has impact(i.e. i1 of m1()),but main method i1 is still
    refering to array object {1} only. That means your swaping effects only to your i1 of method m1().
    The theory is:
    As Chandra Bhatt said,
    Changing the reference inside the called method would have local impact.

    If you change i1[0] to something like 2(i.e. i1[0]=2),means you are changing the state of the object,
    than it will effect main method i1 because both are refering to the same object.

    For this, the theory is:
    As again Chandra Bhatt said,
    Change to the state will effect the calling method also.


    hope you got it.

    regards
    Mallik Avula
     
    anshi kohli
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks for clering the doubt
     
    get schwifty. tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic