Page 2 of 55 FirstFirst 12341252 ... LastLast
Results 21 to 40 of 1093

Thread: Quiz Time

  1. #21
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

  2. #22
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default @Eranga

    let us know the correct answer

    @sanjeev

    why do you think it will print No, is this only because it is posted in Quiz section :D
    i am the future

  3. #23
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, the answer is Yes. :)

    So I post a big banana to Delhi. :)

    Here is the thing I want to point there, I really hate that code. Because == used for string comparison. Not a good practice at all. Because '==' operator compares two object references and check whether two objects are exactly the same object or not.

    Should I put an example, in that case I really need a gift. ;)

  4. #24
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #25
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by rjuyal View Post
    let us know the correct answer

    @sanjeev

    why do you think it will print No, is this only because it is posted in Quiz section :D
    No dear......It doesn't mean this.
    If we are doing practice by this way it is good for us.....


    I guessed It.....may be two different "String" will be created in Heap.....
    that's why i said no....because == checks for reference.
    sanjeev,संजीव

  6. #26
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Well Eranga....

    again good question....

    sanjeev,संजीव

  7. #27
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    yes, certainly

    why it was in Quiz section :p lol

    what about

    ==, even if we use,
    equals/equalsIgnoreCase/contentEqual

    we will get the same answer "YES"

    so, what was the question,

    Or simply can you give us an example where
    "==" is different from "equals"
    i am the future

  8. #28
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    Java Code:
    I guessed It.....may be two different "String" will be created in Heap.....
    that's why i said no....because == checks for reference.

    Strings are interned
    i am the future

  9. #29
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Still there are lots of points to talk. May be not for us, for newbie specially.

    Ok, Sanjeev and Rakesh, can you the difference about those two line of code.

    Java Code:
    if("String".trim() == "String".trim())
    and

    Java Code:
    if("String".trim() == "String      ".trim())
    Did you see the slight difference there. My next quiz is, the answer is changed or remain as "Yes"?

    :):)

  10. #30
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by rjuyal View Post
    Or simply can you give us an example where
    "==" is different from "equals"
    A BIG difference in real world application pal.

  11. #31
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by rjuyal View Post
    yes, certainly

    why it was in Quiz section :p lol

    what about

    ==, even if we use,
    equals/equalsIgnoreCase/contentEqual

    we will get the same answer "YES"

    so, what was the question,

    Or simply can you give us an example where
    "==" is different from "equals"
    Check It

    Java Code:
    public class EqualsCheck {
        
        public static void main(String[] args) {
    
    		String s = "Hello";
    		String s1 = "Hello";
    		String s2 = new String("Hello");
    		
    		if(s == s1)
    			System.out.println("Yes, ==");
    		else
    			System.out.println("No, ==");
    
    		if(s == s2)
    			System.out.println("Yes, ==");
    		else
    			System.out.println("No, ==");
    
    		/******************************/
    		if(s.equals(s1))
    			System.out.println("Yes, equals()");
    		else
    			System.out.println("No, equals()");
    
    		if(s.equals(s2))
    			System.out.println("Yes, equals()");
    		else
    			System.out.println("No, equals()");
    		/******************************/
    
        }
    }
    sanjeev,संजीव

  12. #32
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Still there are lots of points to talk. May be not for us, for newbie specially.

    Ok, Sanjeev and Rakesh, can you the difference about those two line of code.

    Java Code:
    if("String".trim() == "String".trim())
    and

    Java Code:
    if("String".trim() == "String      ".trim())
    Did you see the slight difference there. My next quiz is, the answer is changed or remain as "Yes"?

    :):)


    Ans will be YES ...... because trim() method will be called first
    sanjeev,संजीव

  13. #33
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Unhappy BIG was much bigger in year 2023

    You will get you banana back as your gift, if you give us the example

    Using String, use '==' and 'equals' but answer should be different.

    Please sanjeev, if you have some example then post it here :(


    Java Code:
    if("String".trim() == "String      ".trim())

    i guess the answer should be same again.

    Both will become "String" after trimming, and as String are interned both will have same reference.
    i am the future

  14. #34
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sanjeevtarar View Post




    Ans will be YES ...... because trim() method will be called first
    No, you are wrong. Because trim() not will effect with '==' operators. That one of the deprecate process take place by Suns'

    If you do it as follows,

    Java Code:
            if("String".equals("String    ".trim()))
                System.out.println("Yes");
            else
                System.out.println("No");
    Your answer is "Yes". trim() do the omitting white spaces perfectly.

  15. #35
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by rjuyal View Post
    You will get you banana back as your gift, if you give us the example

    Using String, use '==' and 'equals' but answer should be different.

    Please sanjeev, if you have some example then post it here :(


    Java Code:
    if("String".trim() == "String      ".trim())

    i guess the answer should be same again.

    Both will become "String" after trimming, and as String are interned both will have same reference.

    Rakesh.....see example in reply #31
    sanjeev,संजीव

  16. #36
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default thanks

    Java Code:
    public class EqualsCheck {
        
        public static void main(String[] args) {
    
    		String s = "Hello";
    		String s1 = "Hello";
    		String s2 = new String("Hello");
    		
    		if(s == s1)
    			System.out.println("Yes, ==");
    		else
    			System.out.println("No, ==");
    
    		if(s == s2)
    			System.out.println("Yes, ==");
    		else
    			System.out.println("No, ==");
    
    		/******************************/
    		if(s.equals(s1))
    			System.out.println("Yes, equals()");
    		else
    			System.out.println("No, equals()");
    
    		if(s.equals(s2))
    			System.out.println("Yes, equals()");
    		else
    			System.out.println("No, equals()");
    		/******************************/
    
        }
    }


    Wow... i wanted this actually

    Thanks Sanjeev :)
    i am the future

  17. #37
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    No, you are wrong. Because trim() not will effect with '==' operators. That one of the deprecate process take place by Suns'

    If you do it as follows,

    Java Code:
            if("String".equals("String    ".trim()))
                System.out.println("Yes");
            else
                System.out.println("No");
    Your answer is "Yes". trim() do the omitting white spaces perfectly.
    Hey Eranga

    Really ....... It's important point.

    askkkkkkkkkkkkkkkkkk morreeeeeeeeeee dear..
    sanjeev,संजीव

  18. #38
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by rjuyal View Post

    Wow... i wanted this actually

    Thanks Sanjeev :)
    It's Ok Rakesh.

    Do you really know how both (== and equals()) are working and why output is different. Give some thoughts and then explain here.

    sanjeev,संजीव

  19. #39
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems that I got my banana back, ;)

    Here is an example I came to mind in a second.

    Java Code:
    public class WhisIsTheBest {
    
        public static void main(String[] args) {
            
            String strObjOne = new String("String");
            String strObjTwo = new String("String");
            String strObjThree = strObjOne;
     
            System.out.println("String 1 is " + strObjOne);
     
            System.out.println("String 2 is " + strObjTwo);
     
            System.out.println("String 3 is " + strObjThree);
     
            System.out.println(strObjThree == strObjOne);
     
            System.out.println(strObjThree == strObjTwo);
            System.out.println(strObjThree.equals(strObjTwo));
        }
    }
    What did you say now pal.

  20. #40
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Page 2 of 55 FirstFirst 12341252 ... LastLast

Similar Threads

  1. Hello, first time here.
    By ludragon in forum Introductions
    Replies: 2
    Last Post: 01-03-2008, 06:03 AM
  2. Help pls with a quiz
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 07:09 AM
  3. Time method
    By carderne in forum New To Java
    Replies: 5
    Last Post: 11-05-2007, 10:34 AM
  4. DataObject with the time given by me
    By garinapavan in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 07:33 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
  •