?
Solved

Are these two object the same

Posted on 2005-03-27
11
Medium Priority
?
200 Views
Last Modified: 2010-03-31
I have a little question here:

For example:

MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();

static Stack stack = new Stack();

then
I insert both objects into the stack like:

stack.push(obj1);
stack.push(obj2);
........

then I take both objects out of the stack,

newObj1 = stack.pop();
newObj2 = stack.pop();

if I compare these two objects by:

if(newObj1 == newObj2)
   System.out.println("same");
else System.out.println("different");

will the result be the same?
i though the result would be different, because two objects refer to different memoery location even inside the stack.
I tested and got the "same" answer, but does not
understand why it's the same,, can anyone give me an explanation?I


0
Comment
Question by:arthurwang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13641121
they should be different, can you post your complete code
0
 
LVL 13

Expert Comment

by:kawas
ID: 13641171
If your 2 objects are primitive types, then equality is straight forward and subject to promotion rules, for example, a float value of 10.0 is considered equal to a byte value of 10.. If the objects are not primitives, the value in the comparison operator is taken as the reference to the object, usually the memory address. If you are using java 1.5, watch out, because you may get different results using == and .equals because of the java 1.5s new feature of boxing and unboxing variables.

Eddie
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13641830
That is strange.
Are you sure you are comparing newXX to newXX?
Regardless the name that you picked for newXX
is wrong.

The reason is:
stack.push(obj1);
stack.push(obj2);
........

Stack is First in Last out or (which is the same) Last in first out
So when you do the first

newObj1 = stack.pop();

You are Basically getting obj2

and when you do
newObj2 = stack.pop();
You are getting obj1
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 30

Expert Comment

by:Mayank S
ID: 13643479
>> If your 2 objects are primitive types

Doesn't look like the case because of: >> MyClass obj1 = new MyClass();

If you were putting two constant String literals having the same value into the Stack, then I would've thought that it was because of the StringPool. But since its MyClass, I guess you will need to post your full code so that we can find out why this is happening.
0
 

Author Comment

by:arthurwang
ID: 13646675
Thanks everyone's comment, here, Myclass refer to an OracleConnection which is located inside a singleton class ConnectionBean as below:

import java.io.*;
import java.util.*;
import java.sql.*;

import oracle.sql.*;
import oracle.jdbc.driver.*;
import oracle.jdbc.driver.OracleTypes;
import oracle.jdbc.driver.OracleDriver;
import oracle.jdbc.pool.OracleDataSource;

import oracle.ord.im.OrdMediaUtil;

public class ConnectionBean {

    private final static String JDBC_CONNECT_STRING =
    "jdbc:oracle:thin:@XXXXX:1521:ORCL";

    private final static String JDBC_USER_NAME = "XXXXXX";
    private final static String JDBC_PASSWORD  = "XXXXXX";


    private static Stack connStack = new Stack();

    private static boolean driverLoaded = false;

    public static final ConnectionBean connBean = new ConnectionBean();

    private ConnectionBean (){};

    private OracleConnection conn;



    public OracleConnection getConnection()
        throws SQLException
    {

        synchronized( connStack )
        {

            if ( !driverLoaded )
            {

                DriverManager.registerDriver(
                                    new oracle.jdbc.OracleDriver() );
                driverLoaded = true;

            }

            if ( connStack.empty() )
            {

                                           OracleDataSource ods = new OracleDataSource();
                                           ods.setUser(JDBC_USER_NAME);
                                           ods.setPassword(JDBC_PASSWORD );
                                           ods.setURL(JDBC_CONNECT_STRING);
                                     conn = (OracleConnection)ods.getConnection();
                try
                {
                    OrdMediaUtil.imCompatibilityInit( conn );
                }
                catch ( Exception e )
                {
                    throw new SQLException( e.toString() );
                }
            }
            else
            {
                conn = (OracleConnection)connStack.pop();
            }
        }//synchronized

        conn.setAutoCommit( true );
        return conn;
    }

    public void freeConnection()
    {

        if ( conn != null )
        {
            synchronized( connStack )
            {
                        System.out.println();
                connStack.push( conn );
            }
        }

    }//freeConnection

}//class

then, in an application file, I have a reference in the same type:

private OracleConnection conn1;
private OracleConnection conn2;
 private ConnectionBean connBean = ConnectionBean.connBean;

then I put the comparison source code inside a function:

public String compare(String username, String password)
    throws ServletException, IOException, SQLException
       {
               if ( conn1 == null )
                {
                    conn1 = connBean.getConnection();
                }
                   if ( conn2 == null )
                {
                    conn2 = connBean.getConnection();
                }

                   if(conn1 == conn2) System.out.println("same");
                   else System.out.println("different");
             ...............some database access code here....................
             if ( conn1 != null )
            {
            connBean.freeConnection();
            conn1 = null;
            }
        if ( conn2 != null )
            {
            connBean.freeConnection();
            conn2 = null;
            }

//above code will get the result as "different", because the stack is empty before, both object /conn1 and conn2 are created from new memory location.
//but after insert both into the stack, I run this code again, since at this moment, the stack already have two OracleConnection Object inside, then , the result shows:"same", and why?
0
 
LVL 15

Accepted Solution

by:
aozarov earned 200 total points
ID: 13646759
Youre freeConnection method is wrong:
Your free connection should get the connection as an argument and put it back in the stack
instead of using a member variable which will basically have the value of the recent getConnection.
So in your case the last getConnection returns conn2 and this connection will be put twice when you call free connection.
Change:
public void freeConnection()
    {

        if ( conn != null )
        {
            synchronized( connStack )
            {
                    System.out.println();
                connStack.push( conn );
            }
        }

    }//freeConnection

to
public void freeConnection(Connection connection)
    {

        if ( connection != null )
        {
            synchronized( connStack )
            {
                    System.out.println();
                connStack.push( connection );
            }
        }

    }//freeConnection

and remove the member variable conn.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13646793
Of course when you do so you will have to declare it as a member variable of the getConnection method or else
change the way that method returns the connection.
So, basically replace:
private OracleConnection conn;



    public OracleConnection getConnection()
        throws SQLException
    {

with

    public OracleConnection getConnection()
        throws SQLException
    {
OracleConnection conn = null;
0
 

Author Comment

by:arthurwang
ID: 13647671
Thank you very much for the help, Aozarov, I really appreciate it. But I have a small question here for the above code:

       if ( conn != null )
        {
            connBean.freeConnection(conn);------------------(1)
            //conn.close();     //why Can not close the connection-------(2)
            conn = null;
        }

Follow by returning the conn object back to the stack of ConnectionBean(1), I tried to close the connection (2), then whenever I want to get the connection again from any other classes, or simply run the source code again, I got exception for connection closed.
For example:

class 1{
private OracleConnection conn;
 private ConnectionBean connBean = ConnectionBean.connBean;
................
                   if ( conn == null )
                {
                    conn = connBean.getConnection();
                }
.................
if ( conn != null )
        {
            connBean.freeConnection(conn);
            conn.close();
            conn = null;

        }

then in class2
class 2{
private OracleConnection conn;
 private ConnectionBean connBean = ConnectionBean.connBean;
................
                   if ( conn == null )
                {
                    conn = connBean.getConnection();
                }

then I got closed exception here, I thought, once the object conn was returned back to the stack by calling above statement (1), conn is free to close because it's a class memeber of class1, have nothing to do with the stack or the object inside the stack any more, but why once it's closed, the object inside the stack can not be used any more? I am not sure about the mechanism of conn.close(). I just assume any database object is like a piple, one side is closed, it become useless!


 

0
 
LVL 15

Expert Comment

by:aozarov
ID: 13647733
If you close a connection then you can't use it anymore (there is no "open" method).
If your ConnectionBean is basically pooling connection then you should never close the connections (unless you want to get rid of them).
BTW, if you need a good and free connection pooling library then have a look at: http://jakarta.apache.org/commons/dbcp/ 
Most of the libraries that supports connection pooling will wrap the connection that is returned by them and when you call close on the wrapped connection
the wrapper will return the real connection to the pool. So the fact that this connection is pooled is transparent to the application.
0
 

Author Comment

by:arthurwang
ID: 13782370
Hi, aozarov,

I met a big problem, I want to upload file and save it at hard disk by using struts, but I can not figure it out and looks like no one can help me out:

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21376124.html

from my experience, I think you are an experts, can you look at the question above and help me out? the original question is to ask upload without form bean, but I just think it's ok if you can show me some code that you can upload a file and save in the disk by using struts, please, I really don't know where to look for help, and seems like no one can answer my question.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13783563
I replied on that thread.
Try it and I will be happy to assist if you have further issues.
I mentioned there that sturts comes with an example of how to do file upload.
This can be found at: webapps/struts-examples.war.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question