Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By JosAH

Thread: Relational expressions and others

  1. #1
    EmptyBelly is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Relational expressions and others

    Hello!
    Whether the following Java relational expressions valid or not. Correct me if I made any mistake :-)

    • 5 <= 4+2



    Valid, I think this is valid as 5 always smaller than 6

    • "Hello" != "World"



    Valid, as one string can be different than another string.

    • a < a - 0 (where a is an initialized non-local variable of integer type)


    Not sure about this one.

    Finally,

    Variables a and b are of type int in the following statement:
    a = b = 1;
    Is it a valid statement in Java? Why?

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    263
    Rep Power
    2

    Default Re: Relational expressions and others

    You mean valid if they are true or valid if they are accepted by the java grammar?
    All of them are valid meaning that the syntax is correct.
    Only, when comparing strings, avoid using ==, rather use the equals method, so (!"Hello".equals("World")); true
    in your last sample you assign 1 to b and b to a

  3. #3
    EmptyBelly is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Relational expressions and others

    Yes, that's all I meant whether they accept by java grammar.

    So the listed 3 in my previous message are valid, can you explain to me WHY they are valid :-)

    Also, what do you mean: "Only, when comparing strings, avoid using ==, rather use the equals method, so (!"Hello".equals("World"))"

    I confused by !"Hello".equals(World"). Sorry for so many questions.

    Thanks a lot!!


    Quote Originally Posted by superhaNds View Post
    You mean valid if they are true or valid if they are accepted by the java grammar?
    All of them are valid meaning that the syntax is correct.
    Only, when comparing strings, avoid using ==, rather use the equals method, so (!"Hello".equals("World")); true
    in your last sample you assign 1 to b and b to a

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: Relational expressions and others

    To compare String you use the equals method ...
    the == and != is used in python :P in Java to compare two Strings you use equals.. for example to check if the String "apple" is equal with "banana" you must do this :

    Java Code:
    String apple = "apple" ;
    String banana = "banana";
    if(apple.equals(banana)){
        System.out.println("No way!");
    }else{
        System.out.println("Sure");
    }
    now to check if one string is Not equal you use the "!" for example !banana.equals(apple);
    now let's see why the statement banana == apple is valid but not necessary true :)
    imagine a grid with columns and rows which each one cell is an address.. In each address you save variables...so when you check if StringOne == StringTwo you check if the StringOne "See"(reference) in the same address(cell) like StringTwo.. which can be possible.. you will understand about that when you check about the Objects copies

    TIP 1. To check if one String is equal with null the statement var.equals(null) is not valid .. only in this situation we use var == null !
    TIP 2. If you have two Strings one var1 = "David" and var2= "david" to check if this two String are equals if you try this :

    Java Code:
    var1 = "David";
    var2 = "david";
    if(var1.equals(var2)){
    	System.out.println("Yes")
    }else{
    	System.out.println("No")
    }
    the output will be NO! because the java has high sensitive with strings.. so in this case you should use the method equalsIgnoreCase()
    e.x :

    Java Code:
    var1 = "David";
    var2 = "david";
    if(var1.equalsIgnoreCase(var2)){
    	System.out.println("Yes")
    }else{
    	System.out.println("No")
    }
    Now the output will be Yes ;)
    Last edited by ShadowWalker; 06-24-2013 at 07:38 PM.

  5. #5
    EmptyBelly is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Relational expressions and others

    Sounds like only 2 are valid:

    5 <= 4+2
    a < a - 0 (where a is an initialized non-local variable of integer type)

    But this one invalid, right?

    "Hello" != "World"


    Quote Originally Posted by EmptyBelly View Post
    Yes, that's all I meant whether they accept by java grammar.

    So the listed 3 in my previous message are valid, can you explain to me WHY they are valid :-)

    Also, what do you mean: "Only, when comparing strings, avoid using ==, rather use the equals method, so (!"Hello".equals("World"))"

    I confused by !"Hello".equals(World"). Sorry for so many questions.

    Thanks a lot!!

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    263
    Rep Power
    2

    Default Re: Relational expressions and others

    It is valid, == and != work for strings but it is a weird case, and even if it didn't work it would still be valid, but the result would not be what you would expect.
    String s = "a";
    String a = "a";

    System.out.println(a == b); prints true and != false

    Anyway, it is recommended that the equals method is used when comparing strings to avoid any issues.

  7. #7
    EmptyBelly is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Relational expressions and others

    Thanks for the explanation!! Now I understand. STRICTLY speaking, can we say, "Hello" != "World", is invalid?

    Also, can you explain to me why the first 2 are valid?

    Thx!

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Relational expressions and others

    == and != tests on objects test for object reference equality, not contents (in contrast to primitives). So if you test (myObjectA == myObjectB) that will return true if the variables point to the same object in memory.
    Java Code:
    MyObject a = new MyObject();
    MyObject b = a;
    MyObject c = new MyObject();
    a == b true --> b points to the same object as a
    b == c false
    a == c false

    With Strings there is a catch. Java prevents Strings from being created multiple times, so you don't have 500 copies of "yes" in memory. Java will intern the String "yes" and make every String variable that contains "yes" point to the same interned String. That's also the reason Strings are immutable, i.e. cannot be changed. Consider next example:
    Java Code:
    package bla;
    
    public class EqualityTest {
        public static void main(String[] args) {
            String a = "yes";
            String b = "yes";
            String c = new String("yes");
            String d = new StringBuilder("yes").toString();
    
            System.out.println("a == b " + (a == b));
            System.out.println("b == c " + (b == c));
            System.out.println("a == d " + (a == d));
    
            System.out.println("a.equals(b) " + (a.equals(b)));
            System.out.println("b.equals(c) " + (b.equals(c)));
            System.out.println("a.equals(d) " + (a.equals(d)));
        }
    }
    This gives this output:
    Java Code:
    a == b true
    b == c false
    a == d false
    a.equals(b) true
    b.equals(c) true
    a.equals(d) true
    As you can see, a == b is true because the compiler interned the Strings automatically and the variables point to the same object. However, the other objects have gotten their String content via other ways (new String() and StruingBuilder()), and thus point to different objects because the compiler could not optimize these, so == returns false. Note thal ALL the equals() tests are true.

    Lesson learned: when testing Strings *ALWAYS* use equals() (or equalsIgnoresCase()).

  9. #9
    EmptyBelly is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Relational expressions and others

    Thanks again! Why this one is valid then?

    "a < a - 0 (where a is an initialized non-local variable of integer type)"

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: Relational expressions and others

    Quote Originally Posted by EmptyBelly View Post
    Thanks again! Why this one is valid then?

    "a < a - 0 (where a is an initialized non-local variable of integer type)"
    Why not let a be 10? Then it says 10 < 10 - 0. Does this look like a valid statement? If so, what is the result?
    Java Code:
    //will this compile?
    int a = 10;
    if ( a < a - 0) {
        //will I get here?
    }

  11. #11
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: Relational expressions and others

    a < a - 0 is valid as an expression in java (When say valid i mean is accepted by compiler).. but actually the result of this expression will be always false..
    for example let's say that a = 4
    is this true ; 4 < 4 - 0 ? of course no.. 4<= 4-0 will be true... or 4= 4-0 but 4<4-0 is false..
    Last edited by ShadowWalker; 06-25-2013 at 10:53 AM.

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

    Default Re: Relational expressions and others

    If a were a double type variable with the value NaN however, a == a-0 would also be false as a+42 > a is false as well. NaN is a very funny value ...

    kind regards,

    Jos
    ShadowWalker likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 12-15-2010, 02:22 AM
  2. Quick Question about relational operators
    By SwEeTAcTioN in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 05:11 AM
  3. SPT Relational Database Web Tool 1.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-10-2007, 06:40 PM
  4. SPT Relational Database Web Tool 1.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-16-2007, 06:39 PM
  5. RMBench Relational Modeller 1.0.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-26-2007, 12:46 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
  •