Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

User defined First class objects  RSS feed

 
megha ghanekar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
String and arrays are first class onjects. As they may be created without using new operator. Is there any way using which i can create my own first class objects.
e.g.
if i have my class Demo as follows . I should ne able to say.
Demo d = "meghana";
where class Demo is as follows
class Demo
{
private String buff;

public Demo()
{
buff = null;
System.out.println("default constructor of Demo");
}
public Demo(String s)
{
buff = s;
System.out.println("parametrised constructor of Demo");
}
}

Warm Regards,
meghana.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked up the term "first-class object" to make sure what you mean.

No, you can't write your own class in Java with which you can do something like:

Some programming languages support operator overloading, which means you can write your own implementation of the operator "=" for specific operand types - but Java does not have operator overloading.
 
megha ghanekar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Jesper,
Can u please tell me the reason why it is not allowed. Is it that first class objects are stored differntly than the normal objects. Or is there any other reason, like synatx itself is a problem?

Warm Regards,
meghana.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked up the term "first-class object" to make sure what you mean.
I looked in the same place, in the link you quoted, and there were so many different definitions for "first-class object" that I think "first-class object" could mean anything. Sounds a bad term to me.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
I looked in the same place, in the link you quoted, and there were so many different definitions for "first-class object" that I think "first-class object" could mean anything. Sounds a bad term to me.


The original poster will have to provide us with his definition of "first-class object", as well as his defence of why this is an important concept and why he thinks Java should support it. We're waiting...
 
megha ghanekar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!!
I come from C++ background. I conduct trainings on c++, wherein there are lots of memory issues.But still u know how exactly each & everything works there. I m new to java.I have recently started trainings with java.This was the doubt asked by 1 of my students. He wanted to know whether we can create our own first class objects or not. Java hides everything & I wanted to know the ezact reason why there are no userdefined first class objects.
Correct me if i m wrong. It is said that Arrays & Strings are first class objects u dont need to use new operator with them.
I know this much.And i m curious @ it.Want to know more.

Warm Regards,
meghana
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is said that Arrays & Strings are first class objects u dont need to use new operator with them.


In java?

I don't know who told you this. If you do not use the new operator then there better be an array or a string object already created (new) and available so all you have to do is "declare" a string or an array and assign it to it, otherwise how are you going to create a string or an array? You are correct when you say that you DONT HAVE TO, but take these examples:

When you say int[] arr = {1,2,3,4,5}; you just created (new) and array with memory space with a reference to it.

Also if you say:
int[] v1; <-- this declares it. (is is pointing to anything???)
v1 = new int[3]; <-- we just did.

Same thing with Strings

String a;
a = "foo";
b = "test";
a = b;

This doesn't compile because b is not declared as a String.

HTH
 
megha ghanekar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Gabriel,
Can u plz tell me the exact meaning of first class objects & why they r not supported by java?

Warm Regards,
meghana
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the first things that Wikipedia mentions about first-class objects is:

* being expressible as an anonymous literal value

Which is what Megha seems to be asking about in his post. In C++ and other programming languages you can do something like

by overloading the "=" operator; you write your own implementation of the "=" operator that operates on a Demo and a String.

The designers of the Java language at Sun deliberately left operator overloading out of Java, because it's one of those things in C++ which can make programs quite messy and unreadable if programmers don't use it wisely.

Megha, what you call first-class objects (which is not a term that is very commonly used in programming) are not stored differently in Java. Even though you don't see the "new" operator in your code, it's still executed behind the scenes. The Java programming language just contains some special syntax constructs to make your source code shorter and more clear. The syntax is not a "problem", it was invented to make life as a programmer easier for you...
[ June 14, 2006: Message edited by: Jesper Young ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by megha ghanekar:
Can u [sic] plz [sic] tell me the exact meaning of first class objects & why they r [sic] not supported by java?


Wait a minute, isn't it you who should be defining "first class object" for us? It's not a term used in Java, in fact I just got Adobe Acrobat to scan the JLS (Java Language Specification) and the phrase "first class object" does not occur once in it. What does that tell you? Imagine going into a music store and looking at the pianos, then asking the salesman, "how much horsepower has this one got?"
 
megha ghanekar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!!
Sometimes new learners have some wrong concepts in there mind. That is why, they want to get it cleared. Many a times it happens that we hear some things from somewhere. we try to find solutions to it, we may mis interprete it. I thought rhis is a good place to get my doubts solved, with so many people to help.
I had heard these words "Arrays & Strings are first-class objects just bcoz there is no need to use new operator with it." from my seniors. This confused me a lot. I didnt have exact idea about it & i really wanted to know. So i went on asking the same thing. Sorry @ it.
If you go to the electronics store & you ask about a television set. and you may not understand each & every feature of that television set by name, then u ask the salesperson. Now if the salesperson asks you to first read the manual thoroughly and come. then u are helpless.
I will first try to search on my own & come back.
Warm Regards,
megha.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:


Wait a minute, isn't it you who should be defining "first class object" for us? It's not a term used in Java, in fact I just got Adobe Acrobat to scan the JLS (Java Language Specification) and the phrase "first class object" does not occur once in it. What does that tell you? Imagine going into a music store and looking at the pianos, then asking the salesman, "how much horsepower has this one got?"


It is not used in Java literature because if it were, it would inadvertantly highlight some of the defects of the language which violates the objectives of the author(s) of Java literature. The term "first class object" has been around for a long time and is used quite heavily in texts related to programming language theory that attempt to solve a problem in such a way as to not be biased by ulterior motives such as marketing pressures, etc. i.e. attaining some level of correctness is a true first class objective .

One need not look far to find a functional language which is defined by having "first class functions" (no, C is not a functional language despite what the teacher may have said).

The JLS is not authoritative.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheer up, Tony . You never seem to have a good word to say about Java™. Isn't there some Haskell forum you'd rather be haunting?
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:


It is not used in Java literature because if it were, it would inadvertantly highlight some of the defects of the language which violates the objectives of the author(s) of Java literature. The term "first class object" has been around for a long time and is used quite heavily in texts related to programming language theory that attempt to solve a problem in such a way as to not be biased by ulterior motives such as marketing pressures, etc. i.e. attaining some level of correctness is a true first class objective .

One need not look far to find a functional language which is defined by having "first class functions" (no, C is not a functional language despite what the teacher may have said).

The JLS is not authoritative.


So why not define this term for the rest of us?

Yuriy
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Jeff Albertson. But Tony, you have told us about "first class functions" and "first class objectives," and haven't told us what "first-class object" is.

There was a thread here about two weeks ago, which I can't seem to find now, but it had yet another definition of a first=class object, one whose fields would not change (I think). So I still think people say "first class object" to mean so many different things that the term is of little use without further explanation.

CR
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!