Question about String initialization~~~

String a = "sss";
String b = "sss";
the result of a==b is true .
String a = new String("sss");
String b = new String("sss");
however , the result of a==b is false .
why? the operator "==" compares WHAT of two object ??
thanks , ;[                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

== compare the reference of the object
String s1 = new String("sss") creates a new String object at location xxxxxx, while
String s2 = new String("sss") creates a new String object at location yyyyy.
therefor xxxxxxx != yyyyyyyy
by location..the memory address
String s3 = "sss"; // java create a new String object "sss" at location zzzzzzzz and assign the reference to s3
String s4 = "sss"; // java assign the reference (location zzzzzzzz) to s4
so s3 == s4....
noticed only one String object is created...s3 and s4 holds the reference to the 1st String created..though it is not clearly shown in th ecode...it actually look something like this
String temp = new String("sss");
String s3 = temp;
String s4 = temp;

Similar Messages

  • Question about the Initialization Parameters Information in the Alert.log

    Hi, All -
    What is the correct answer for the following question.
    Specifically, what information does Oracle provide you with in the alert.log regarding initialization parameters?
    a. Values of all initialization parameters at startup
    b. Values of initialization parameters modified since last startup
    c. Values of initialization parameters with non-default values
    d. Only values of initialization parameters that cannot be modified dynamically.
    I think the answer should be B, but I would like to confirm.

    The answer is C
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/process.htm#sthref1633
    The alert log is a special trace file. The alert log of a database is a chronological log of messages and errors, and includes the following items:
    All internal errors (ORA-600), block corruption errors (ORA-1578), and deadlock errors (ORA-60) that occur
    Administrative operations, such as CREATE, ALTER, and DROP statements and STARTUP, SHUTDOWN, and ARCHIVELOG statements
    Messages and errors relating to the functions of shared server and dispatcher processes
    Errors occurring during the automatic refresh of a materialized view
    The values of all initialization parameters that had nondefault values at the time the database and instance start
    Kamran Agayev A. (10g OCP)
    http://kamranagayev.wordpress.com

  • Question about string manipulation

    Hello, I am practicing with Java and trying to learn it, and have a quick question. I am trying to get the count on a string of numbers for after a decimal point. the numbers are generated as the result of a math, so I can't use index, since the location of the decimal changes based on the number imputed. I want to figure this out on my own, but have hit a wall here, i've spent the past few hours trying every string command I can think of, and haven't figured it out yet, anyone mind pointing me in the right direction for which string to use? Thanks in advance for any help you can provide!

    Is this what you want?
    public class Fallen{
      public static void main(String[] args){
        String number = "123.45678";
        String frac = number.substring(number.indexOf('.') + 1);
        System.out.println(frac + " length=" + frac.length());
        frac = number.replaceFirst("^.+\\.", "");
        System.out.println(frac + " length=" + frac.length());
    }

  • Question about String constant pool

    Is there is any function available to find how many Sring literal in String Constant pool

    Well, there's stuff like BCEL.
    But it may not do what you want.
    Why do you want to find out what's in the constant pool?

  • Easy question about String

    Hi!
    I have a string in a variable. how can I check if the String is composed just by numbers?
    thank you very much!

    Use the following method from Integer API
    parseInt
    public static int parseInt(String s)
                        throws NumberFormatExceptionParses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002d') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.If it throws NumberFormatException its alphanumeric else its numeric. You would however have to remove any spaces or commas etc. since they are not mumeric either

  • A question about String.hashCode()

    Why the implementation of hashCode() for String class using 31 as a base?

    Why the implementation of hashCode() for String class
    using 31 as a base?I think it's a magic number. It has been found to produce a reasonably even distribution over the int range.

  • A question about string.trim()

    I got a fragment of code as follows:
    String s = "abcd ";
    boolean a = (s.trim()=="abcd");
    boolean b = (s =="abcd ");
    Why a is false, b is true?

    The reason why the below code has true assigned to be is quite easy to explain:
    String s = "abcd";
    String y = "abcd";
    boolean b = (s == y);
    ...The String class has a "Pool" of strings. When you use String literals; the compiler automatically checks if it is in the String pool already and if it exists; a reference to that String is returned; if not a new entry in the pool is added and then you get a reference to that. This is only true for Stirng literals and String created using the intern() method.
    If you compile this code you will see that the strings are not equal:
    public static void main(String args[])
      String s = "abcd";
      String y = args[0];
      boolean b = (s == y);
      System.out.println(b);
    }This is because the "abcd" is in the String pool when your program starts; but because the value "abcd" passed in (is not created in the pool automaticlaly). Therefor they are two different String objects.
    This code block shows that you can add Strings to the pool. The intern() method checks to see if the String is in the pool already and if it is it will return you a reference to it; if not it will create one and then return it.
    public static void main(String args[])
      String s = "abcd ";
      String y = args[0].intern();
      boolean b = (s == y);
      System.out.println(b);
    }- Chris

  • Question about string constant in Hex display format

    how to get a plus of 2 string constant which is display in Hex display like attahced, for example I have string "28" and "5D" in Labview which has been in Hex format, the plus of this 2 string should be "85" in Hex. pls help on this, thanks
    Attachments:
    1.jpg ‏21 KB

    This'll work, too.  I'm afraid of casting types...
    Jm
    Jim
    You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
    Attachments:
    hex.png ‏2 KB
    hex.vi ‏7 KB

  • Question about string array?

    Hello,
    When passing Stting arrays to a function, is there anyto test if the array is emply?
    void testFucntion(String[] test){
    }

    Or test the array is null.
    if (test == null || test.length == 0)

  • Another question about string!

    Hi,
    I want to return a function as true if all the characters in that string are hexDigit. false otherwise.
    & also another class should return as true if all the charaters are Digit. false otherwise i was using
    public boolean isDigit(String str)
    for (int i=0; I<str.length(); i++)
    if (str.charAt(i).isDigit)
    return true
    return false
    what's wrong with this code? what shall I use instead of this? can anyone Help please?

    The way you've written it, you'll be returning true as soon as the first digit in the string is found:
    // on first loop will check first char in string.
    if(str.charAt(i)isDigit)
    // first char in string is a digit so method returns true
    return true;
    [\code]
    You could change it so that you will return false when non-numerical character is found. If none is found you will fall out the loop and return true.if(!str.charAt(i).isDigit)
    return false;
    return true;
    [\code]
    Don't know if there's a method for checking hexDigit. If not use something like:
    if (str.charAt(x).isDigit || str.charAt(x) >='A' && str.charAt(x) <='F');
    [\code]

  • A question about string

    hello,
    String s1 = "123";
    String s2 = "123";The following codes, create two String objects or one object?
    Any ideas?
    Thanks.

    The compiler creates one string, and both variables get pointed at it.
    http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101083

  • Question about String

    hi all,
    I am new to java
    I have one problem..
    that is I have one string like : 85WS98(or also: 67S, 1G3)
    from this String I want to only the
    85W98 = 85;
    67S = 67
    1G3 = 1
    I am using below code but its not working for : 85W98 and 1G3
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < str.length(); i++){
    char comp = str.charAt(i);
    if(Character.isDigit(comp)){
    sb.append(comp);
    String result = sb.toString();
    So if anyone knows please send the solution
    thanks

    So you want all the numeric chars before the first non-numeric char?
    Change this part of the code:
    if (Character.isDigit(comp)) {
      sb.append(comp);
    } else break; // break after first non-numeric char

  • Question about string in xdoxslt

    Hi,
    When using xdoxslt i want to set a variable to store many fields which means the variable is a concat of the fields and it seems that the '||' is not allowed in xdoxslt.
    I find a function append_to() and uses it as below
    <?xdoxslt:set_variable($_XDOCTX,'PeopleCnt',xdoxslt:append_to($_XDOCTX,xdoxslt:get_variable($_XDOCTX,'PeopleCnt'),'sdfs'))?>
    and the output of PeopleCnt is still the original value.
    Is there anything wrong i have taken for this function or is there anyting else i can do?
    Thanks!

    This'll work, too.  I'm afraid of casting types...
    Jm
    Jim
    You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
    Attachments:
    hex.png ‏2 KB
    hex.vi ‏7 KB

  • Few questions about Calendar / SimpleDateFormat classes

    Hi.
    I have few questions about Calendar class:
    1. How can I get only date representation (without the time)?
    after doing:
    Calendar cal = Calendar.getInstance();
    I tried to clear unecessary fields:
    cal.clear(Calendar.SECOND);
    cal.clear(Calendar.MINUTE);
    cal.clear(Calendar.HOUR_OF_DAY);
    But after printing the time, it seems that the HOUR was not cleared:
    SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    System.out.println(sdf1.format(cal.getTime()));
    ---> 03/11/2004 17:00:00
    Am I missing somthing?
    2. I want to make sure that two different formats couldn't be compared. i.e., if I'll try to parse one String according to different format -- ParseException will be thrown.
    String date = "19/04/2004 13:06:10";
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date dateObj = sdf.parse(date);
    However, even though that formats are different, no exception is thrown and the return Date is 19/04/2004 00:00:00.
    How can I cause to exception to be thrown if formats are not identical?
    Thanks in advanced

    The Calendar class has a few of what Microsoft would call 'features'. :^)
    To clear the time, call:
    calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY), 0, 0, 0);
    If you want 'pessimistic' rather than 'optimistic' date parsing, you have two options:
    1) Call calendar.setLenient(false);
    See if that is strict enough for your needs, otherwise:
    2) Write code to ensure a stricter parsing of the string passed in. For example, you could very simply check the length of the string to determine if time data is also present.
    When parsing, a string like '02/29/02' would parse to the date '03/01/02'. Setting lenient to false may fix this, but the surest way is to do some testing and then make the parsing more pessimistic where it suits your needs.
    - Saish
    "My karma ran over your dogma." - Anon

  • Questions about supporting TLF with Halo components in Flex 4.1

    We recently decided to upgrade to the Flex 4.1 SDK after a year or so at 3.2.  We have been asked to not use the new SPARK components yet because our products would then have a mistmatch of Halo and Spark components in the UI.  We have built support for our Halo components to display html through the ".htmlText" property for the Text components.  For example, we can display strings such as "<p>Choose <b>one</b> option:</p>".  With the upgrade to Flex 4.1 a couple of questions about supporting bi-directional text have come up.  My understanding is that in order to support bi-directional text we need to use the Text Layout Framework.  Does anyone have a suggestion on how we can utilize the TLF with the Halo components in Flex 4.1?    Can we still use the ".htmlText" property somehow or is there a new property that understands the TextFormat markup?  We would love to still be able to use the limited html tags that are supported for the "htmlText" property.  Is there an option where that syntax is still understood?
    Thanks in advance!
    David

    The halo components use TextField, and the spark components use TLF. Both sets have support for some html markup to import and export text. To use bidi text, you have to use TLF; TextField won't work properly.
    It is possible to use TLF to build your own components, and this can work well particularly for applications with specialized needs. But if you need bidi support for advanced components like DataGrid and List, then you should use the spark components.
    Thanks!
    - robin

Maybe you are looking for

  • Cannot load cookies at all

    I cannot load cookies i try to enable and it says i do but it does not so what can i do?

  • Link to the new iPad advert.

    http://www.youtube.com/watch?v=CjiKAL05Nc4&lc

  • 32-bit OEM windows on 64-bit laptop?

    Hi guys!                I use a lenovo g550 model 20023 with a Intel Penium t4500. on the official intel site, as you can see here, this processor has a 64-bit instruction set, which shows that this is a 64-bit laptop...But , when this laptop was bro

  • Can't export flash cs6 in good quality

    Hi, I can't seem to export my animation from flash cs6 in good quality. I tried exporting in avi, but that reduced the quality a lot and when I exported in quicktime, the audio didn't sync well. Not sure what to do, but I really want the video to loo

  • Apps not recognized by iTunes

    Hello - I would appreciate any suggestions regarding an apparent issue with my iPod touch 2G. This is in conjunction with iTunes 8.0.1 on a G5 tower, dual core 2.0. The iPod will download and run apps from the Apps Store without difficulty, and iTune