Is Arraylist is typesafe or strongly typed?
Asked Answered
T

4

5

I don't know what exactly the difference between "Strongly typed" and "Type safety" is!

Could you please clarify this in a simple language?

Suppose we are using Arraylist, but I am unable to conclude it is typesafe or strongly typed. or can we use it as both.?

Thiamine answered 1/8, 2013 at 2:55 Comment(1)
Note that "strongly typed" normally applies to a programming language, while "type safe" is likely coming from "type safe collection" phrase...Cyprio
L
8

An ArrayList is not typesafe. What this means is that ArrayList can be assigned a value of any type:

ArrayList myList = new ArrayList();
myList.Add("this is a string");
myList.Add(19); //Notice that this is an int, but it doesn't throw an error!

This is an issue because when you go to use the list, you don't know the types that are in the list. The chances of having an error thrown are very high.

Avoid using ArrayLists! Use a generic list instead, such as List<T>

Laughing answered 1/8, 2013 at 2:59 Comment(1)
Can't you make ArrayList typesafe via generics?: ArrayList<String> myList = new ArrayList<String>(); Or are you saying to prefer this over that?: List<String> myList = new ArrayList<String>();Cchaddie
H
7

Type-safe and strongly-typed are loaded phrases, such that it's hard to get programmers everywhere to pin down a precise definition. But there is at least enough consensus to state that ArrayLists are neither in every way that matters.

Do not use ArrayLists! Instead, use the generic List<T> collection.

Handclasp answered 1/8, 2013 at 2:57 Comment(6)
do you mean arraylist is Neither TypeSafe Nor Strongly TypedThiamine
I mean that it does not enforce either for items stored within the collection.Handclasp
but in short conclusion is same. as Neither TypeSafe Nor Strongly TypedThiamine
I would say that the ArrayList class itself is strongly-typed: an ArrayList is a very specific type within the strongly-typed .Net Type system. The number and types of properties and methods don't mutate at run time, and checking with reflection will always return System.Collections.ArrayList. But in context, that doesn't matter nearly as much as the data contained in the collection, and the type-safety or strength of that data is not at all enforced.Handclasp
well... Thanks... what would be your answer out of following :A. TypeSafe B. Strongly Typed C. TypeSafe and Strongly Typed D. Neither TypeSafe Nor Strongly TypedThiamine
If this was homework, it sounds like a trick question and I'd ask the instructor for clarification. It sounds like either D or B could answer the question, depending on who is asking it.Handclasp
S
0

The ArrayList data structure is a not type-safe, nor strongly-typed. You cannot guarantee what type of object is in an ArrayList, thus everything is stored as an Object, similar to how objects are stored in Session cache for ASP.NET.

The biggest downside to using ArrayList was the casting that needed to happen whenever you wanted to get access to the objects in the actual list. If any of the values in the list were not of the type you were expecting, then you would potentially get casting exceptions and the code to deal with that was just ugly.

Use generics wherever possible, with List<T> being the closest generic type to the ArrayList data structure.

Strongbox answered 1/8, 2013 at 3:14 Comment(0)
T
0

I'm not sure I understand your question.

"Strong/weak typed" is a quality of programming languages. Java is strongly typed, which means it has a strict syntax, and it can find problems in your code at compile time, before errors make it to run time.

Java may not exactly be type safe, though (I'm not sure of the exact answer). It allows generics, which means you can make an ArrayList of Strings (ArrayList<String>), ArrayList of Integers (ArrayList<Integer>), ArrayList of Booleans (ArrayList<Boolean>). ArrayList<T> means you can substitute <T> with any object. ArrayList<? extends T> means you can substitute with any A, B, C, Potato, etc. that extends T.

Say you have the following classes:

class Admin extends User {
    //Stuff
}
class Student extends User {
    //Stuff
}
class Teacher extends User {
    //Stuff
}

You can even have an ArrayList of any child of a User object, for example: ArrayList<User>. To this list, you can add Student, Admin, and Teacher objects.

But if you do not specify what type of data this ArrayList holds (eg: List l = new ArrayList();), you can place any Object in it, causing it to not be type safe.

An example of generics abuse can be seen on Wikipedia.

Teets answered 1/8, 2013 at 3:29 Comment(1)
Perfect.But i I think my question is very clear. Generic is the way which we use for typesafe implementation.but Arraylist in its basics, is neither typesafe nor strongly type. is this true statement.Thiamine

© 2022 - 2024 — McMap. All rights reserved.