Quark's musings

edited May 2016 in Share Your Work

I have been on this forum for just over 7 years now and in that time I have answered many questions (not always successfully :\"> ) but on many occasions I felt that while the answers helped the original poster a more in-depth analysis of the problem and solution would benefit many others.

So I have decided to start my own blog to do just that. I have just published my first post entitled "frameRate, frameCount and miilis" that I hope you will find interesting and informative. There will be more to follow as and when I find time.

Click to visit Quark's musings

Comments

  • That painting is beautiful. Metallurgy is super cool. I'm a welder that used to work on 747 engines among several others using an Electron Beam Machine. Good ol' JT8D :)

    I really support this blog idea. Almost like a reference for the reference! haha
    That framerate() v frameRate video explains a problem I was having a while back. trying to get something synced. Never thought to look at that. Thanks!

    I think comparing millis() and System.nanoTime() might be useful as well.

  • I did several years in non-destructive testing and x-rayed, gamma-rayed and ultrasonically tested miles of welding in my time.

    Yes there are issues with using nanoTime that could be usefully discussed.

  • BTW I am no artist I didn't create the painting :)

  • That's so cool!! B-) Did you use isotopic or electronic for the gamma ray test? Heh, I put my doseometer on the EB control panel for only two days one time and it came back with 200x levels. Turns out there was only 1/r^2 "shielding" with a few mils of Pb...didn't work there much longer after that. Went to college for physics instead.

  • Used Iridium 192 sealed isotope - great fun!

  • edited April 2016

    Holy crap, that's a strong ray! I was exposed to <100kev range. Beyond that, the base metal would turn to vacuum filled foam. :D You should have seen the 1 inch tungsten block we used for calibration. heheheh

  • That's great! A lot of recurring issues on here don't have a tutorial yet, that's one less now... (I think I even saw this topic mentioned on the TODO list on P5's Github).

    Went to college for physics instead.

    You have my condoleances.

  • @colouredmirrorball glad you approve. BTW lay off the physics, along with chemistry and maths it was one of my favourite subjects at school O:-)

    I am planning posts on 2D geometry with particular emphasis on techniques used in programming 2D games but I am happy to take suggestions.

  • edited April 2016

    You have my condoleances.

    My grandfather was the blind physicist for the ENIAC. :) He could visualize everything going on. He also worked at Camp Century doing radar equations during the cold war. It's said he could find the answer to most Maxwell Equations before his colleagues finished writing things down. I have a love for reality like he did. Your empathy is appreciated, The USA's education system sucks.

  • BTW lay off the physics

    I joke, I have a BSc in physics but it was much harder to achieve than I anticipated! So anyone attempting the same feat can count on my sympathy. I switched masters though, now doing nanotechnology engineering instead of continuing physics. Meanwhile I discovered Processing and programming but I decided if I pursued a career in programming, it would take the fun out of it right away! (Not entirely unlike it did with physics...)

    Cool to learn about your grandfather... I have enough trouble with the Maxwell equations even when using stacks and stacks of paper!

  • That's awesome! I had trouble financially. Man, I feel bad about going so off topic.

    Hey, What are some of the things you guys see over-posted?
    I am seeing a bunch of people with organizational problems.
    Personally I think going over the referencing function of assign and how to know when it's using what feature. That's not covered on the page for assign and really easy to get gnarly problems if you're unaware of it.

    Or what about the public, private, static, super, this relationships? Those aren't covered very well either.

  • public, private, static, super, this

    Of course these are all part of object orientation, a paradigm Processing worked hard at hiding from the user. This is fine for the most basic sketches and helps people get started with programming.

    As the programmer progresses onto larger projects the level of code-complexity increases exponentially. In many cases OOP can significantly reduce complexity and bring many other benefits.

    The problem is that beginners learn the syntax to create classes and objects but don't realise there is a lot more to OOP than the syntax. A principle of OOP is that a class should autonomous, in other words it should be responsible for updating its own attributes/fields. I often see poorly designed, non-autonomous classes which doesn't significantly reduce the level of complexity.

    Processing does have a tutorial on classes and objects perhaps I should read it :D

  • edited April 2016

    This is advanced as the videos get. Just the difference between copy and reference.
    The P3 reference has poor coverage as well.
    https://processing.org/reference/super.html
    Name: super
    Description: Keyword used to reference the superclass of a subclass.
    ooooh now I get it??? huh? lol

  • Interesting I have never watched this video before, shame about the errors.

    1) I have never heard of pass by copy the correct term is pass by value. It could have been a slip of the tongue, or some terminology invented for Processing, so might forgive that one.

    2) In the video the speaker says that objects are passed by reference. WRONG in Java ALL function parameters are passed by value NEVER by reference. It is more accurate to say that Java passes object-references by value. This might seem like splitting hairs and a newbie is unlikely to notice the difference but when looking at sorting and copying objects the difference is important.
    In C++ the programmer can specify whether a parameter is passed by value or by reference, but not in Java, everything is passed by value.

  • edited April 2016

    @AverageJoe, I'm afraid Shiffman's explanation about passing arguments to functions is very flawed! :-&

    His 1st example void change(int val) {} is OK. Nailed it 100%!
    However its reference counterpart doesn't exactly match the former and it's very misleading! :-\"

    B/c in the former, the assignment operator is used directly over the local parameter: val = val * 2;
    However, in the latter example, the assignment acts upon some object's field which the parameter points to: aP.x = 300;

    Here's a fairer example of an attempting of reassign passed variables inside a function: O:-)

    // forum.Processing.org/two/discussion/16299/quark-s-musings
    // GoToLoop (2016-Apr-28)
    
    void setup() {
      int x = 50;
      doubleVal(x);
      println("x:", x, ENTER);
    
    
      PVector vec = new PVector(50, 50);
      doubleVal(vec);
      println("vec:", vec);
    
      exit();
    }
    
    static final void doubleVal(int val) {
      val *= 2; // Primitive reassignment
      println("DoubleVal():", val);
    }
    
    static final void doubleVal(PVector vals) {
      vals = vals.copy().mult(2); // Reference reassignment
      println("DoubleVal():", vals);
    }
    
  • I have never heard of pass by copy the correct term is pass by value.

    @quark, I guess he's meant pass a copy of a value. Which is actually what really happens:
    All function's parameters are assigned the copy of the values passed to it.

  • edited April 2016

    In short, no matter the datatype, everything stored in a variable is a value.
    And Java always pass the copy of that value to a function's parameter. :-B
    In other cases, the result of an expression as a primitive value. :P

  • This sketch proves that Java does not use pass by reference for objects.

    If Java used pass by reference the the output would be

    9 1 1

    but the actual output is

    9 1 9

    showing the value stored in the object reference foo has not been changed.

    class Foo {
      int fn;
    
      Foo(int nbr){
        fn = nbr;
      }
    }
    
    void setup(){
      Foo foo = new Foo(9);  // foo is an object reference
      println(foo.fn);
      // the object reference stored in foo is copied into the parameter f
      anotherFoo(foo);
      // the object reference stored in foo has NOT been changed.
      println(foo.fn);
    }
    
    // the parameter f is a copy of the object reference used in the function call
    void anotherFoo(Foo f){
      // changes the object reference stored in the parameter
      f = new Foo(1);
      println(f.fn);
    }
    

    It is important for programmers to understand the difference between object references and objects

  • It is important for programmers to understand the difference between object references and objects.

    1. Object references: the 1st memory address value of some contiguous allocated block of memory.
    2. Objects: Some contiguous allocated block of memory.
      In Java they contain non-static fields and some other extra header data.
      For example, the memory address of the class which it was used to instantiate it.
  • In computer programming "pass by value" and "pass by reference" are clearly defined as to their meaning and that meaning is fixed irrespective of the programming language being discussed. So when I mentioned the pass by copy it was a bit tongue in cheek since it was clear from the context what he meant.

    It is important for programmers to understand the difference between object references and objects

    What you say is correct but I didn't make it clear what I meant by this. I meant that programmers should be aware that the statement, Foo foo; declares an object reference of type Foo and NOT an object of type Foo.

    Understanding this is important when
    * using collections (especially when the same object reference is being stored in more than one collection)
    * sorting objects (actually involves rearranging object references
    * copying / cloning objects

    It is extremely common among all Java programmers to talk about objects when they really mean object references and this is often misleading for OO novices.

  • edited April 2016

    ... statement Foo foo; declares an object reference of type Foo and NOT an object of type Foo.

    • IMO, what confuses most of us is that we think that variables & objects are the same. :O)
    • In most programming languages, variables generally take up 4 bytes (32 bits).
    • But they can vary from 1 byte (8 bits) up to 8 bytes (64 bits).
    • I dunno of any languages which feature variables w/ more than 8 bytes. But they may exist. :-?
    • When we do: PVector vec = new PVector(50, 50);, there's no way that a whole PVector object would fit into vec, which is generally only 4 bytes (32 bits).
    • What is stored in vec, within those 4 bytes, is the memory address which refers to the contiguous allocated block of memory, reserved for the PVector object and its non-static fields. :-B
  • what confuses most of us is that we think that variables & objects are the same.

    I think you mean primitives & objects. After all in the statement
    PVector vec = new PVector(50, 50);

    vec is a variable of type PVector reference

  • edited April 2016

    I think you mean primitives & objects.
    vec is a variable of type PVector reference.

    Hmm... That wasn't my focus there. 8-|
    I just wanted to call the attention that vec is simply a variable.
    And variables have a very limited fixed # of bytes in order to store its value.
    Datatypes, no matter whether they're int, double, boolean or a reference, they're simply values.

    For the sake of this example, let's say that new PVector() allocates a contiguous block of memory starting @ memory address 100 000.
    Therefore PVector vec = new PVector(); assigns value 100 000 to variable vec.

    When we do PVector panda = vec;, it means both panda & vec variables hold the same value 100 000.
    Which btW points to the very same PVector object allocated by new PVector() at the beginning.

    The same rule applies for any container:

    PVector vec = new PVector();
    PVector panda = vec;
    
    PVector[] oranges = new PVector[10];
    oranges[5] = vec;
    

    Given that vec = panda = oranges[5], what is new PVector() exactly?
    A vector, a panda or 1 of the those 10 oranges??? 8-}
    The answer is neither. Objects don't have names; variables do!!! :O)
    And objects are too big to fit into variables after all.
    Variables can only hold 1 limited value and that's it. :-h

    Now what happens when we pass any variable (or expression) as an argument to invoke some function?

    PVector vec = new PVector(50, 50);
    doubleVal(vec);
    println("vec:", vec);
    
    static final void doubleVal(PVector vals) {
      vals = vals.copy().mult(2); // Reference reassignment
      println("DoubleVal():", vals);
    }
    

    Let's say vec holds value 200 000 this time.
    1st at doubleVal(vec);, the value 200 000 stored in variable vec is read.
    Then a copy of that value is sent to function doubleVal().
    Once arrived, doubleVal()'s local parameter vals is assigned that value 200 000.

    At vals.copy(), the value 200 000 is read from vals and then the deference operator dot . reaches the object at memory 200 000: https://Processing.org/reference/dot.html

    Objects don't store methods nor static fields. Therefore the object redirects itself, sending along its this, to the original PVector class, where its method copy() actually resides.

    Method copy() instantiates another PVector w/ the same values for fields x, y & z.
    Let's just say this new object is created at memory address is 250 000.

    For the 2nd method in sequence we now got: 250 000.mult(2);
    Finally mult() returns value 250 000, and it's thus reassigned to local parameter vals.

    Given that vals is a local parameter variable of method doubleVal(), the original variable vec continues to hold the value 200 000 as before, unaffected by the reassigned of vals.

    Whew! I've got carried away. #:-S That's all folks! :-bd

  • So I think we can all agree that I will eventually do a post on "Object References and Objects". :)

  • edited April 2016

    Agreed a lot! But please include some section which explains the differences between a variable and an object or any type of value. L-)

  • edited May 2016

    When we do PVector panda = vec;, it means both panda & vec variables hold the same value 100 000.



    Two new examples to add to my example collection! These are some good ones too. Well, I guess this explains where I went wrong with another project from last year using XML.

    That PortalsForDeveloping is an interesting idea, Chrisir.
    It led me to static void games which has an interesting topic of discussion:
    http://staticvoidgames.com/tutorials/basicJava/fromProcessingToJava

    Public, not surprisingly, is the opposite of private. In Processing, anything declared without an access modifier is public. You've been using the public access modifier this whole time, but it's a good idea to explicitly specify it rather than leaving it up to the default. This might seem like extra syntax, but as you work on bigger projects or on a team, or as you switch over to Java, explicitly specifying the access modifier of every variable and function will become much more important. Better get into the habit now!

  • Thank you

    I think everybody can contribute there

  • edited May 2016

    @AverageJoe, what value do you think it's actually stored in any variable after all?
    If an object is allocated @ memory address 100 000, if we assign it to some variable, that's exactly the value that variable's gonna hold, spread by 4 or 8 bytes! :>

  • Yeah it's really interesting how the assign operator works in Java. Also kinda surprising how there is so much confusion about it. I think a blog topic comprehensively covering when something is copied or referenced would be useful.

  • edited May 2016

    Also kinda surprising how there is so much confusion about it.

    • That confusion derives from ignorance about how machines actually work inside.
    • Languages like C, C++ and even Java favor machine behavior over human expectation.
    • What folks need to be aware of is that variables are merely labels for some very short contiguous sequence of bytes within memory. Most common is 4 bytes. Largest is 8 bytes:
      http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
    • There's no way any object could fit into so few number of bytes!
    • That's why the only information kept in reference type variables is the value of the 1st logical memory address of an object.
    • Which btW is a contiguous allocated block of memory. Generally in the heap region.
    • In short, everything is a value spread by some few bytes within memory.
    • But the value needs to be interpreted to its intended task later.
    • Such as fixed-point, floating-point and memory reference values.
    • Those are basically all of the actual category types which Java stores in all of its variables!
  • edited May 2016

    Another point of contention are the equality == & inequality != operators.

    Most folks, after being beaten up by them a couple of times, already know that we shouldn't use == nor != in order to compare String objects, but their equals() method instead. b-(

    That's exactly the classic case of human expectation being trampled by machine oriented design that most "classic" programming languages like C & Java favor over the former. /:)

    Human mind thinks about the char[] content of the String.
    While Java thinks about the 1st logical memory address of the contiguous allocated block of memory which encapsulates the non-static fields of the String object.

    When we use == or != in Java, we're actually comparing the true values held by 2 variables.
    But we tend to think we're comparing the content of the objects in which those values refer to.

    What fewer folks know about is that Java collects all String literals and pre-allocate them.
    In the end, "equal" literals end up sharing the same memory address of those pre-allocated String objects, even when stored in different variables.

    String a = "I'm pre-allocated by Java!";
    String b = "I'm pre-allocated" + " by Java!";
    println(a == b, a.equals(b));  // true true
    
    String c = new String(a);
    println(b == c, b.equals(c));  // false true
    
    c = c.intern();
    println(b == c, b.equals(c));  // true true
    
    println(P3D == OPENGL, P3D.equals(OPENGL)); // true true
    exit();
    
  • So an array is an object and that's why System.copyarray(fromArray, fromStartPoint, toArray, toStartPoint); is used to copy a simple array?

  • edited May 2016

    So an array is an object...

    Any datatype apart the 8 primitive 1s is an object/reference type:
    http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    ... is used to copy a simple array?

    arraycopy() replaces the content of the 2nd array w/ the content of the 1st:

    1. https://Processing.org/reference/arrayCopy_.html
    2. http://docs.Oracle.com/javase/8/docs/api/java/lang/System.html#arraycopy-java.lang.Object-int-java.lang.Object-int-int-

    It can even copy some portion of 1 array to another position within the same array too. ;;)

  • Why is array1 = array2 a reference, while int1 = int2 a copy?.
    Why don't references have special commands in Java?

  • edited May 2016

    @AverageJoe, as I've said before, variables hold 1 value only in a given time.
    In Java, that value can be split as 1 of these 3 big groups:

    1. Fixed-point primitive numerals: boolean, byte, short, char, int & long.
    2. IEEE 754 Floating-point primitive numerals: float, double.
    3. Object reference memory addresses: All of the rest. >-)
  • edited May 2016

    Regardless which group some variable belongs to, when we pass it as an argument to some function, the actual value (no matter which datatype) held by that variable is read; then it is sent to the corresponding parameter of that function. That's called pass-by-value. :-B

  • The object reference variable may or may not be an actual memory address it depends on the JVM implementation. The point is that the object reference is an abstraction that hides the representation / implementation details from us. Also the memory address used to store an object can be changed by the JVM without warning, especially by the garbage collector.

    In a pure OO language there would be no primitive data types and all variables would be objects removing the primitive/object dichotomy.

  • edited May 2016

    The object reference variable may or may not be an actual memory address it depends on the JVM implementation.

    In any Virtual Machine, hardly some reference would be the actual computer's physical memory address. That's why I've mentioned it sometimes as "Logical" memory address in this forum thread. :-B

    Regardless, it's still some value which represents a memory address codified as 4 bytes (32 bits). Or other times, when the Java app is too big, as 8 bytes (64 bits).

    And that's the value which is passed on when invoking any function w/ parameters.
    It's Java's famous everything pass-by-value. :\">

    As I'm trying to tell every1 here since the beginning, it doesn't matter whether any variable is holding some fixed-point, IEEE 754 floating-point, or even a reference value: It's merely a value. :-j

  • edited May 2016

    Also the memory address used to store an object can be changed by the JVM without warning, ...

    Perhaps the actual physical memory address happens to be moved around some times.
    However, the logical memory address value must be kept rigorously the same while the object lives.
    Otherwise, the value held by reference variables would suddenly stop working! >:)

  • Perhaps it would be clearer if I said

    Also the memory location used to store an object can be changed by the JVM without warning, ...

  • edited May 2016

    In a pure OO language there would be no primitive data types and all variables would be objects removing the primitive/object dichotomy.

    Python is such a language. But it pays a big price for not having primitive types as Java or C. b-(

    However, there are some other big programming languages which can transparently turn any wrapper object holding a numerical value into its corresponding primitive type! :-bd
    Logically we don't control when/whether such internal conversion takes place. It's up to the VM. 8-|

    As an example I'd cite C#, which is a super-set copycat of Java.
    I dunno much about it, but I've heard that everything is an object there.
    And its .net VM is smart enough to optimize number objects as primitive 1s when it's possible.

    I believe that our own browser's modern JavaScript VMs can do the same for its datatype Number.
    Hardly any1 invokes methods or access other properties upon numerical variables.
    Therefore JS can turn them into actual primitives internally! Even fixed-point values!!! \m/

  • For how values are represented in memory for programming languages w/o raw primitive type storage, that is everything is stored as memory addresses; plus optimizations like temporarily keeping the raw value in CPU registers, take a look at these 2 links below: O:-)

    1. https://JavaScriptWeblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/
    2. https://www.DartLang.org/articles/numeric-computation/
  • Thanks for sharing the links, very interesting :)

    C# and the other .NET languages share a Common Type Specification (CTS) and a Common Language Runtime (CLR) which compiles the source code to IL (Intermediate Language). When the program is executed the CLR uses a JIT compiler to convert the IL into into native machine code. Very different approach to the JVM.

    When designing C# and the .NET framework Microsoft looked at all the common programming languages identifying the strong aspects to include and the weak aspects to avoid, so it is not a copy of Java or any single language in particular.

    In fact MS created the J# language as a bridge for Java developers wanting to move over to .NET. J# was able to use existing Java byte code.

  • edited May 2016

    ... which compiles the source code to IL (Intermediate Language).

    Java's ".class" files are bytecode. Which "coincidentally" is an intermediate language too. Surprised? ;))

    When the program is executed the CLR uses a JIT compiler to convert the IL into into native machine code.

    JiT = Just-in-Time compilation. Even JavaScript got 1, even more Java! :>
    What technique do you think Java's VM does to optimize "hot" sections of bytecode after all?

    Very different approach to the JVM.
    ... so it is not a copy of Java or any single language in particular.

    I really fail to grasp why you'd think so! Practically C# got all Java's syntaxes & features.
    Only the opposite is false to some unique C# features not present in Java. :-@
    In my understanding, that's the very definition of a super-set language! ~O)

    https://en.Wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java

Sign In or Register to comment.