Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: return value chaned when get back from function call

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default return value chaned when get back from function call

    Hi,

    I have the following classes:

    public class CGenericData implements Serializable {}

    public class CGpsMsgData {
    public int x;
    public int y;
    public int z;
    }

    public class CGpsMessage extends CGenericData {
    public CGpsMsgData cGpsMsgData;
    }

    I'am using UDP to send/recv messages:
    public void Send(CGenericData cGenericData) {
    try
    {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(outputStream);
    os.writeObject(cGenericData);
    byte[] data = outputStream.toByteArray();
    m_cDatagramPacketSendPacket.setData(data);
    m_cDatagramPacketSendPacket.setLength(data.length) ;
    m_cDatagramSocket.send(m_cDatagramPacketSendPacket );
    }
    catch (IOException e1)
    {
    e1.printStackTrace();
    }
    }


    public void Receive(CGenericData cGenericData) {
    try
    {
    m_cDatagramSocket.receive(m_cDatagramPacketReceive Packet);
    byte[] byteData = m_cDatagramPacketReceivePacket.getData();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteData);
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    CGenericData cGenericData1 = (CGenericData) objectInputStream.readObject();
    cGenericData = cGenericData1;
    }
    catch (IOException e)
    {
    return;
    }
    catch (ClassNotFoundException e)
    {
    return;
    }
    }

    I have something strange:
    I get the Gps messages I send ,
    and when I look on the cGenericData at Receive function before it return to the caller, I see the values I sent.
    But !!!
    when I look on it after thee function call (from the main) it has default values, why ? (it is not like pointers in c++) ?

    Thanks


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: return value chaned when get back from function call

    Quote Originally Posted by amit1983 View Post
    public void Receive(CGenericData cGenericData) {		
    	try 
    	{
    		m_cDatagramSocket.receive(m_cDatagramPacketReceivePacket);
    		byte[] byteData = m_cDatagramPacketReceivePacket.getData();			
    		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteData);
    		ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    		CGenericData cGenericData1 = (CGenericData) objectInputStream.readObject();
    		cGenericData = cGenericData1;   // <------- NOT RIGHT				
    	} 
    [....]
    Arguments in Java are always passed by value. Even a reference is passed by value. When you invoke Receive you are really passing a copy of the reference pointing to your CGenericData object.

    In the line I have highlighted in the comment, you are assigning a new object to the cGenericData parameter. You cannot change the value of the variable in the main (because the cGenericData parameter is distinct and with a copy of the reference) .... at most you can change the internal state of the passed object.

    So doing:
    cGenericData = cGenericData1;

    is not the right thing. What is the solution? The most simple: change the state of the object the Receive receives. Or, conceptually better, return the new object.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: return value chaned when get back from function call

    Hi,

    Thank's for your answer.

    1. What do you mean when you write: " change the *state* of the object the Receive receives" ?
    (can you give me an example ?)

    2. If I want to return byte[], is it possible ?
    if so, how I can deliver the length of the byte[] ?

    Thank's again

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: return value chaned when get back from function call

    Quote Originally Posted by amit1983 View Post
    1. What do you mean when you write: " change the *state* of the object the Receive receives" ?
    (can you give me an example ?)
    If your class has public fields (and this is your case), simply access those fields (e.g. obj.field = something;). However having public fields is not generally a good "design". It's better to keep fields private and provide public "accessor" methods like setXyz/getXyz. This is a basic concept about Java "beans". For example: obj.setXyz(.....);

    This is the meaning of "change the state of the object".

    Quote Originally Posted by amit1983 View Post
    2. If I want to return byte[], is it possible ?
    if so, how I can deliver the length of the byte[] ?
    Yes, it's possible to return a byte[]. Every array has an implicit length field. So the length is always known.

    But in your case, note that if you return a byte[], its content is a serialized object. And deserialization is the responsibility of the caller. This is not a very good design. You should hide this aspect into the method, like you have done in the Send method.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Need help understanding why my return value is not returning back.
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2013, 08:17 PM
  2. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 02:21 PM
  3. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum Object Oriented Programming
    Replies: 3
    Last Post: August 12th, 2011, 04:42 AM
  4. Need help with my double return function.
    By thisbeme in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 30th, 2011, 09:50 AM
  5. Problem with Return Function
    By Tracy22 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2010, 03:32 PM