a few questions...

Monkey Forums/Monkey Beginners/a few questions...

dubbsta(Posted 2016) [#1]
1. wondering what something like (new int) means
2. how to properly use (return)
3. whats the difference between (method new() ) and (method variable() )


Xaron(Posted 2016) [#2]
1) In object oriented languages almost everything is an object, well in languages like Smalltalk this means even basic data types like int. When you have an object you usually cannot work with it as it's just kind of a blue print. So you have to instantiate it. That's done with the "new" statement. This will create an instance of an object. So when you have a class A, you do new A() and can work with the instance A then.
For basic data types you don't have to do this in Monkey, you can just use them. Stuff like "new Int[]" is used to create arrays of integers for instance. So you allocate memory for this array.

2) Well... return does what it does. It just "returns" to the previous caller. When you call a method A and have return in there, it just continues after that method call A. Plus you can return values as well, e.g. your method or function calculates something and returns that value back to the caller.

3) "Method New()" is somehow the constructor in Monkey. So when you instantiate an object (by calling "New A()", this method "New" is then called. You can do initialization stuff in there. Other methods can do whatever you want them to do, calculate stuff, display things or whatever.