primitives and classes

edited February 2014 in Programming Questions

If a primitive type has its own class within processing itself (or java) is it not an object or float of type object and if so what differentiates it from being used like other objects? ie being able to hold multiple streams of data ect would its behaviour be controlled by the variables contained in the constructor of class float and could i initialize a primitive in the same way i would any other object eg:

float wolf ;                           
void  setup(){
wolf = new float(2);
}

and would this be the same as saying: float wolf = 2;

Answers

  • edited February 2014
    • Variables are merely labeled names for physical memory addresses.
    • In Java, variables can be either primitive or reference.
    • There are 8 primitive types and everything else is of reference type.
    • Those 8 primitive types are also keywords.
    • There are 8 wrapper classes for each of those 8 primitive types too.
    • A primitive variable interprets its content as a numerical or boolean value.
    • A reference variable interprets its stored value as a memory reference to an object.
      Which in turn is a dynamically allocated contiguous block of memory.
  • edited February 2014
    float primitiveFloat = 10.5;
    Float referenceFloat = -30.8;
    

    Main difference between those 2 values above is on how they are represented in memory.

    The 10.5 value is stored as a 4-byte memory sequence.
    The -30.8 is inside a Float object placed in some contiguous memory block in the heap region.

    What's actually stored in variable referenceFloat is the memory address of that Float object!

    Hope that's enough! O:-)

  • wow that's great would the array function "float[] name= new float[number]" be a reference or a primitive then? i guess the invoking the new operator is confusing me as i assume this is used to initialize only "objects" the type float is primitive and so i could see this being stored as a list of 4 byte sequences ?

  • edited February 2014 Answer ✓
    float wolf ;
    void setup(){ wolf = new float(2); }
    

    Will not work because float is a primitive and new is used to create objects from classes. But this will work.

    Float wolf ;
    void setup(){ wolf = new Float(2); }
    

    Note that the wrapper classes Float, Integer, Double etc are immutable so in the following

    Float wolf ;
    void setup(){ 
       wolf = 2.0; 
       wolf = 3.0;
    }
    

    will create two Float objects one at line 3 and a second at line 4

  • edited February 2014 Answer ✓
    final int number = 20;
    float[] name = new float[number]
    

    Arrays are objects too, although they have special treatment in Java b/c they are instantiated & accessed thru' curly braces [].
    Therefore, what's stored in variable name is the memory address of that dynamically allocated memory block thru' new.
    So, where are the float primitive elements? Inside that array object's memory block! 8-X

  • thanks that clears things up alot of "new" things were brought up ;) but that definitely sorted out my query

    Cheers

Sign In or Register to comment.