Raised This Month: $ Target: $400
 0% 

Java Scripting


Post New Thread Reply   
 
Thread Tools Display Modes
Sean D
Junior Member
Join Date: Apr 2006
Old 03-03-2009 , 11:25   Re: Java Scripting
Reply With Quote #21

Quote:
Originally Posted by YamiKaitou View Post
Can you say more than "won't work"? Integer is part of the Object class from what I can remember. It has been a about a 1.5 years since I messed with Java and almost 3 since I messed with the Integer class. I might just test it unless you can tell me. Integer and int are seperate classes, so don't mix them together if that is what you are doing.

Nvm, tested it and it does seem that it does not pass it as a Object
it's already been covered:

Quote:
Originally Posted by Sean D
the alternative to what's already been said is to create your own wrapper class, which takes like 5 seconds, but it's a real pain that java's Integer class (and other numeric types) are immutable
Sean D is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 03-03-2009 , 15:23   Re: Java Scripting
Reply With Quote #22

Quote:
Originally Posted by Sean D View Post
it's already been covered:
Sean, the problem here is not the fact that Integer class is immutable.

If you do

Code:
Integer x = new Integer(4);
x = new Integer(3);
x will be 3 despite being its class immutable. But, if you give it to a function, inside it, you will not be able to change the value of the outside x.

Last edited by joaquimandrade; 03-03-2009 at 15:27.
joaquimandrade is offline
Sean D
Junior Member
Join Date: Apr 2006
Old 03-03-2009 , 16:51   Re: Java Scripting
Reply With Quote #23

the fact that it's immutable has everything to do with it. contrary to what's been said, java passes everything to methods by value, which means that even references to objects are passed by value. so, if you pass an immutable object by value to a method, its internal data won't be changed, and that's the only reason why. if you do the following, you can easily change values:

Code:
public class Int
{
	private int m_x;

	public Int(int x) { m_x = x; }
	public int get() { return m_x; }
	public void set(int x) { m_x = x; }
}

public class Main
{
	public static void main(String [] args)
	{
		Int y = new Int(3);
		func(y);
		System.out.println(y);	   // output is 5
	}

	public static void func(Int i)
	{
		i.set(5);
	}
}
Quote:
x will be 3 despite being its class immutable.
you cannot use the assignment operator (as in C++, for example) because there is no operator overloading in java, so the concept of references can't be implemented in that fashion, so i'm not sure why that was even of debate. they can be implemented indirectly using the method above.

Last edited by Sean D; 03-03-2009 at 16:55.
Sean D is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 03-03-2009 , 17:07   Re: Java Scripting
Reply With Quote #24

Quote:
Originally Posted by Sean D View Post
the fact that it's immutable has everything to do with it. contrary to what's been said, java passes everything to methods by value, which means that even references to objects are passed by value. so, if you pass an immutable object by value to a method, its internal data won't be changed, and that's the only reason why. if you do the following, you can easily change values:

Code:
public class Int
{
    private int m_x;

    public Int(int x) { m_x = x; }
    public int get() { return m_x; }
    public void set(int x) { m_x = x; }
}

public class Main
{
    public static void main(String [] args)
    {
        Int y = new Int(3);
        func(y);
        System.out.println(y);       // output is 5
    }

    public static void func(Int i)
    {
        i.set(5);
    }
}
you cannot use the assignment operator (as in C++, for example) because there is no operator overloading in java, so the concept of references can't be implemented in that fashion, so i'm not sure why that was even of debate. they can be implemented indirectly using the method above.
The question here is that we are talking about two different things.

When you pass a variable to a function you can't change "to where it is pointing" cause, inside it you only have the value of what is being pointed to (what has nothing to do with its type being immutable), and you also can't change its given value because it is immutable. If it was passed by reference or if it was mutable you could change it.
joaquimandrade is offline
Sean D
Junior Member
Join Date: Apr 2006
Old 03-03-2009 , 17:57   Re: Java Scripting
Reply With Quote #25

i realize they are two different things; i was trying to throw a subtle insult at your intelligence by commenting on how you shouldn't have even been talking about java's assignment operator in the first place, because of how basic of a concept it is. i didn't anticipate, however, your utter genius by dumbing it down even further when you just explained how the assignment operator works, probably already knowing that you're preaching to the choir. so i've obviously walked into a checkmate and will surrender this victory to you. but you haven't won the war...
Sean D is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 03-03-2009 , 18:16   Re: Java Scripting
Reply With Quote #26

Quote:
Originally Posted by Sean D View Post
i realize they are two different things; i was trying to throw a subtle insult at your intelligence by commenting on how you shouldn't have even been talking about java's assignment operator in the first place, because of how basic of a concept it is. i didn't anticipate, however, your utter genius by dumbing it down even further when you just explained how the assignment operator works, probably already knowing that you're preaching to the choir. so i've obviously walked into a checkmate and will surrender this victory to you. but you haven't won the war...
haha you are funny.
joaquimandrade is offline
bomnacama
Senior Member
Join Date: Dec 2008
Location: Portugal
Old 03-03-2009 , 19:23   Re: Java Scripting
Reply With Quote #27

Quote:
Originally Posted by joaquimandrade View Post
haha you are funny.
__________________



Please FEED MY HAMSTERS by giving me +karma
bomnacama is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:37.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode