Wednesday, July 30, 2014

Java Questions

Does Java pass by reference or pass by value?

 

Q: If Java uses the pass-by reference, why won't a swap function work?
A: Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.
Take the badSwap() method for example:
public void badSwap(int var1, int var2)
{
  int temp = var1;
  var1 = var2;
  var2 = temp;
}
When badSwap() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type from int to Object, since Java passes object references by value as well. Now, here is where it gets tricky:
public void tricky(Point arg1, Point arg2)
{
  arg1.x = 100;
  arg1.y = 100;
  Point temp = arg1;
  arg1 = arg2;
  arg2 = temp;
}
public static void main(String [] args)
{
  Point pnt1 = new Point(0,0);
  Point pnt2 = new Point(0,0);
  System.out.println("X: " + pnt1.x + " Y: " +pnt1.y); 
  System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
  System.out.println(" ");
  tricky(pnt1,pnt2);
  System.out.println("X: " + pnt1.x + " Y:" + pnt1.y); 
  System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);  
}
If we execute this main() method, we see the following output:
X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0
The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references. Figure 1 below shows two references pointing to the same object after Java passes an object to a method.


Figure 1. After being passed to a method, an object will have at least two references
Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.


Figure 2. Only the method references are swapped, not the original ones
O'Reilly's Java in a Nutshell by David Flanagan (see Resources) puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" As a result, you cannot write a standard swap method to swap objects.


Tony Sintes is a principal consultant at BroadVision. Tony, a Sun-certified Java 1.1 programmer and Java 2 developer, has worked with Java since 1997.
 
 
Source: http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html

 

Thursday, June 12, 2014

Largest Sum Contiguous Subarray

 Complexity is O(n)


public static void kandealgo ()
    {
        int[] numbers = { -1, -2 };
        int max_so_far = 0;
        int max_end_here = 0;

        boolean allnegative = false;
        int max_negative_value = 0;

        for (int i = 0; i < (numbers.length); i++)
        {
            if (numbers[i] > 0)
            {
                allnegative = true;
            }
            else
            {
                if (max_negative_value == 0 || max_negative_value < numbers[i])
                {
                    max_negative_value = numbers[i];
                }

            }

            max_end_here = max_end_here + numbers[i];

            max_end_here = Math.max(max_end_here, 0);
            max_so_far = Math.max(max_so_far, max_end_here);
        }

        if (!allnegative)
        {
            System.out.println("All values are negative in array , and min value is " + max_negative_value);
        }
        else
        {
            System.out.println("Max so far is " + max_so_far);
        }
    }

Thursday, December 26, 2013

Fix: SoapUI - 4.6.3 Test case

If you have installed newer version of soapUI chances are it wont work with mac
  • Installation problems




If your installed SoapUI instance on a Mac is not responsive, try the following steps to resolve the issue:
  1. In soapui-settings.xml file add or update the following line:
    true
  2. If that doesn't work, start SoapUI Pro with the following parameter added to the command line:
    -Dsoapui.jxbrowser.disable=true
  3. Alternatively, you can navigate to SoapUI*.app and do "Show Package Info" ( from popup menu ).
    Then you should see Contents directory. Open Info.plist file for editing where you can add:
    soapui.jxbrowser.disable
    true
  4. We have also seen some issues with SoapUI installs in Mac when GateKeeper is active. So to rule out that possible conflict, deactivate GateKeeper and try starting SoapUI

Also if you want to change the look and feel of SoapUI or SoapUI Pro to look more like a Mac application, change the following settings:
  1. Go to File > Preferences > UI Settings tab
  2. Check "Use native look & feel"
  3. Restart SoapUI Pro

Installing soapui

Once you've downloaded the installer the installation is pretty strait forward. Start it by just double-clicking on it. After preparing the installation which takes only a few seconds you'll see the starting screen:
mac-setup-start

Just click Next to continue. You'll be asked to accept our license agreement

mac-setup-soapui-lic-agreement

After accepting and clicking Next you'll be able to select the destination folder which by default is set to /Applications but can be changed.
mac-dest-install-folder

Next step gives you opportunity to optionally include, as additional components, soapUI source files and Hermes installation.

mac-install-components

In case of installing soapUI Pro JDBC drivers jars can be included as additional option (and of course no source is available).

mac-setup-wizard-select-comp-pro
After that a window appears where you'll be asked to select if desktop icon should be created and if jxbrowser should be disabled. Starting from version soapUI 3.6-beta2 JxBrowser is working on mac, but there is still a issue left with  browser being always displayed on top if opened.
mac-select-additional
Finally, by clicking Next, the installation starts
mac-setup-installing
If you have chosen to install Hermes the installation will be triggered next, with it's own info and license agreement
mac-start-hermes-installation
mac-hermes-info
mac-hermes-lic-agreement
and choosing the destination
mac-hermes-destination
mac-hermes-pacs
mac-hermes-listed
mac-hermes-install
mac-hermes-done

After couple of minutes most the installation should be finished and you should see the next screen:
mac-setup-wizard-finished
Now you are ready to use it. Good luck!