views:

434

answers:

7

When I started OO programming many years ago I gained the impression that variables (if that is the right word) were either "primitives" (int, double, etc.) or first-class objects (String, JPane, etc.). This is reinforced by a recent answer on primitives in Java and C# (@Daniel Pryden: http://stackoverflow.com/questions/1597999/are-primitives-different-in-java-and-c). However don't know whether C# ValueTypes are primitives, objects or some other beast such as second-class objects. I see that SO has only one use of the first-class tag so maybe it is no longer a useful term.

I did not find the Wikipedia article useful (http://en.wikipedia.org/wiki/First-class%5Fobject) ("This article is in need of attention from an expert on the subject."). I'd be grateful for a taxonomy and current usage of terms, primarily related to Java and C# (though maybe other languages will shed enlightenment).

EDIT clarification. I'd like to understand the term "first-class" and what its range of use is.

SUMMARY After 5 answers (30 mins) the picture seems to be that there is no consensus on "first-class" (except that it's not very useful here) and that the distinction between primitives and objects in C# is blurred (primitives can have methods). So now I don't know what a "primitive" is either! But I am getting a clearer idea of exactly how int C# and int Java differ

+1  A: 

For each primitive data type in Java, the core class library provides a wrapper class that represents it as a Java object. For example, the Int32 class wraps the int data type, and the Double class wraps the double data type.

On the other hand, all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided. For instance, int is the short name for System.Int32 and double is the short form of System.Double.

The list of C# data types and their aliases is provided in the following table. As you can see, the first eight of these correspond to the primitive types available in Java. Note, however, that Java's boolean is called bool in C#.

From : http://msdn.microsoft.com/en-us/library/ms228360%28VS.80,lightweight%29.aspx

Braveyard
Note that you say "primitive data types in C# are objects" - assuming this is a correct usage it blurs the distinction between primitive and object,
peter.murray.rust
Well, I am not saying it, this is what written on MSDN, please follow the link I provided above.
Braveyard
A: 

http://onjava.com/onjava/2003/05/21/delegates.html

in other words c# methods are first class object because we can pass it in another method. we can use methods like any other values(strings, numbers, user-created object).

Another example of first class objects that u can find uncommon in other languages but c# is Expressions

Trickster
Methods in C# are *not* first-class objects. They can, however, be *lifted* to objects through the use of delegates or explicit wrappers.
pst
(Python and Javascript are languages which have methods as first-class citizens).
pst
methods not first-class in clr. but in c# they first-class because of syntax. no matter what will be done by compiler or runtime, we talking here about languages. imho :-)
Trickster
+4  A: 

In .NET you don't have primitive types vs classes. Instead, you have structs vs classes, but structs share many of the features of classes (such as the ability to have properties and methods), and inherit from the Object class as well.

When you write int in C#, for example, it is just a language shortcut for the Int32 struct. You can do for example int i=int.Parse("34"), or even string s=1234.ToString(). In order to assign struct instances to variables of type Object, there is the boxing/unboxing mechanism.

In Java, on the other hand, you have indeed the primitive types vs classes dicotomy. So for example to perform operations on a variable of type int, you must use the auxiliary Integer class. That's one of the things that I don't like of Java compared to .NET.

EDIT. When you read about "first-class objects" (or classes), it means "fully-powered objects", that is, classes that have the same capabilities as any other system classes or user-made classes. This is to distinguish from "limited primitive types".

Konamiman
I wonder why this got voted up, `int` is not `Int32`, one is a value type the other is a class.
Blindy
@Blindy, actually both of them are value types. int is quick representation of `Int32`. That's all
Braveyard
P.s : for more information : http://tinyurl.com/yguowkt, both `int` and `Int32` are value types and stored in stack.
Braveyard
+3  A: 

The problem is that "first class object" is not a well defined concept.

The normal usage is that someone says that an "object" is a class of thing that should have all of the properties X, Y and Z. But there are other things that don't have all of those properties, but they are sort of object-ish. So we'll call the former "first class" objects and the rest not "first class" ... and may be not objects.

The problem is that there are any number of views on the properties that a thing needs to have to make it a "first class" object. And no prospect of the people with opposing view coming to a consensus. (For example, a Javascript language expert might argue strenuously that an object is only first class if it is template-based.)

The only really solid insights about "first-classness" will be those that you can glean from the respective language specifications for Java and C#. And they only really apply within the scope of the respective languages / type systems ... and not across multiple languages.

So "first class Java object" or "first class C# object" might be meaningful, but "first class object" taken out of context is not.

Well that's my opinion ...

Stephen C
Thanks - this is the sort of answer I was looking for.
peter.murray.rust
+1  A: 

Frankly, I have no idea of what a "first-class object" is...
But I first found usage of a similar idiom in Lua documentation and mailing list, saying that functions are first-class citizens, or first-class values.

I let one of the authors of Lua to explain what it is: Programming in Lua : 6 - More about Functions

It means that, in Lua, a function is a value with the same rights as conventional values like numbers and strings. Functions can be stored in variables (both global and local) and in tables, can be passed as arguments, and can be returned by other functions.

Somehow, this definition applies to objects in Java: you can store them in variables, in arrays, use them as function parameters and return them, use them as key of HashMap and other collections, etc.
Not sure if that's how the term is used for objects, but at least it makes sense... :-)

In a language like C, objects have to be made from scratch, using some tricks (re-creating C++, somehow...), so they are not first-class: you have to pass pointers around to manipulate them.

PhiLho
Right, functions are a good example: First-Class means here "you can store them in a List". You can't store ints in a Java list (at least you couldn't in earlier versions, because an int is not an object - you can now, but there is some magic involved).
Daren Thomas
+1  A: 

When we're talking about "first-class objects" by "objects" we mean some concepts of the language, not the objects that we create in that language. That is why there is also such terms like "first-class citizens".

So, for example, Java has following concepts - Java-objects, Java-primitives, fields, methods and other (by Java-objects I mean anything that is instance of Object type). I'd say that in Java both Java-objects and Java-primitives are first-class citizens in the language.

In C# we have some additional concepts that we can "test" for first-class properties. For example, delegates. We can assign delegate ot variable (give a name), pass it to the method as an argument, return it from method, incorporate in data structures (have a Dictionary of delegates for example). So I think we can say that delegates are first-class objects in C#. You can continue for other concepts of C# - events, properties...

Functional languages have concept of "function" and of course it is a first-class citizen in the any functional language. I'd say that we can call language a functional language if it has "function" as a first-class concept (name, pass, return, incorporate...).

So, if some language bring some concepts we can "measure" the power of this concepts in the language it self.

alsor.net
Actually, I didn't take into account the property of being able to create object at run-time. So from this perspective dalagates are not first-class objects in C#.
alsor.net
+3  A: 

The notion of "first-class citizen" or "first-class element" in a programming language was introduced by British computer scientist Christopher Strachey in the 1960s in the context of first-class functions. The most famous formulation of this principle is probably in Structure and Interpretation of Computer Programs by Gerald Jay Sussman and Harry Abelson:

  • They may be named by variables.
  • They may be passed as arguments to procedures.
  • They may be returned as the results of procedures.
  • They may be included in data structures.

Basically, it means that you can do with this programming language element everything that you can do with all other elements in the programming language.

Jörg W Mittag