-
Notifications
You must be signed in to change notification settings - Fork 4
Raw Types with Generic
When declaring a variable or instantiating an object of a generic class, one might notice that Java does not produce any error if the type parameter is left out. Said syntax is valid as what you're doing is creating a variable or instantiating an object of a raw type.
ArrayList list = new ArrayList<Integer>();
When assigning to a variable of a raw type, the behaviour is similar to that of when a wildcard (?
) is used as the type parameter.
// Wildcard example
ArrayList<?> list = new ArrayList<Integer>();
list = new ArrayList<String>();
// Raw type example
ArrayList list = new ArrayList<Integer>();
list = new ArrayList<String>();
However, do not be mistaken. Raw types are not the same as wildcards. Wildcards come with lots of restrictions that raw types do not have. Said restrictions make wildcards a lot safer to use.
// Wildcard example
ArrayList<?> list = new ArrayList<Integer>();
list.add(1) // Will procuce an error
// Raw type example
ArrayList list = new ArrayList<Integer>();
list.add(1) // No error
Base on code like the following, one might argue that perhaps raw types are just generic types that use the Object
type parameter.
// Raw type example
ArrayList list = new ArrayList<Integer>();
list.add(1)
list.add("Hello")
list.add(1.3)
System.out.println(list) // Will print [1, Hello, 1.3]
However, this is wrong as raw types allow for even stranger behaviour.
ArrayList<String> list2 = list;
list.get(1) // No error as index 1 contains "Hello"
list2.get(0) // ClassCastException
I am able to assign list
to a variable of type ArrayList<String>
. An exception is only thrown when I try to access an items in list2
that isn't of type String
.
Althought an absence of type restrictions might seem like a good thing for those coming from dynamic typed languages (e.g. python and javascript), this is actually extremely dangerous in a statically typed language like Java. It allows for "strange" code that breaks the conventions of statically typed languages, causing behaviours that most programmers would not expect. Raw types exist for backwards compatibility reasons and programmers should be careful to avoid explicity using them at all cost.
Peer Learning
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Stu
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
SSH Without Password
Copying Files From PE Nodes
Using tmux
CS2030 Contents
Lecture 1 SummaryLecture 2 Summary
Access Modifiers
Lecture 3 Summary (Polymorphism)
Compile Time Type VS Runtime Type
Abstraction, Encapsulation, Inheritance, and Polymorphism
SOLID Principles
Class VS Abstract Class VS Interface
Comparable VS Comparator
Generic Types T
HashMap
Raw Types with Generic
Lambda expression
PECS (Producer Extends Consumer Super)
Optional
Streams
Parallel Streams
Monad
Functors and Monads with Category Theory