

{"id":14039,"date":"2018-04-19T12:35:17","date_gmt":"2018-04-19T12:35:17","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=14039"},"modified":"2026-05-28T16:10:24","modified_gmt":"2026-05-28T10:40:24","slug":"java-wildcard","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-wildcard\/","title":{"rendered":"Java Wildcard &#8211; Types of Wildcard in Java"},"content":{"rendered":"<p>One of the most important and useful concepts of Java Generics is the Java wildcard. There are three types of wildcards in Java: upper-bounded wildcards, Lower-bounded wildcards, and Unbounded Wildcards. In this article, we will discuss all these types of wildcards in Java.<\/p>\n<h3>Wildcard in Java<\/h3>\n<p>Basically, in Java, question marks are known as Wildcards, which we can use in generic programming. Wildcards are used to represent the unknown type. We can use Java wildcards in a type of parameter, local variable, or field, and also as a return type.<\/p>\n<p>The Java generic types are not compatible with one another; this is one of the differences between them and arrays. This incompatibility is removed by using the wildcard(?) as an actual parameter.<\/p>\n<p>There are two types of parameters that a programmer can pass to a method in the form of in and out parameters, they are as follows:<\/p>\n<h4><strong>in variable<\/strong><\/h4>\n<p>in variable is a variable that provides the program with the actual data to work with. Let us consider a method, path(source, destination). In this method, the source variable acts as the input variable, as it provides the data to work with.<\/p>\n<h4><strong>out variable<\/strong><\/h4>\n<p>The out variable is a variable that calculates and updates the data provided by the in variable. Let us consider the same method, path(source, destination). In this method, the destination variable acts as the out variable, as it calculates the destination path with the help of the data provided by the source variable and updates it.<\/p>\n<h3>Types of Wildcards in Java<\/h3>\n<p>1. Upper Bounded Wildcards<br \/>\n2. Lower Bounded Wildcards<br \/>\n3. Unbounded Wildcards<\/p>\n<p>Let us discuss them one by one.<\/p>\n<h4>Upper Bounded Wildcards<\/h4>\n<p>The upper-bounded wildcards help us relax the restriction on the type of variable in the method. If we want to relax the restriction on the variable type, we use this type of wildcard.<\/p>\n<p>Let us take an example where we take a method sum in which we use the upper bound wildcard to take any type of variable as input. Can we do this by using the? character followed by the extends keyword.<\/p>\n<p><strong>Syntax of upper-bounded wildcards in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">private static double sum(List &lt; ?extends Number &gt; my_List)\r\n<\/pre>\n<p><strong>Code to understand the above example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.Wildcards;\r\nimport java.util.*;\r\npublic class Upperbound\r\n{\r\n     public static void main(String[] args) {\r\n    List &lt; Integer &gt; integer_List = Arrays.asList(1, 3, 5, 7);\/\/Integer List\r\n    System.out.println(\"Total sum is:\" + sum(integer_List));\r\n    List &lt; Double &gt; double_List = Arrays.asList(1.2, 3.4, 5.6, 7.8);\/\/Double List\r\n    System.out.print(\"Total sum is: \" + sum(double_List));\r\n  }\r\n  private static double sum(List &lt; ?extends Number &gt; my_List)\/\/Using ? to relax restriction of input type.\r\n  {\r\n    double sum = 0.0;\r\n    for (Number iterator: my_List) \r\n    {\r\n      sum = sum + iterator.doubleValue();\r\n    }\r\n    return sum;\r\n  }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">Total sum is:16.0<br \/>\nTotal sum is: 18.0<\/div>\n<h4>Lower Bounded Wildcards<\/h4>\n<p>To widen the use of the type of variable, we can use the Lower Bounded Wildcards.<\/p>\n<p><strong>For example<\/strong><span style=\"margin: 0px;padding: 0px\"><strong>,<\/strong> if<\/span>\u00a0we use List&lt;Integer&gt;, we will be able to take only a list of integers and not any other type of variables. Using Lower Bounded Wildcards, we can use List&lt;Number&gt; and List&lt;Object&gt; to take integer input. This also allows us to widen the type of variable and let us take any type of variable. This can be achieved by using the? Character followed by the super keyword.<\/p>\n<p><strong>Syntax of lower-bounded wildcards in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public static void printLowerBounded(List &lt; ?super Integer &gt; LowerBounded_list) \r\n<\/pre>\n<p><strong>Code to understand the above example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.Wildcards;\r\nimport java.util.*;\r\npublic class Lowerbound\r\n{\r\n  public static void main(String[] args) \r\n  {\r\n    List &lt; Integer &gt; int_List = Arrays.asList(3, 5, 7, 9);\r\n    printLowerBounded(int_List);\r\n    List &lt; Number &gt; number_List = Arrays.asList(10.5, 20.2, 30.3, 40.5);\r\n    printLowerBounded(number_List);\r\n  }\r\n  public static void printLowerBounded(List &lt; ?super Integer &gt; LowerBounded_list) \r\n  {\r\n    System.out.println(LowerBounded_list);\r\n  }\r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">[3, 5, 7, 9]<br \/>\n[10.5, 20.2, 30.3, 40.5]<\/div>\n<h4>Unbounded Wildcards<\/h4>\n<p>We can use the unbounded Wildcard to specify the type of wildcard along with the wildcard character(?). We generally use this wildcard when the code inside the method is using the objects and when the execution of the method does not depend upon the parameter type.<\/p>\n<p><strong>Syntax of unbounded wildcards in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">private static void print_List(List &lt; ?&gt;Unbounded_list)\r\n<\/pre>\n<p><strong>Code to understand Unbounded Wildcards:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.DataFlair.Wildcards;\r\nimport java.util.*;\r\npublic class Unbounded\r\n{\r\n   public static void main(String[] args) \r\n   {\r\n    List &lt; Integer &gt; int_List = Arrays.asList(1, 3, 5, 7);\/\/Integer List \r\n    List &lt; Double &gt; double_List = Arrays.asList(10.2, 15.3, 20.4, 25.5);\/\/Double list \r\n    print_List(int_List);\r\n    print_List(double_List);\r\n  }\r\n  private static void print_List(List &lt; ?&gt;Unbounded_list)\/\/unbounded wildcard used \r\n  {\r\n    System.out.println(Unbounded_list);\r\n  } \r\n}\r\n<\/pre>\n<p><strong>The output of the above code:<\/strong><\/p>\n<div class=\"code-output\">[1, 3, 5, 7]<br \/>\n[10.2, 15.3, 20.4, 25.5]<\/div>\n<p>Let us summarize the Syntax of the three WildCards:<\/p>\n<p><strong>1. Upper Bound Wildcard:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;? extends Type&gt;\r\n<\/pre>\n<p><strong>2. Lower bound Wildcard:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;? super &lt;Type&gt;\r\n<\/pre>\n<p><strong>3. Unbounded Wildcard:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?&gt;\r\n<\/pre>\n<h3>Guidelines for Using Wildcard in Java<\/h3>\n<p>It is important to understand when to use which kind of Wildcard:<\/p>\n<ul>\n<li>If there is an in variable in the parameter, we use the extends keyword with the wildcard character, i.e, Upper bound Wildcard.<\/li>\n<li>When there is an out variable in the parameter, we use the super keyword with the wildcard character, i.e, Lower bound Wildcard.<\/li>\n<li>If the variables can be accessed with the Object class method, then we use only the wildcard character, i.e., Unbounded wildcard.<\/li>\n<li>If there are both in and out variables in the parameter, then we do not need to use any wildcard.<\/li>\n<li>Wildcards are not meant to be used as a return value. If it is used as a return type, it will make the processing of the API harder because wildcards are not of any type.<\/li>\n<li>Only a single bound can be used in a wildcard. Attempting to use more than one bound will raise an error.<\/li>\n<li>Two operations cannot be performed on a wildcard.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>In this article, we saw what exactly wildcards are and also the different types of wildcards. We also discussed when to use which wildcard. The wildcard is used along with Java generics. It is a very important concept to handle parameters according to our needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most important and useful concepts of Java Generics is the Java wildcard. There are three types of wildcards in Java: upper-bounded wildcards, Lower-bounded wildcards, and Unbounded Wildcards. In this article, we&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":108850,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[7746,7747,14055,14115,15135,15179,16213],"class_list":["post-14039","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java-wildcard","tag-java-wildcards","tag-syntax-of-lower-bounded-wildcard-in-java","tag-syntax-of-upper-bounded-wildcard-in-java","tag-unbounded-java-wildcard-example","tag-upper-bounded-java-wildcard-example","tag-wildcards-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Wildcard - Types of Wildcard in Java - DataFlair<\/title>\n<meta name=\"description\" content=\"Java Wildcard Tutorial- types of Wildcard in Java:Unbounded Wildcard,Upper bounded wildcard, Lower Bounded wildcards with example &amp; syntax\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-wildcard\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Wildcard - Types of Wildcard in Java - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Java Wildcard Tutorial- types of Wildcard in Java:Unbounded Wildcard,Upper bounded wildcard, Lower Bounded wildcards with example &amp; syntax\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-wildcard\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-19T12:35:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-28T10:40:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/types-of-java-wildcards.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Wildcard - Types of Wildcard in Java - DataFlair","description":"Java Wildcard Tutorial- types of Wildcard in Java:Unbounded Wildcard,Upper bounded wildcard, Lower Bounded wildcards with example & syntax","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/java-wildcard\/","og_locale":"en_US","og_type":"article","og_title":"Java Wildcard - Types of Wildcard in Java - DataFlair","og_description":"Java Wildcard Tutorial- types of Wildcard in Java:Unbounded Wildcard,Upper bounded wildcard, Lower Bounded wildcards with example & syntax","og_url":"https:\/\/data-flair.training\/blogs\/java-wildcard\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-04-19T12:35:17+00:00","article_modified_time":"2026-05-28T10:40:24+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/types-of-java-wildcards.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Java Wildcard &#8211; Types of Wildcard in Java","datePublished":"2018-04-19T12:35:17+00:00","dateModified":"2026-05-28T10:40:24+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/"},"wordCount":793,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/types-of-java-wildcards.webp","keywords":["Java Wildcard","Java Wildcards","syntax of Lower Bounded wildcard in java","syntax of Upper bounded wildcard in java","Unbounded Java Wildcard example","Upper bounded Java wildcard example","Wildcards in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-wildcard\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/","url":"https:\/\/data-flair.training\/blogs\/java-wildcard\/","name":"Java Wildcard - Types of Wildcard in Java - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/types-of-java-wildcards.webp","datePublished":"2018-04-19T12:35:17+00:00","dateModified":"2026-05-28T10:40:24+00:00","description":"Java Wildcard Tutorial- types of Wildcard in Java:Unbounded Wildcard,Upper bounded wildcard, Lower Bounded wildcards with example & syntax","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-wildcard\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/types-of-java-wildcards.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/types-of-java-wildcards.webp","width":1200,"height":628,"caption":"types of java wildcards"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-wildcard\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java Wildcard &#8211; Types of Wildcard in Java"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/14039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=14039"}],"version-history":[{"count":10,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/14039\/revisions"}],"predecessor-version":[{"id":148479,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/14039\/revisions\/148479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/108850"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=14039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=14039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=14039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}