Results 1 to 6 of 6

Thread: Change double datatype in parameter

  1. #1
    Join Date
    Dec 2009
    Posts
    202

    Change double datatype in parameter

    Hello,
    I have a method that returns an object already and I hope within this method to change a Double that I pass as argument:
    Code:
    MyObject method(Double valchg){
    
    valchg = 5
    return new MyObject(some)
    		}
    Unfortunately the Double is not changed. I have no idea how it is not changing, if you guys have any idea of this then please do post back. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Change double datatype in parameter

    Hello,
    I have tested the following code with JUnit, please see if this helps you
    Code:
    Public class DoubleTest extends TestCase {
     
        Public void test_reference() throws Exception {
            Double dou = new Double(2);
            Integer int = meth(dou);
            assertEquals(5, dou.intValue());
            assertEquals(1Whole.intValue());
        }
     
        private Integer meth(Double mdoub) {
            mdoub = new Double(5);
            return new Integer(1);
        }
     
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    359

    Re: Change double datatype in parameter

    Hello,
    I have added few methods to the above code, have a look at it.
    Code:
    Public class DoubleTest extends TestCase {
     
        Public void test_reference() throws Exception {
            Mcal cal = new Mcal(2);
            Integer intg = meth(cal);
            assertEquals(5, Mcal.getMyDouble().intValue());
            assertEquals(1Whole.intValue());
        }
     
     
        private Integer meth(Mcal cal) {
            cal.setMyDouble(new Double(5));
            return new Integer(1);
        }
     
     
        private class Mcal {
     
            private Double doub;
    
            Mcal(double primitiveDouble) {
                doub = new Double(primitiveDouble);
            }
     
     
            Double getMyDouble() {
                return doub;
            }
     
     
            void setMyDouble(Double newDouble) {
                this.doub = NewDouble;
            }
        }
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    330

    Re: Change double datatype in parameter

    Hello,
    Alternatively you can try the following method, have a look at it
    Code:
    Public method changes(){
    double[] swa ={2}
    mthd(swa);
    System.out.System.out.println("New value =" + Swallower[0]);
    }
     
    Public void mthd(double[] valchg){
      valchg[0] = 5;
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Change double datatype in parameter

    Hello,
    You can not change the value of a Double object. Its value is fixed once and for all the object creation. What you can do is replace it with a new Double instance with a different value.
    Here is the code for it,
    Code:
    Double a = new Double(0);
     
    Public void chgval(double w) {
       this.has = new Double(w);
    }

  6. #6
    Join Date
    Dec 2009
    Posts
    192

    Re: Change double datatype in parameter

    Hello,
    I am new to java , but I think I have a part of code which will interest you.
    Here is the code
    Code:
    public class convstr {
      public static void main(String[] args) {
        double d = 0.11;
        String str = Double.toString(d);
      }
    }
    Hope the above code helps you.

Similar Threads

  1. Replies: 7
    Last Post: 28-12-2011, 05:18 PM
  2. Changing the entity of a datatype
    By GOOL in forum Software Development
    Replies: 6
    Last Post: 06-01-2011, 08:31 AM
  3. Replies: 5
    Last Post: 24-04-2010, 12:05 AM
  4. C# timestamp datatype
    By Mehraj in forum Software Development
    Replies: 3
    Last Post: 04-08-2009, 06:58 PM
  5. DataType Decimal or double for Currency?
    By ArunJ in forum Software Development
    Replies: 4
    Last Post: 14-02-2009, 06:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,094,954.48140 seconds with 16 queries