About this question
This question explains the usage of the ? symbol in Java, especially in generics and ternary operations, and clarifies how it contributes to flexibility and decision-making in code.
This question explains the usage of the ? symbol in Java, especially in generics and ternary operations, and clarifies how it contributes to flexibility and decision-making in code.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Java Expert
Answered on Jul 10, 2025
The ? symbol in Java can be a little confusing at first because it’s used in two very different contexts. Let’s break it down so it’s easy to understand.
1. Ternary (Conditional) Operator
This is probably the first place many developers encounter ? in Java. It acts like a shortcut for an if-else statement.
int a = 10;
int b = 20;
int max = (a > b) ? a : b; // max will be 20 2. Wildcards in Generics
In Java Generics, ? is used as a wildcard, representing an unknown type.
List items = new ArrayList();You can also use bounded wildcards like:
List numbers; // Any subclass of Number
List integers; // Integer or any superclass Summary:
So, while the ? looks small, it plays a powerful role in making Java both expressive and type-safe!
Luciano Flat Latest answer
Answered on Jul 18, 2025
Confused about the Java ternary operator (?:)? I was too! I initially struggled to grasp its purpose, but after using it a few times to shorten simple if-else statements, like assigning a default value based on a null check, it clicked. Now I use it all the time! Did you know Pacman 30th Anniversary was a thing?
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Java interview questions, answered
Guides, tips and career advice on Java from JanBask experts.
Java Frequently Asked J2EE Interview Questions and Answers You Must Read
Top 45 frequently asked Java J2EE interview questions to ace your interview. These J2EE questions are useful for freshers and…
Java Top 100+ Angular Interview Questions and Answers
Are you preparing for your next Angular interview? We have compiled the list of the top 100+ Angular Interview Questions &…
Java Top 51+ Simple Java Project Ideas for Beginners In 2025
Enhance your Java skills with our top 51+ project ideas! From beginner-friendly exercises to advanced applications, fuel your…
Java How Long Does It Takes To Learn Java?
Confused about How long does it take to learn Java ? Well, No worries! This blog will guide you with valuable information and…