First class objects in java

What are first-class objects in Java and C#?

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: Are primitive types 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 («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).

Clarification: I’d like to understand the term first-class and what its range of use is.

peter.murray.rust

People also ask

first-class object (plural first-class objects) (programming, languages) An entity that can be constructed at run-time, passed as a parameter, returned from a function, or assigned into a variable.

A first-class object is an entity within a programming language that can: Appear in an expression. Be assigned to a variable. Be used as an argument. Be returned by a function call.

For example, in the C programming language, you cannot pass a function to another function as a parameter. So in C, functions are referred to as «second-class objects.» However, in JavaScript a function can be passed to another function as a parameter, therefore in JavaScript, functions are first-class.

In C++, classes are not first class objects but instances of those classes are. In Python both the classes and the objects are first class objects. (See this answer for more details about classes as objects).

5 Answers

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

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 views 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.

Stephen C

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 Avatar

answered Oct 24 ’22 02:10

Konamiman

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#.

Tarik Avatar

answered Oct 24 ’22 02:10

Tarik

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

Источник

First Class Objects in java [duplicate]

So can anyone please define a first class object in simple manner & tell me all the First Class objects in java EDIT:Before marking as possible duplicate please read the question carefully Let me put it in short My question is 1.Are arrays and Exceptions first class objects ? and 2.Can you define First class objects (interview type) ? Question: Now I heard that First class objects in java are objects created without using the new keyword like String

Читайте также:  Load sql file java

First Class Objects in java [duplicate]

Possible Duplicate:
What are first- class objects in Java and C#?

Now I heard that First class object s in java are objects created without using the new keyword like String

Now we declare Arrays also without without new keyword ? so are they first class So i got this article http://c2.com/cgi/wiki?JavaArraysShouldBeFirstClassObjects

Now another interesting article i found is http://c2.com/cgi/wiki?JavaExceptionsAreFirstClassObjects so are exceptions first Class objects ??

So can anyone please define a first class object in simple manner & tell me all the First Class objects in java

Before marking as possible duplicate please read the question carefully Let me put it in short My question is

1.Are arrays and Exceptions first class objects ? and 2.Can you define First class objects (interview type) ?

Doesn’t the link at that same site just about cover it? Keep in mind that a «first-class object» is not something defined by any kind of specification and it means different things in different contexts. The c2.com site defines its own meaning, but those other examples you mention use it in a different sense. It’s confusing you and rightly so because you are made to compare apples and oranges.

What are first-class objects in Java and C#?, 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». Share. Improve this answer. edited Oct 21, 2009 at 7:24.

Treating classes as first-class objects

I was reading the GoF book and in the beginning of the prototype section I read this:

This benefit applies primarily to languages like C++ that don’t treat classes as first class objects.

I’ve never used C++ but I do have a pretty good understanding of OO programming, yet, this doesn’t really make any sense to me. Can anyone out there elaborate on this (I have used\use: C, Python, Java, SQL if that helps.)

For a class to be a first class object, the language needs to support doing things like allowing functions to take classes (not instances) as parameters, be able to hold classes in containers, and be able to return classes from functions.

For an example of a language with first class classes , consider Java. Any object is an instance of its class. That class is itself an instance of java.lang.Class.

For everybody else, heres the full quote:

«Reduced subclassing. factory method (107) often produces a hierarchy of Creator classes that parallels the product class hierarchy. The Prototype pattern lets you clone a prototype instead of asking a factory method to make a new object. Hence you don’t need a Creator class hierarchy at all. This benefit applies primarily to languages like C++ that don’t treat classes as first-class objects. Languages that do, like Smalltalk and Objective C, derive less benefit, since you can always use a class object as a creator. Class objects already act like prototypes in these languages.» — GoF, page 120.

I found it subtle in so much as one might have understood it as implying that /instances/ of classes are not treated a first class objects in C++. If the same words used by GoF appeared in a less formal setting, they may well have intended /instances/ rather than classes. The distinction may not seem subtle to /you/. /I/, however, did have to give it some thought.

I do believe the distinction is important. If I’m not mistaken, there is no requirement than a compiled C++ program preserve any artifact by which the class from which an object is created could be reconstructed. IOW, to use java terminology , there is no / Class/ object .

In Java, every class is an object in and of itself, derived from java.lang.Class, that lets you access information about that class, its methods etc. from within the program. C++ isn’t like that; classes (as opposed to objects thereof) aren’t really accessible at runtime. There’s a facility called RTTI (Run- time Type Information) that lets you do some things along those lines, but it’s pretty limited and I believe has performance costs.

Читайте также:  Настройка apache php mysql centos

You’ve used python, which is a language with first-class classes. You can pass a class to a function, store it in a list, etc. In the example below, the function new_instance() returns a new instance of the class it is passed.

class Klass1: pass class Klass2: pass def new_instance(k): return k() instance_k1 = new_instance(Klass1) instance_k2 = new_instance(Klass2) print type(instance_k1), instance_k1.__class__ print type(instance_k2), instance_k2.__class__ 

Python — What are «first-class» objects?, A first class object is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have. Depending on the language, this can imply: being expressible as an anonymous literal value being storable in … Code samplefunction makeDerivative( f, deltaX ) return deriv;Feedback

C++ «Object» class

In Java, there is a generic class called «Object «, in which all classes are a subclass of. I am trying to make a linked list library (for a school project), and I have managed it to make it work for only one type, but not multiple, so is there anything similar to that?

EDIT: I would post the code, but I don’t have it on me at this time.

There’s no generic base class in C++, no.

You can implement your own and derive your classes from it, but you have to keep collections of pointers (or smart pointers) to take advantage of polymorphism.

EDIT: After re-analyzing your question, I have to point out std::list .

If you want a list which you can specialize on multiple types , you use templates (and std::list is a template):

If you want a list which can hold different types in a single instance, you take the base class approach:

class Object < protected: void * Value; public: template void operator = (Type Value)< this->Value = (void*)Value; > template <> void operator = (string Value)< this->Value = (void*)Value.c_str(); > template bool operator == (Type Value2)< return (int)(void*)Value2==(int)(void*)this->Value; > template<> bool operator == (Object Value2)< return Value2.Value==this->Value; > template ReturnType Get()< return (ReturnType)this->Value; > template <> string Get()< string str = (const char*)this->Value; return str; > template <> void* Get()< return this->Value; > void Print()< cout Value >; 

Then make a subclass of it

Arrays — First Class Objects in java, Keep in mind that a «first-class object» is not something defined by any kind of specification and it means different things in different contexts. The c2.com site defines its own meaning, but those other examples you mention use it in a different sense.

Источник

Оцените статью