Effective value comparison of StringBuilder objects

Hi there,
I got questions in comments of the code. Can anyone help?
public class StringBuilderTest {
     public static void main(String[] args) {
          StringBuilder sb = new StringBuilder("abc");
          StringBuilder sb1 = new StringBuilder("abc");
          // only if sb = sb1 returns true, coz different object referenced
          if ( sb.equals(sb1)){
               System.out.println("equal value");
           * I think the following blocks both return true as a result but the second
           * block is better choice from a performance perspective.
           * Am I right? or wrong?
           * (hopefully, comparison at object level runs faster than char level)
          // two String objects are newly created and compared on char sequence basis
          if ( sb.toString().contentEquals(sb1.toString())){
               System.out.println("equal value");
          // two String objects are newly created and compared on object basis
          if ( sb.toString().equals(sb1.toString())){
               System.out.println("equal value");
           * Overall, the following is the best practice to do the comparison because of
           * reducing intermediate string object to one can improve performance
           * Am I right? or wrong?
          // one String object is newly created and compared against StringBuilder object
          // on char sequence basis
          if (sb.toString().contentEquals(sb1)){
               System.out.println("equal value");
}

Jay-K wrote:
Do you mean sb.toString().equals(sb1.toString)?
Because due to API specification, sb.equals() method is used to compare reference objects not the actual value of sb.Yes - you are right and my suggestion was rubbish.
The comment was about the two string versions. "sb.toString().equals(sb1.toString())" and "sb.toString().contentEquals(sb1.toString())" just do the same thing as far as I can see. These were your first two versions.
>
My question is if these two methods are the same, then why would we need duplicate methods in Java. The only difference that I can think of is the performance issue. Which is what motivated my question.The reason for a contentEquals() as well as an equals() for String seems to be so that you can write code like your third version. Basically contentEquals() is a method that deliberately allows for strings to be compared with (and be regarded as equal to) things that are not strings.

Similar Messages

  • How to change the value in an Integer object?

    Hi,
    Is it possible to change the value that is contained in an Integer object.
    I know Integer objects are immutable. So it might not be possible to chage to value in an integer object once its been initalized a value @ the time of construction.
    Also does autoboxing and unboxing feature of 1.5 help acheive this?
    Please let me know of any other alternative
    Thanks
    Deepak

    Tried the autoboxing and unboxing feature doesnt
    help.It doesn't help in general. But in this special case it doesn't help because it doesn't anything to do with it. Do you really know what you're doing?
    So across the function its not changing the value.
    As I have created an object of Integer class and
    passed a reference of that object into the chage()
    ,So any changes should have been reflected
    acrosss method calls.?Since you let the newly created parameter-reference a point to a new Integer object: no. Why? You have two references to I(31). Then you move one reference to I(33). Why should 31 get another value?
    Does the the java compiler creates a new Integer
    object each time it does autoboxing Not necessarily. Some values are pooled. Actualy, the JVM does it. The compiler never creates any object.
    so that the value
    is lost across method calls?That's not the compiler's or the JVM's fault. It's all a misconception of yours.
    Is there any means to achieve this?What for?
    int a = 0;
    a = change(a);
    int change (final int i) {
      return i + 12;
    }Does exactly what you want, without side-effects.

  • Value comparison on jsp page

    Hi
    I want to compare value stored in bean object with another value to show suitable message on page
    Eg
    if(#{bean.userstatus=="N"}){
    //show this
    }else{
    //show this
    Can anybody please tell me how to do comparison as examplified above
    Thanks

    Use the 'rendered' attribute. It accepts a boolean expression. If it evaluates to false, then the component simply won't be rendered in the component root, otherwise so.
    Here are some basic examples:<h:someComponent rendered="#{myBean.booleanValue}" />
    <h:someComponent rendered="#{myBean.intValue > 10}" />
    <h:someComponent rendered="#{myBean.objectValue == null}" />
    <h:someComponent rendered="#{myBean.stringValue != 'someValue'}" />
    <h:someComponent rendered="#{!empty myBean.collectionValue}" />
    <h:someComponent rendered="#{!myBean.booleanValue && myBean.intValue != 0}" />
    <h:someComponent rendered="#{myBean.stringValue == 'oneValue' || myBean.stringValue == 'anotherValue'}" />In your specific case, just do:<h:someComponent rendered="#{bean.userstatus == 'N'}" />
    <h:someComponent rendered="#{bean.userstatus != 'N'}" />

  • How can I create an iridescent effect in a Photoshop 3D object?

    How can I create an iridescent effect in a Photoshop 3D object? I have searched everywhere for downloadable option for a mac with no luck. I'm working with CS6.

    NO way. Such stuff isn't even commonly available in many 3D programs becaus it's actually pretty advanced shader stuff...
    Mylenium

  • How to add get Day value in a Date object?

    Hi,
    I am writing a sql statement that has two date values. One I am getting it from the database. The format in the database is MM/DD/YYYY. My first question is how do I convert the format into the java date format, YYYY-MM-DD. The second question is I need to find out what the day is and add 1 to it. How do I get Day value in a Date object?
    Thanks.

    Look at "SimpleDateFormat" and "parse" in the archives.

  • How do I get the value of a field object programatically after loading a report

    I'm using CR 11.5 and VS2005 in a console application, there is no GUI. I have a report with a page header that has 3 field objects. These field objects, their values change for every record and I want to be able to capture the value of these field objects in code for further processing outside of CR. Is this possible?
    Thanks in advance!
    I should explain a little more, the information I'm trying to obtain is in the Page Header section of the main report. There are subreports. Basically what I'm trying to put together is a list of how many pages are part of each record, the number of pages fluctuates per records so I need to be able to cycle through. Any other suggestions?

    When accessing the fields on the report through the SDK you will only get what database field they are using for data.  The internal data collection that is used by the report can be gathered using the rowsetcontroller from the ReportClientDocument.

  • How to dispay the text values of the info objects in my transformations

    Hi
    Can any one say how to do a lookup for a text table. For example for a particular customer number i need to get the customer name from the text table.In the same way i need to display the text values of the info objects in my transformations. It would be great if any one could send me the code.
    Thanks
    R

    Thanks for the immediate reply Anshul, Actually i am having the fields of short description and long description in my text table,  my requirement is to display only the long description data in the text table. So it would be great if you could get me the solution.
    Thanks
    R

  • Table for po effective value

    Hi,
    I need to develop a purchase order report, so I need the following
    gross price  : 100
    fright        : 10
    insurance : 10
    my net value is : 100 rs
    Effective value : 120 rs
    Where can I find ( table) for this effective value
    fright and insurance I can find from KONV table, But I need effective value or total value
    Thanks
    Srinivas

    PO Header: EKKO
               field: EKKO-KNUMV (number of condition document)
    PO Item: EKPO
               field: EKPO-NETWR (net value in PO currency)
    Conditions: KONV
               field: KONV-KNUMV = EKKO-KNUMV
               for PO item conditions, item number of condition document = item number of PO
               for PO header conditions, item number of condition document = 0000
    Subtotals in the pricing procedure are calculated and not stored in any KONV.
    PO header Net Price = sum of all EKPO-NETWR for all the items.
    Hope this helps.

  • How to enter values for an Info-object Manually.

    Hi
    I have to enter certan values manually in the customised info-object which are not available in the
    BW at the moment.
    If I go via SE11 I am only getting change option.
    If I go via SM30 it says The maintenance dialog for ****** is incomplete or not defined.
    How can I enter certain values to this info-object.
    Please advise.
    Many thanks,
    Kate.

    Hi
    Use transaction rsdmd to maintain infoobject values - or right click any infoobject an select maintain master data.
    Regards,
    Beat

  • How I configure a default value in an entity object?

    Hi,
    1:how I configure a default value in an entity object?
    attribute type: DATE
    I want to set the current date
    ...thanks

    Override the create() action in your entity object implementation:
    protected void create(AttributeList attributeList)
    * This method should be subclassed to supply programmatic default values to
    * various attributes of a new Entity Object. For example, to create a sequence.
    * Instances of subclasses should call super.create() before performing any operations.
    System.out.println("create(AttributeList attributeList) - start");
    Date datSysdate = ( oracle.jbo.domain.Date) Date.getCurrentDate();
    super.create(attributeList);
    // set the default to today...
    setHiredate( datSysdate );
    System.out.println("create(AttributeList attributeList) - end");
    }

  • Business graphics/series:series/Point" Role "Values":A minimum 1 object(s)

    Hi All,
    I am getting an error:
    Webdynpro generation: metadata constraint of component component_name is violated: Point "// WebDynpro/View:com.domain.ProjectViewName/RootUIelementcontainer/...../ business graphics/series:series/Point" Role "Values":A minimum 1 object(s) is required.
    I am using pie chart for graphics.
    Thanks and regards,
    Hanif Kukkalli

    Hi ,
    When you create a series , you have to create a series -> series_point->point ->numerical or time value..
    Blogs by Marcin Galczynski
    /people/sap.user72/blog/2006/05/01/advanced-business-graphics--time-scatter
    Or you can use a simple series
    /people/sap.user72/blog/2005/03/23/business-graphics-in-webdynpro
    Regards
    Bharathwaj

  • How to get value of a nested object?!!

    I am writing a small game program in java(applet). I am not getting the value of a nested object in the paint method. Please help.
    the text marked as code is wrong. Can u tell me a simple method?
    s1.a[0].x will give error. What should I use?
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    class SnakeBit
         int x,y; //x and y are coordinates
         int position;
    public SnakeBit(int x, int y)
         this.x=x;
         this.y=y;
    public SnakeBit copy(SnakeBit a)
         this.x=a.x;
         this.y=a.y;
         return this;
    public int getValueX()
         int a;
         a=this.x;
         return a;
    public int getValueY()
         int b;
         b=this.y;
         return b;
    }//end of class SnakeBit.
    class Cell
         public int x,y;
    //************************************************Snake class***************************************
    class Snake
         public Snake()
                             SnakeBit a[]=new SnakeBit[5];
                             a[0]=new SnakeBit(8,4);
         public static void main(String args[])
                             SnakeBit a[]=new SnakeBit[5];
                             a[0]=new SnakeBit(8,4);
                             a[1]=new SnakeBit(8,5);
                             a[2]=new SnakeBit(8,6);
                             a[3]=new SnakeBit(9,6);
                             a[4]=new SnakeBit(10,6);
                             a[5]=new SnakeBit(10,7);
                             a[6]=new SnakeBit(10,8);
              Snake a1=new Snake();
         public void up(SnakeBit a[])
              move(a);
              a[0].y=a[0].y-1;
         public void right(SnakeBit a[])
              move(a);
              a[0].x=a[0].x+1;
         public void left(SnakeBit a[])
              move(a);
              a[0].x=a[0].x-1;
         public void down(SnakeBit a[])
              move(a);
              a[0].x=a[0].y+1;
         public void move(SnakeBit a[])
              a[6].copy(a[5]);
              a[5].copy(a[4]);
              a[4].copy(a[3]);
              a[3].copy(a[2]);
              a[2].copy(a[1]);
              a[1].copy(a[0]);
    }//end of class Snake.
    public class Game1 extends Applet //implements KeyListener
         Snake s1;
         SnakeBit aa;
         int sizeOfCell=10; //default=10
         int startX=20; //starting x position of board.default=20
         int startY=20;     //starting y position of board.
         int maxX=200;          //max x value of board, ie width. default=200
         int maxY=200;          //max y value of board ie height.
         boolean drawBoard;
         //int arrays which will store the values of the objects
         int x[]=new int[5];
         int y[]=new int[5];
         public void init()
              s1=new Snake();
              //drawBoard=false;
              //x=s1.getValueX();
              //y=s1.getValueY();
              //aa = s1.a[0];
         public void paint(Graphics g)
         //public void Mpaint(Graphics g, Snake s1)
              //if (drawBoard==false)
                   drawBoard=true;
                   g.setColor(Color.cyan);
                        for(int i=startX;i<=maxX;i=i+sizeOfCell)
                        for(int j=startY;j<=maxY;j=j+sizeOfCell)
                             g.fillRect(i,j,sizeOfCell,sizeOfCell);
                   g.setColor(Color.red);
                                  for(int i=startX;i<=maxX;i=i+sizeOfCell)
                                  for(int j=startY;j<=maxY;j=j+sizeOfCell)
                                       g.drawRect(i,j,sizeOfCell,sizeOfCell);
              for(int i=0;i<7;i++)
    <code>
              g.drawRect(s1.a[0].x * sizeOfCell, s1.a[0].y*sizeOfCell,sizeOfCell,sizeOfCell);
    </code>
    }//end of class Game.

    Those who feel the program is too big to solve,( actually the code is not well written, and documentation is nill)
    I will try to explain in my own words.
    There are only 3 classes
    1)SnakeBit
    2)Snake
    3)Game
    SnakeBits draws a small part of a snake.
    Snake draws the whole snake.
    In the constructor of the Snake class , one SnakeBit is created.(Actually we create 5 or more, but right now to make the problem clear, I have created only one.)
         SnakeBit a[]=new SnakeBit[5];
         a[0]=new SnakeBit(8,4);
    In the game class, a Snake is created.
    In the game class, I can access the members of the Snake class. But I cannot access the members of the SnakeBit class.
    I need to access the members of the SnakeBit class from the paint method. otherwise How am I going to draw it on the screen.
    So tell me a way to do it.
    I want to get the x and y variable of the SnakeBit class from the Game class.
    Please help.

  • Version comparison for BW objects

    Hello guys,
    How to see versions of BW objects like, Infocube, Multiprovider, datasource? I wanted to do the version comparisons for few objects in Dev with respect to Production. How do I make sure that both have the same versions and there is no harm in transporting it from dev to prod.
    In case we already did the changes in dev before checking the comparison, what do we do?
    Please give me complete process of version comparison and where exactly I could see for versions.
    Please reply asap.
    Thanks in adv.
    Regards,
    BMW 325ci

    Unfortunately, there is no tool delivered with SAP BW that provides the ability to compare objects across the landscape. There are some third-party tools that provide this functionality.
    You could create your own by create an ABAP program, or several ABAP programs, using the following BAPI Function Modules to get the details of the objects in each environment and then do a comparison:
    InfoCube - BAPI_CUBE_GETDETAIL
    InfoObject Catalog - BAPI_IOBC_GETDETAIL
    InfoObject - BAPI_IOBJ_GETDETAIL
    InfoPackage - BAPI_IPAK_GETDETAIL
    InfoSet - BAPI_ISET_GETDETAIL
    DSO - BAPI_ODSO_GETDETAIL
    MultiProvider - BAPI_MPRO_GETDETAIL

  • In Captivate, how to effectively control the movement of objects on the stage?

    How to, say, make a shape (an arrow, for example) which points to a certain location move (and maybe rotate at the same time) to another location (another point on the stage) one second later. I know I can "apply effect" to it, but still I don't find it very easy to control the start position and end position of the arrow and the way it rotates (for example, how to make it rotate along the X, Y and Z axis respectively?)
    Or, how can I make an image fly into the stage to a specific location from a certain direction, linger for a specific time, and then fly out in a specified direction? And while setting up this animation, is it possible to scrub across the timeline to see the effect of the animation, as you do in Flash Professional?

    An old pain, efficient control of motion paths and motion effects, I know.
    All motion effects are relative to the start position of the object, that can allow you to realize the fly in and fly out: time both effects and leave the necessary 'linger' time between the effects. The motion paths can be edited. To have more precise coordinates you can either edit the XML that describes the effect, or create a motion effect in Flash Professional and save it as XML to be used in Captivate, or use my approach: I create a crosshair group with a perfect vertical and horizontal line object, that you can position exactly using the X and Y coordinates and then drag start or end point of the motion path over that cross hair. I have been pleading to have at least a snap function or a serious improvement that allows to enter coordinates but apparently not many users are wanting this improvement. I used that approach here in the first movie: Reset Effects in Captivate 6 - Captivate blog
    And no, Preview slide (which is what you do when scrubbing the timeline) is not a real preview, you cannot see those effects.
    Please, next time mention the version you are using. In CP7 and before effect duration was relative to object duration, in CP8 it is (finally) absolute timing.

  • When the combobox is rendered all the values displayed look like [object Object].

    I wasn't sure where to post this so please forgive me.
    I am trying to populate a combobox with a series of values
    returned from a Coldfusion method that retrives a resault froma
    database. Basically all the data from a table of staff.
    <cffunction name="getUsers" access="remote"
    returntype="array">
    <cfquery name="q" datasource="#datasource#">
    SELECT *
    FROM STAFF_CHARTS_STAFF_TEMP
    </cfquery>
    <cfset aRecordset= querytoarray(q)>
    <cfset flash.result=aRecordset>
    <cfreturn flash.result>
    </cffunction>
    <cffunction name="querytoarray" returntype="array"
    output="No">
    <cfargument name="q" required="Yes" type="query">
    <cfset var aTmp = arraynew(1)>
    <cfif q.recordcount>
    <cfloop query="q">
    <cfset stTmp = structNew()>
    <cfloop list="#lcase(q.columnlist)#" index="col">
    <cfset stTmp[col] = q[col][currentRow]>
    </cfloop>
    <cfset arrayAppend(aTmp,stTmp)>
    </cfloop>
    <cfelse>
    <cfset stTmp = structNew()>
    <cfloop list="#lcase(q.columnlist)#" index="col">
    <cfset stTmp[col] = "">
    </cfloop>
    <cfset arrayAppend(aTmp,stTmp)>
    </cfif>
    <cfreturn aTmp>
    </cffunction>
    The result from a call of CF method getUsers is set as the
    data provider to ComboBox with id = "cb"
    <mx:RemoteObject
    id="myService"
    destination="ColdFusion"
    source="staff_ratings.staff_Ratings-debug.staff"
    showBusyCursor="true">
    <mx:method name="getUsers" result="handleResult(event)"
    fault="Alert.show(event.fault.message)"/>
    </mx:RemoteObject>
    <mx:Script>
    <![CDATA[
    import mx.rpc.events.ResultEvent;
    import mx.controls.Alert;
    [Bindable]
    public var sResult:Array;
    public function handleResult(event:ResultEvent):void{
    sResult=event.result as Array;
    cb.dataProvider=sResult;
    ]]>
    </mx:Script>
    However when the combobox is rendered all the values
    displayed look like [object Object].
    I think the problem is that the result is a array of arrays
    so I need to find away to ectract a particular colun of data into
    the combobox
    How do I fix this?

    This is what I did for a single field.
    If you were wanting the to have muliple fields in the
    dropdown, I would concatenate them in the query.
    something like
    select fname + ' ' + lname + ' ' + anotherfield as tech from
    dbo.table
    <cffunction name="getTech" output="no" access="remote"
    returntype="query">
    <cfset var qTech="">
    <cfquery name="qTech" datasource="datasource">
    select tech
    from dbo.table
    group by tech
    order by tech
    </cfquery>
    <cfreturn qTech>
    </cffunction>
    <mx:RemoteObject
    id="dataManager"
    showBusyCursor="true"
    destination="ColdFusion"
    source="cust.components.cfgenerated.Records">
    <mx:method name="getMasterQuery"
    result="getMasterQuery_result(event)" fault="server_fault(event)"
    />
    <mx:method name="deleteItem"
    result="deleteItem_result(event)" fault="server_fault(event)" />
    </mx:RemoteObject>
    <mx:ComboBox id="TECH"
    dataProvider="{dataManager.getTech.lastResult}" labelField="tech"
    />

Maybe you are looking for

  • When I narrate my keynote presentation, why is the file size so huge?

    When I narrate my keynote presentation, why is the file size so huge?

  • Set Action path in ValueChangeListener in SelectOneChoice

    Hi, I would like to see how to set the action path programatically in my java bean. What I meant is for example, in jspx page: <af:commandLink text="#{row.RequestUid}" id="cl1" action="gotoRequestOverview"> </af:commandLink> I can define "action" in

  • Java Learning for the beginners

    Hi All, I have completed OCA exam (SQL and PL/SQL) and now i want to learn JAVA to develop front-end database application. I am quite new with JAVA and i have no experience with it. would anyone pls. help me by providing the website/URL from where i

  • Junk character '#' comes in Project description and WBS description...

    Dear all, When we transfer and release the project from C-projects module to PS, then sometime, a junk character like "#" gets appended to the Project & WBS description, when we retrive the Project in CJ20N. This junk character creates a lot of probl

  • MIMO best practices

    Hi all, You will have to excuse me but I am very new to LabVIEW and I have 3 basic questions: 1.) I am experimenting in the lab with the 2x2 MIMO set up by performing some basic testing on your pre-built 2x2AlamoutiQAM with BER software that I downlo