{"id":1407,"date":"2017-02-17T16:57:21","date_gmt":"2017-02-17T16:57:21","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1407"},"modified":"2021-03-21T09:02:48","modified_gmt":"2021-03-21T09:02:48","slug":"raw-type-example-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/raw-type-example-in-java\/","title":{"rendered":"Raw Type example in Java"},"content":{"rendered":"<article id=\"post-1502\" class=\"post-1502 post type-post status-publish format-standard hentry category-java-automation\">\n<div class=\"entry-content\">\n<h3>Raw Type<\/h3>\n<p>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:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class Node&lt;T&gt; {\r\n public T value;\r\n ...\r\n}\r\npublic class SingleLinkedList&lt;T&gt; {\r\n private Node&lt;T&gt; head;\r\n private int size;\r\n ....\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>To create a parameterized type of SingleLinkedList&lt;T&gt; you need to supply an actual type for the type parameter T as shown below:<\/p>\n<p>SingleLinkedList&lt;Integer&gt; intList = new SingleLinkedList&lt;Integer&gt;() ;<\/p>\n<p>If you omit the type argument, you create a raw type for the list as shown below:<\/p>\n<p>SingleLinkedList rawList = new SingleLinkedList&lt;&gt;();<\/p>\n<p>SingleLinkedList is a raw type. References to generic type SingleLinkedList&lt;T&gt; should be parameterized as shown above.<\/p>\n<h3><strong>Example<\/strong><\/h3>\n<p>You can assign a parameterized type to its raw type:<\/p>\n<p>SingleLinkedList&lt;String&gt; stringList = new SingleLinkedList&lt;&gt;();<br \/>\nSingleLinkedList rawList = stringBox; \/\/ This is ok<\/p>\n<p>But if you assign a raw type to a parameterized type, you get a warning:<\/p>\n<p>SingleLinkedList rawList = new SingleLinkedList(); \/\/ rawList is a raw type of SingleLinkedList&lt;T&gt;<br \/>\nSingleLinkedList&lt;Integer&gt; intList = rawList; \/\/ You get warning<\/p>\n<p><strong>Type safety: The expression of type SingleLinkedList needs unchecked conversion to conform to SingleLinkedList&lt;Integer&gt;<\/strong><\/p>\n<p>You also get a warning if you use a raw type to invoke generic methods defined in the corresponding generic type:<\/p>\n<p>SingleLinkedList&lt;String&gt; stringList = new SingleLinkedList&lt;&gt;();<br \/>\nSingleLinkedList rawList = stringList;<br \/>\nrawList.addElementToTail(\u201cTestingDocs\u201d); \/\/ you get warning<\/p>\n<p><strong>Type safety: The method addElementToTail(Object) belongs to the raw type SingleLinkedList. References to generic type SingleLinkedList&lt;T&gt; should be parameterized<\/strong><\/p>\n<p>Sample Automation methods that tests both Integer and String single linked lists are shown below:<\/p>\n<h3>Sample JUnit Automation methods<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.javaautomation.questions;\r\n\r\nimport static org.junit.Assert.*;\r\nimport org.junit.Before;\r\nimport org.junit.Test;\r\n\r\npublic class SampleSingleLinkedListTest {\r\n SingleLinkedList&lt;Integer&gt; intList;\r\n SingleLinkedList&lt;String&gt; stringList;\r\n int beforeSize ;\r\n int afterSize ;\r\n \r\n @Before\r\n public void setUp() throws Exception {\r\n intList = new SingleLinkedList&lt;Integer&gt;() ;\r\n intList.addElementToTail(35);\r\n intList.addElementToTail(99);\r\n stringList = new SingleLinkedList&lt;String&gt;() ;\r\n stringList.addElementToTail(\"http:\/\/\");\r\n stringList.addElementToTail(\"www.\");\r\n stringList.addElementToTail(\"TestingDocs\");\r\n }\r\n \r\n \r\n @Test\r\n public void addIntTest() {\r\n beforeSize = intList.size();\r\n intList.addElementToTail(12);\r\n afterSize = intList.size();\r\n assertEquals(afterSize ,beforeSize+1);\r\n }\r\n \r\n @Test\r\n public void addStringTest() {\r\n beforeSize = stringList.size();\r\n stringList.addElementToTail(\".com\");\r\n afterSize = stringList.size();\r\n assertEquals(afterSize ,beforeSize+1);\r\n }\r\n \r\n @Test\r\n public void removeStringTest() {\r\n System.out.println(\"List Before delete:\");\r\n stringList.printListElements();\r\n beforeSize = stringList.size();\r\n stringList.removeElement(\"www.\");\r\n afterSize = stringList.size();\r\n System.out.println(\"List After delete:\");\r\n stringList.printListElements();\r\n assertEquals(afterSize ,beforeSize-1);\r\n \r\n }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1503\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Single-Linked-List-Junit-Automation.png\" alt=\"Single Linked List Junit Automation\" width=\"1183\" height=\"711\" title=\"\"><\/p>\n<\/div>\n<\/article>\n<div id=\"comments\" class=\"comments-area\">\n<div id=\"respond\" class=\"comment-respond\">\n<form id=\"commentform\" class=\"comment-form\" action=\"https:\/\/questions.testingdocs.com\/wp-comments-post.php\" method=\"post\" novalidate=\"\">\n<p class=\"comment-form-comment\">\n<\/form>\n<\/div>\n<\/div>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1407\" data-postid=\"1407\" class=\"themify_builder_content themify_builder_content-1407 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>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&lt;T&gt; { public T value; &#8230; } public class SingleLinkedList&lt;T&gt; { private Node&lt;T&gt; head; private int size; &#8230;. } &nbsp; To create a parameterized [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1407","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1407"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1407\/revisions"}],"predecessor-version":[{"id":19802,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1407\/revisions\/19802"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}