Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Using IF statement to control return statements.

    Java Code:
       public static int seqSearch(int numRecords, String[] stuName,
          double[] stuGpa, String nameKey, double gpaKey)
       {
            
            for(int i = 0; i < numRecords; i++)
               if(stuName[i] == nameKey && stuGpa[i] == gpaKey)
                  return i;
            return -1;
    
    
       }
    So, how would I used an if statement to control this? I'm doing sequential search to find if the name is found in the array and if the gpa is in the array, then it should return the position it was found in (i). But, all it does do is return -1 and print out that none were found.

  2. #2
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Use .equals instead of ==. For instance...

    stuName[i].equals(nameKey)

    Of course, you run the risk of stuName[i] being null, so you'd be better off using StringUtils.equals from the apache commons API.

  3. #3
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Yeah, I ended up using stuName[i].equals(nameKey). I forget that == doesn't work with strings. :x

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by Teclis View Post
    Yeah, I ended up using stuName[i].equals(nameKey). I forget that == doesn't work with strings. :x
    For the record, the == operator does work with Strings, but it tests to see whether the two variables point to the same String object.

    Java Code:
    String s1 = "Java!";
    String s2 = s1;
    
    if (s1 == s2)
       // This is true
    Java Code:
    String s1 = new String("Java!");
    String s2 = new String("Java!");
    
    if (s1 == s2)
       // This is false

  5. #5
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    This also works :)...

    String s1 = "test";
    String s2 = "test";

    if(s1 == s2)
    // this is true

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    No that technically is false but because of JIT enhancements and efficiency, if the same String has already been created, it just uses that old object.
    Use "new String("")" forces Java to create a new object for this String.

    Java Code:
    String s1 = "test";
    String s2 = new String("test");
    
    if(s1 == s2) //false :)

  7. #7
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    @ra4king
    We are both correct. Both of our code examples will compile and will result in how we both posted.

    Note: I'm not suggesting that == is the correct or preferred way of testing String equality. I'd recommend avoiding it when ever possible. However, I merely wanted to point out that it will technically work in certain cases.

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Oh my response was directed towards Snotmare.

    But yes, == should never be used to test String equality, but to test for same objects.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ra4king View Post
    No that technically is false but because of JIT enhancements and efficiency, if the same String has already been created, it just uses that old object.
    It has nothing to do with the JIT compiler; the java source code compiler (javac) stores literal Strings in a separate area in the class files and they are loaded in a shared String pool by the JVM when the class is loaded. All literal Strings are therefor only stored once in a Java program. Also see the String.intern() method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Yes, if the same String is in the heap, it uses that. The compiler optimizes the code for the Java JIT run-time.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ra4king View Post
    Yes, if the same String is in the heap, it uses that. The compiler optimizes the code for the Java JIT run-time.
    It really is beyond me what the JIT compiler has to do with it; the Java source code compiler doesn't even know a JIT compiler exists. I think you're mixing up terminology and technology.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    No there is not JIT compiler. JIT means Just-In-Time and it refers to run time systems.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ra4king View Post
    No there is not JIT compiler. JIT means Just-In-Time and it refers to run time systems.
    And what does the runtime system do 'just in 'time'?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    The Java Virtual Machine translates bytecode into machine code on the fly.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ra4king View Post
    The Java Virtual Machine translates bytecode into machine code on the fly.
    Yep, and that is done by a JIT compiler, directed by the HotSpot mechanism. Please get your facts straight. String pooling has nothing to do with JIT compilation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    What I meant was that the JIT optimizes the code so that during run-time, in the heap, there is only 1 object of a String.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ra4king View Post
    What I meant was that the JIT optimizes the code so that during run-time, in the heap, there is only 1 object of a String.
    The JIT compiler doesn't do that; it's the class loader that checks and organizes the String pool. And b.t.w. there is no 'JIT' there's the 'JIT compiler'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    The class loader? I need to take another look at that paper I read then.
    Thanks for the help.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ra4king View Post
    The class loader? I need to take another look at that paper I read then.
    Thanks for the help.
    Yep, it's the class loader: every .class file has a 'constant pool' area; the class loader stores the Strings in that area in a pool and resolves all address references to those constant Strings (now stored in the pool).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Every .class file has it's own String pool?
    So would this be false:
    Java Code:
    class A {
        String hello = "Hello";
    }
    
    class B {
        String hello = "Hello";
    }
    
    class Main {
        public static void main(String args[]) {
            System.out.println(new A().hello == new B().hello);
        }
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 04-11-2011, 07:26 PM
  2. Return statements in methods
    By adjit in forum New To Java
    Replies: 12
    Last Post: 03-17-2011, 02:31 PM
  3. Help with simple control statement
    By Kevin BSU in forum New To Java
    Replies: 3
    Last Post: 11-15-2010, 04:27 PM
  4. If Statement return
    By ricardo5222 in forum New To Java
    Replies: 4
    Last Post: 11-01-2010, 11:43 AM
  5. Static Method and Return Statements
    By berelson in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 11:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •