Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Collections in Java

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good evening everyone,

I have two questions for you.

I studied that in Java there are 5 reference types: Classes, Interfaces, Enums, Arrays and Annotations.

The questions are:

1)why Collections are NOT reference types?
2) Is it correct to say that a Collection is an object which represents a group of objects of the same type?
 
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is kind of like asking this:

I've learned that there are five different kinds of vehicles: Cars, Trucks, Airplanes, Boats, and Rockets
Why isn't Transportation included?

I assume you're talking about the Java Collections API. The Collections API contains a number of classes and interfaces. Examples of classes in the Collections API are ArrayList, HashMap, and HashSet. Examples of interfaces included in the Collections API are List, Map, and Set. All these are reference types.
 
Marshal
Posts: 68899
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claud Mann wrote:Good evening . . .

Good afternoon. You must be in a different time-zone from me.

there are 5 reference types: . . .

Who told you that? The Java® Language Specification (=JLS) recognises four kinds of reference. Annotated types are special kinds of interface and enums are special kinds of class.

1)why Collections are NOT reference types? . . .

What gives you that idea? I suspect you have confused the existence of the Collections Framework with the concept of reference types. The Collections Framework contains several interfaces (reference types) and classes (reference types). Most of its types are parametrised with formal type parameters which (I think) can be used as type variables (reference types) and there aren't any arrays directly visible. Some of the classes, however, use arrays as implementation details.
An implementation of any of the interfaces in the Collections Framework is a class, and that is a reference type. As you say, it collects other types of object together. Not primitives, but they can be put into a collection by boxing conversion.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claud Mann wrote:2) Is it correct to say that a Collection is an object which represents a group of objects of the same type?


You seem to be mixing up different concepts.

Yes, a collection can be a group of objects of the same type. It can also be a group of objects that have different types (like a List<Object>). A collection is essentially an aggregation of objects that have something in common, whether it's attribute or behavior.

I think what you're confused by is that a class can also be defined as something that represents a group of objects with the same characteristics and behaviors.

Seeing a class as one type of reference type is one way of looking at it. On the other hand, a class can also be a collection but being a collection is a different way of looking at it. Reference types and Collections are two different abstractions, they're not mutually exclusive of each other.

It's kind of like seeing humans with a biology lens (humans belong to the animal kingdom and are mammals) vs. with a chemistry lens (humans are carbon-based and are up to 60% H2O). Both these ways of looking at humans are correct. Your question, however, is like saying "I thought humans belonged to the animal kingdom so how can they also be carbon-based?"
 
Claud Mann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
It's kind of like seeing humans with a biology lens (humans belong to the animal kingdom and are mammals) vs. with a chemistry lens (humans are carbon-based and are up to 60% H2O). Both these ways of looking at humans are correct. Your question, however, is like saying "I thought humans belonged to the animal kingdom so how can they also be carbon-based?"



Thanks to Campbell and Junilu!
So, let me know if this sentence is correct:
a collection is a particular kind of class which is able to "collect" some objects.
Is it right?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet). Any interface and classes you have to do object of their classes  then collection are "collect" some objects

 
Claud Mann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again to everyone.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic