Fundamental '=' and '=='


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Fundamental '=' and '=='

  1. #1
    Join Date
    May 2005
    Posts
    115

    Fundamental '=' and '=='

    Code:
    	int i=5;
    	int j=0;
    
    	System.out.println("int i is: " +i);
    	System.out.println("int j is: " +j);
    
    	j=i;
    
    	System.out.println("int j is: " +j);
    Does the code j=i mean that the value of i is passed into j or the reference?

    I'm always confused with pass by reference and value.

    What is want to do is to pass the value 5 into j, yet when i code j==i and compile it, it says the line is not a statement. Can't compile..

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The value. Reference is for objects, ints are not objects.

    j==i is not a statement its a boolean.
    eschew obfuscation

  3. #3
    Join Date
    May 2005
    Posts
    115
    So == is only applicable when used with objects and boolean. With objects it is passed by reference, meaning both pointers would b actually pointing2 the same object..correct?

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    == can be used for both object and non-oblects (int, double, float, boolean, char &
    reference comparison).
    j==i is a boolean expression. For comparing objects content java uses the
    equals(Object ob) method as this language does not offer operator overloading.

    The answer is yes, == used for objects will be true if the references are for the same
    memory location.
    eschew obfuscation

  5. #5
    Join Date
    May 2005
    Posts
    115
    how is j==i a boolean expression? j and i are already both declared as int, how is it boolean?

  6. #6
    Join Date
    Aug 2005
    Posts
    5
    j==i is boolean in the sense of:

    if (j==i){ do stuff }

    if j = 5 and i = 4 then do stuff would not happen
    if j = 5 and i = 5 then do stuff would happen.

  7. #7
    Join Date
    Mar 2004
    Posts
    635
    use "=" to assign a value to a variable. Use "==" when comparing two values.

    j==i is saying that "does j equal i". The expression will either return TRUE or FALSE;

  8. #8
    Join Date
    May 2005
    Posts
    115
    thx guys..i've got it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center