Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Reference in Java

Reference in Java

Scheduled Pinned Locked Moved Java
javadata-structurestutorialquestionlounge
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kujtim Hyseni
    wrote on last edited by
    #1

    Hello, this is a simple question I think. I am trying to get the array of bytes as a reference from method, although in the method in creates the array, outside returns the original initialized with null. How to get it from routine, but as parameter since routine returns boolean. Maybe routine should return class containing the array and boolean variable. This is the code:

    public class PassingRefs{

    static boolean varbyteobject(byte \[\] obj)
    {
    	System.out.println("---inside routine---");
    	int rndlen=(int)(Math.round(Math.random()\*50)+1);
    	
    	obj=new byte\[rndlen\];
    	System.out.println(obj.length);
    	
    	return true;
    }
    
    public static void main(String \[\] args){
    	byte \[\] inpobj=null;
    	varbyteobject(inpobj);
    	System.out.println("---out from routine---");
    	System.out.println(inpobj.length);
    }
    

    }

    and this is what I get after execution:

    ---inside routine---
    29
    ---out from routine---
    Exception in thread "main" java.lang.NullPointerException
    at PassingRefs.main(PassingRefs.java:18)

    Kujtim

    D F K 3 Replies Last reply
    0
    • K Kujtim Hyseni

      Hello, this is a simple question I think. I am trying to get the array of bytes as a reference from method, although in the method in creates the array, outside returns the original initialized with null. How to get it from routine, but as parameter since routine returns boolean. Maybe routine should return class containing the array and boolean variable. This is the code:

      public class PassingRefs{

      static boolean varbyteobject(byte \[\] obj)
      {
      	System.out.println("---inside routine---");
      	int rndlen=(int)(Math.round(Math.random()\*50)+1);
      	
      	obj=new byte\[rndlen\];
      	System.out.println(obj.length);
      	
      	return true;
      }
      
      public static void main(String \[\] args){
      	byte \[\] inpobj=null;
      	varbyteobject(inpobj);
      	System.out.println("---out from routine---");
      	System.out.println(inpobj.length);
      }
      

      }

      and this is what I get after execution:

      ---inside routine---
      29
      ---out from routine---
      Exception in thread "main" java.lang.NullPointerException
      at PassingRefs.main(PassingRefs.java:18)

      Kujtim

      D Offline
      D Offline
      David Skelly
      wrote on last edited by
      #2

      In Java, all method parameters are passed by value, not by reference. In other words, when you call varbyteobject in your code, it does not pass the reference to the original byte array. It passes a copy of the reference to the byte array. So, this line of code:

      obj=new byte[rndlen];

      has no effect on the original reference to the byte array in the calling main method since you are updating the copy, not the original. It is explained in more detail in this article: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html[^] You can just return the new byte array from your method. Why do you want to return a boolean?

      1 Reply Last reply
      0
      • K Kujtim Hyseni

        Hello, this is a simple question I think. I am trying to get the array of bytes as a reference from method, although in the method in creates the array, outside returns the original initialized with null. How to get it from routine, but as parameter since routine returns boolean. Maybe routine should return class containing the array and boolean variable. This is the code:

        public class PassingRefs{

        static boolean varbyteobject(byte \[\] obj)
        {
        	System.out.println("---inside routine---");
        	int rndlen=(int)(Math.round(Math.random()\*50)+1);
        	
        	obj=new byte\[rndlen\];
        	System.out.println(obj.length);
        	
        	return true;
        }
        
        public static void main(String \[\] args){
        	byte \[\] inpobj=null;
        	varbyteobject(inpobj);
        	System.out.println("---out from routine---");
        	System.out.println(inpobj.length);
        }
        

        }

        and this is what I get after execution:

        ---inside routine---
        29
        ---out from routine---
        Exception in thread "main" java.lang.NullPointerException
        at PassingRefs.main(PassingRefs.java:18)

        Kujtim

        F Offline
        F Offline
        fly904
        wrote on last edited by
        #3

        SPIRANCA wrote:

        static boolean varbyteobject(byte [] obj)
        {
        System.out.println("---inside routine---");
        int rndlen=(int)(Math.round(Math.random()*50)+1);

        obj=new byte\[rndlen\];
        System.out.println(obj.length);
        
        return true;
        

        }

        varbyteobject(inpobj);

        Why do you need to return a boolean? Why not just pass the byte[] back? Personally I would return an intialised byte[] if it was true and null if it was false.

        static byte[] varbyteobject(byte[] obj)
        {
        ...
        return obj;
        }

        //If varbyteobject doesn't return null (false).
        if ((inpobj = varbyteobject(inpobj)) != null)
        System.out.println(inpobj.length);
        else
        System.out.println("Array not initialised.");

        If at first you don't succeed, you're not Chuck Norris.

        1 Reply Last reply
        0
        • K Kujtim Hyseni

          Hello, this is a simple question I think. I am trying to get the array of bytes as a reference from method, although in the method in creates the array, outside returns the original initialized with null. How to get it from routine, but as parameter since routine returns boolean. Maybe routine should return class containing the array and boolean variable. This is the code:

          public class PassingRefs{

          static boolean varbyteobject(byte \[\] obj)
          {
          	System.out.println("---inside routine---");
          	int rndlen=(int)(Math.round(Math.random()\*50)+1);
          	
          	obj=new byte\[rndlen\];
          	System.out.println(obj.length);
          	
          	return true;
          }
          
          public static void main(String \[\] args){
          	byte \[\] inpobj=null;
          	varbyteobject(inpobj);
          	System.out.println("---out from routine---");
          	System.out.println(inpobj.length);
          }
          

          }

          and this is what I get after execution:

          ---inside routine---
          29
          ---out from routine---
          Exception in thread "main" java.lang.NullPointerException
          at PassingRefs.main(PassingRefs.java:18)

          Kujtim

          K Offline
          K Offline
          Kujtim Hyseni
          wrote on last edited by
          #4

          Possible solution would be to return a struct-class(since Java doesn't supports structs), so that it contains the byte array and the boolean variable(which tells about success or failure). Although we could check the success or failure from the byte array-some of the byte contain that info. Thanks anyway, Kujtim

          D 1 Reply Last reply
          0
          • K Kujtim Hyseni

            Possible solution would be to return a struct-class(since Java doesn't supports structs), so that it contains the byte array and the boolean variable(which tells about success or failure). Although we could check the success or failure from the byte array-some of the byte contain that info. Thanks anyway, Kujtim

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            Success or failure in what sense? Looking at the example code you have posted there is very little that can go wrong. The convention in Java is that success of an operation like this is assumed and you can expect a byte array to be returned: if anything goes wrong and the operation cannot be completed successfully, an exception is thrown. This may be a checked or an unchecked exception. There is a lot of debate over which of these is better, and I have no desire to open that debate up here (we'll be here forever while people argue backwards and forwards). Lets just say that if the operation fails, an exception is thrown and the type of exception tells you what went wrong. The calling method then catches the exception and responds accordingly. In other words, your method returns an intialised byte array. If the initialisation of the byte array fails, throw an exception indicating why it failed. I can't tell you what exception would be best because I don't know what you expect to go wrong from the example code you posted. You certainly could make it work by returning a class containing the byte array and a success/failure flag but this is not the preferred way of doing it in Java.

            K 1 Reply Last reply
            0
            • D David Skelly

              Success or failure in what sense? Looking at the example code you have posted there is very little that can go wrong. The convention in Java is that success of an operation like this is assumed and you can expect a byte array to be returned: if anything goes wrong and the operation cannot be completed successfully, an exception is thrown. This may be a checked or an unchecked exception. There is a lot of debate over which of these is better, and I have no desire to open that debate up here (we'll be here forever while people argue backwards and forwards). Lets just say that if the operation fails, an exception is thrown and the type of exception tells you what went wrong. The calling method then catches the exception and responds accordingly. In other words, your method returns an intialised byte array. If the initialisation of the byte array fails, throw an exception indicating why it failed. I can't tell you what exception would be best because I don't know what you expect to go wrong from the example code you posted. You certainly could make it work by returning a class containing the byte array and a success/failure flag but this is not the preferred way of doing it in Java.

              K Offline
              K Offline
              Kujtim Hyseni
              wrote on last edited by
              #6

              This very very simplified part of the large project I am developing. It will manage (register, unregister, list, ...) clients to listen/write at serial(COM) and TCP/IP. So the byte array is actually stream that commes(input arg) and goes(returned by function) by the so called "bridge" I am developing. Bytes of the stream contain the info from / to the requester. Hope this is more clear. Kujtim

              D N 2 Replies Last reply
              0
              • K Kujtim Hyseni

                This very very simplified part of the large project I am developing. It will manage (register, unregister, list, ...) clients to listen/write at serial(COM) and TCP/IP. So the byte array is actually stream that commes(input arg) and goes(returned by function) by the so called "bridge" I am developing. Bytes of the stream contain the info from / to the requester. Hope this is more clear. Kujtim

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                In that case, I would definitely use an exception to indicate failure. Personally I would use a checked exception in this case (puts on tin hat and runs for cover from the "I hate checked exceptions" brigade) but YMMV. Returning a success indicator is a very C way of doing things. Using a class as an equivalent of a struct to hold the byte array and a yes/no success flag is the result of trying to force C coding practices onto Java, which is why it feels clumsy and inelegant.

                K 1 Reply Last reply
                0
                • D David Skelly

                  In that case, I would definitely use an exception to indicate failure. Personally I would use a checked exception in this case (puts on tin hat and runs for cover from the "I hate checked exceptions" brigade) but YMMV. Returning a success indicator is a very C way of doing things. Using a class as an equivalent of a struct to hold the byte array and a yes/no success flag is the result of trying to force C coding practices onto Java, which is why it feels clumsy and inelegant.

                  K Offline
                  K Offline
                  Kujtim Hyseni
                  wrote on last edited by
                  #8

                  The remote user issues registration request from the GUI. For that request we build a stream which contains all the details. It is executed on the host module, if succeeds it receives the answer upon success, if not it receives failure message stream. We defined dedicated PROTOCOL for this. Any failure is catched and handled by try/catch internally. But the remote objects needs to communicate between, thus the protocol between them. Kujtim

                  modified on Monday, July 27, 2009 9:42 AM

                  D 1 Reply Last reply
                  0
                  • K Kujtim Hyseni

                    The remote user issues registration request from the GUI. For that request we build a stream which contains all the details. It is executed on the host module, if succeeds it receives the answer upon success, if not it receives failure message stream. We defined dedicated PROTOCOL for this. Any failure is catched and handled by try/catch internally. But the remote objects needs to communicate between, thus the protocol between them. Kujtim

                    modified on Monday, July 27, 2009 9:42 AM

                    D Offline
                    D Offline
                    David Skelly
                    wrote on last edited by
                    #9

                    Well, I thought I understood your problem, but now I'm confused. It sounds as if you are passing data cross-process using a custom protocol. If it's a dedicated protocol that you have designed yourself, you are free to pass data in whatever format you want. I'm not sure what that has to do with returning data from Java method calls and I'm not sure what answer you're expecting to get from this forum so I will bow out at this point.

                    1 Reply Last reply
                    0
                    • K Kujtim Hyseni

                      This very very simplified part of the large project I am developing. It will manage (register, unregister, list, ...) clients to listen/write at serial(COM) and TCP/IP. So the byte array is actually stream that commes(input arg) and goes(returned by function) by the so called "bridge" I am developing. Bytes of the stream contain the info from / to the requester. Hope this is more clear. Kujtim

                      N Offline
                      N Offline
                      Nagy Vilmos
                      wrote on last edited by
                      #10

                      Am I right that what you require is that any change to the stored data is propogated back up the line to anyone interested? If so, then this sounds more like a subscribe/publish pattern.


                      Panic, Chaos, Destruction. My work here is done.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups