• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Are these two object the same

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
arthurwang
Asked:
arthurwang
1 Solution
 
objectsCommented:
they should be different, can you post your complete code
0
 
kawasCommented:
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
 
aozarovCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
 
arthurwangAuthor Commented:
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
 
aozarovCommented:
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
 
aozarovCommented:
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
 
arthurwangAuthor Commented:
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
 
aozarovCommented:
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
 
arthurwangAuthor Commented:
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
 
aozarovCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now