What Is and Examples of Using Data Type - References

What is and examples of using data type - References

I'm taking a guess that you're referring to t.references :associated_model in a migration?

Suppose two models, Post and Author.

class Post < ActiveRecord::Base
belongs_to :author
end

class Author < ActiveRecord::Base
has_many :posts
end

Your migration contains:

create_table :posts do |t|
t.references :author
end

This will create the author_id column on the posts table with the integer datatype.

In migrations, t.belongs_to is an alias for t.references and matches the naming used to set up the associations in your models.

Why do reference data types point?

How big is that orange rectangle going to be?

For primitives, you already know the size already. But what about for objects? For concrete final classes, you'd know how much memory is going to be used already... but what about other cases?

For example:

InputStream x = new FileInputStream("foo");

If the variable x has to contain all the fields of the object (and know what type it is) the it's got to be big enough for all the FileInputStream fields. Okay, in this case we can manage that, although it's slightly odd for the size of an InputStream variable to be defined by its usage. What about this:

InputStream x = getInputStream("foo");

The compiler can't know what type of object would be returned by getInputStream - so how can it know how big x is going to be? When the value of x is a reference, it's much simpler - it's the same size regardless of the actual type of the object it refers to.

Then of course you want to be able to share objects between various "users" of that object, which is where using references becomes very efficient. (We don't have to copy all the fields every time we pass a value around - we just copy the reference.) Treating objects as values instead of references changes the semantics a lot. For example:

// A simple mutable type with get/setValue doing the obvious thing
MutableType x = new MutableType();
x.setValue(5);
MutableType y = x;
x.setValue(10);
System.out.println(y.getValue());

With references, this prints 10 because x and y are just references to the same object. With value type semantics, presumably it would print 5 instead. Not unreasonable - but a very big change in how the language works.

Then there are other issues besides, but I think that's enough to start with :)

Of course, C++ has answers to all of these things, but the designers of Java deemed it simpler to just use references for all non-primitive types.

C++ pointers and references data type

No, these are still real datatypes.

Pointers point to an address space in the memory. For example,

int test = 5;
int *foo = &test;

& will get the address that test is located at. *foo just states that foo will be storing the address.

With this in mind, foo will not = 5. But rather some hex address such as 0xA000125BFFFFFF or something weird like that.

If this helps, some people like to write:

int* foo

So you can think of this as a int pointer called foo.

But, one thing to point out is that all of these are type int. This will apply to another datatype such as float or double or some other custom one.

However, one thing to note is that you cannot do this:

double test = 5.02;
int *foo = &test;

What's the difference between primitive and reference types?

These are the primitive types in Java:

  • boolean
  • byte
  • short
  • char
  • int
  • long
  • float
  • double

All the other types are reference types: they reference objects.

This is the first part of the Java tutorial about the basics of the language.

Primitive and Reference data type in javascript

Assignment makes a copy of the value only if it's a primitive type (like Number, Boolean, etc...). Otherwise, assignment just copies a reference to the same object (Object, Array, etc...). A new object is not created with assignment.

Reference

An assignment does not create a copy/clone/duplicate of an object.

What is Firebase Firestore 'Reference' data type good for?

References are very much like foreign keys.

The currently released SDKs cannot store references to other projects. Within a project, references can point to any other document in any other collection.

You can use references in queries like any other value: for filtering, ordering, and for paging (startAt/startAfter).

Unlike foreign keys in a SQL database, references are not useful for performing joins in a single query. You can use them for dependent lookups (which seem join like), but be careful because each hop will result in another round trip to the server.

What is definition of reference type?

A reference is an alias, an alternate name for an object. It is not an object itself (and in that way is not a pointer, even if some of their uses overlap with uses of pointers).

References have certain limitations to their handling, related to their non-objectness. For example, you can't create an array of references. They have to be initialized (bound, seated) as soon as they are declared, since they can't possibly exist without an object to alias.

You can however store them, and they obey the rules of automatic variables or member variables. One of their uses is to poke through C++'s pass-by-value function calls.

Note that const references have a neat side-effect of being aliases : when bound to a temporary (i.e unnamed) object, they give said object a name, and therefore extend its lifetime to that of the reference itself.

{ // Block scope
Foo fooVal = makeFoo(); // Say makeFoo() returns a (temporary, unnamed) Foo
// Here the temporary Foo is dead (fooVal is a copy).

// Foo &fooRef = makeFoo(); // Error, reference is non-const
Foo const &fooCRef = makeFoo(); // All good

// ...

// The second temporary is still alive
fooCRef.doSomethingFunny(); // Works like a charm !

} // The second temporary dies with fooRef

Beware though, it is possible (though contrived) to have an object go out of scope with references still pointing to it. You will then have dangling references, which are not to be used anymore (doing so would be Undefined Behaviour).

Foo *fooPtr = new Foo; // Here is a Foo
Foo &fooRef = *fooPtr; // Here is an alias for that Foo

delete fooPtr; // Here is the end of that Foo's life

fooRef.doSomethingFunny(); // Here comes trouble...

Data Type different from reference table

The problem here was due to referencing.
The table Train_ticket_fare must have TICKET_CLASS as primary key which is to be referenced to the table Ticket. I did the opposite and referenced it wrong.
That is why it was showing wrong data-type.
Thanks @stickybit
Sorry if I posted wrong question. I am new to platform.



Related Topics



Leave a reply



Submit