Site icon TestingDocs.com

Raw Type example in Java

Raw Type

A raw type is the name of a generic interface or class without any type of arguments. For example, given the generic class name SingleLinkedList Node class:

public class Node<T> {
 public T value;
 ...
}
public class SingleLinkedList<T> {
 private Node<T> head;
 private int size;
 ....
}

 

To create a parameterized type of SingleLinkedList<T> you need to supply an actual type for the type parameter T as shown below:

SingleLinkedList<Integer> intList = new SingleLinkedList<Integer>() ;

If you omit the type argument, you create a raw type for the list as shown below:

SingleLinkedList rawList = new SingleLinkedList<>();

SingleLinkedList is a raw type. References to generic type SingleLinkedList<T> should be parameterized as shown above.

Example

You can assign a parameterized type to its raw type:

SingleLinkedList<String> stringList = new SingleLinkedList<>();
SingleLinkedList rawList = stringBox; // This is ok

But if you assign a raw type to a parameterized type, you get a warning:

SingleLinkedList rawList = new SingleLinkedList(); // rawList is a raw type of SingleLinkedList<T>
SingleLinkedList<Integer> intList = rawList; // You get warning

Type safety: The expression of type SingleLinkedList needs unchecked conversion to conform to SingleLinkedList<Integer>

You also get a warning if you use a raw type to invoke generic methods defined in the corresponding generic type:

SingleLinkedList<String> stringList = new SingleLinkedList<>();
SingleLinkedList rawList = stringList;
rawList.addElementToTail(“TestingDocs”); // you get warning

Type safety: The method addElementToTail(Object) belongs to the raw type SingleLinkedList. References to generic type SingleLinkedList<T> should be parameterized

Sample Automation methods that tests both Integer and String single linked lists are shown below:

Sample JUnit Automation methods

package com.javaautomation.questions;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class SampleSingleLinkedListTest {
 SingleLinkedList<Integer> intList;
 SingleLinkedList<String> stringList;
 int beforeSize ;
 int afterSize ;
 
 @Before
 public void setUp() throws Exception {
 intList = new SingleLinkedList<Integer>() ;
 intList.addElementToTail(35);
 intList.addElementToTail(99);
 stringList = new SingleLinkedList<String>() ;
 stringList.addElementToTail("http://");
 stringList.addElementToTail("www.");
 stringList.addElementToTail("TestingDocs");
 }
 
 
 @Test
 public void addIntTest() {
 beforeSize = intList.size();
 intList.addElementToTail(12);
 afterSize = intList.size();
 assertEquals(afterSize ,beforeSize+1);
 }
 
 @Test
 public void addStringTest() {
 beforeSize = stringList.size();
 stringList.addElementToTail(".com");
 afterSize = stringList.size();
 assertEquals(afterSize ,beforeSize+1);
 }
 
 @Test
 public void removeStringTest() {
 System.out.println("List Before delete:");
 stringList.printListElements();
 beforeSize = stringList.size();
 stringList.removeElement("www.");
 afterSize = stringList.size();
 System.out.println("List After delete:");
 stringList.printListElements();
 assertEquals(afterSize ,beforeSize-1);
 
 }

}

 

Exit mobile version