This is the ternary conditional operator, which can be used anywhere, not just the print statement. It’s sometimes just called “the ternary operator”, but it’s not the only ternary operator, just the most common one.
Here’s a good example from Wikipedia demonstrating how it works:
A traditional if-else construct in C, Java and JavaScript is written:
if (a > b) { result = x; } else { result = y; }
This can be rewritten as the following statement:
result = a > b ? x : y;
Basically it takes the form:
boolean statement ? true result : false result;
So if the boolean statement is true, you get the first part, and if it’s false you get the second one.
Try these if that still doesn’t make sense:
System.out.println(true ? "true!" : "false."); System.out.println(false ? "true!" : "false.");
Related Posts:
- What does “?” mean in Java? [duplicate]
- What does “?” mean in Java? [duplicate]
- Does Python have a ternary conditional operator?
- Does Python have a ternary conditional operator?
- Creating a “logical exclusive or” operator in Java
- What does “|=” mean? (pipe equal operator)
- The difference between += and =+
- What does “?” mean in Java?
- What is the Java ?: operator called and what does it do?
- What does the colon (:) operator do?
- How to check if my string is equal to null?
- Short form for Java if statement
- Java Not Greater than Or Equal to Operator for Char Type
- Reverse a string in Java
- “Char cannot be dereferenced” error
- Explanation on Integer.MAX_VALUE and Integer.MIN_VALUE to find min and max value in an array
- How to check if a String contains another String in a case insensitive manner in Java?
- Failed to write core dump. minidumps are not enabled by default on client version of windows
- Non-static variable cannot be referenced from a static context
- SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
- “Missing return statement” within if / for / while
- How to convert byte array to string and vice versa?
- good example of Javadoc
- How do popBackStack() and replace() operations differ?
- Can’t find mysqladmin on Mac OSX 10.6.8
- What is an AssertionError? In which case should I throw it from my own code?
- How to write a Unit Test?
- Best way to Format a Double value to 2 Decimal places
- Collision resolution in Java HashMap
- “Content is not allowed in prolog” when parsing perfectly valid XML on GAE
- Java Undefined Object
- Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
- How to convert an ArrayList containing Integers to primitive int array?
- Left Hand Side of an Assignment must be a Variable CharAt
- Compiling a java program into an executable
- Java Installation Not Completed / Unable to install Java
- Convert an integer to an array of characters : java
- What is Java String interning?
- How to write an inline IF statement in JavaScript?
- What condition does while(true) test? When is it true and false?
- What is the significance of log4j.rootLogger property in log4j.properties file? What happens if I don’t use this property?
- “Instantiating” a List in Java? [duplicate]
- HTTP Status 405 – Request method ‘POST’ not supported (Spring MVC)
- Missing `server’ JVM (Java\jre7\bin\server\jvm.dll.)
- Convert String array to ArrayList
- java uses or overrides a deprecated API error
- What is setBounds and how do I use it?
- Exception in thread “main” java.lang.NumberFormatException: For input string: “S”
- How to pass a function as a parameter in Java?
- ResultSet exception – before start of result set
- The activity must be exported or contain an intent-filter
- How to set JAVA_HOME environment variable on Mac OS X 10.9?
- Remove last character of a StringBuilder?
- Why SpringMVC Request method ‘GET’ not supported?
- Error: JAVA_HOME is not defined correctly executing maven
- Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory
- Android – SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
- How to sort an array of objects in Java?
- The specified child already has a parent. You must call removeView() on the child’s parent first (Android)
- HTTP Status 500 Error instantiating servlet class
- How to convert the following json string to java object?
- Javascript: operator overloading
- Java end of file
- A KeyValuePair in Java
- Unclosed Character Literal error
- Java error: void cannot be converted to String
- CreateProcess error=2, The system cannot find the file specified
- Add String Array to ArrayList
- Java Array Sort descending?
- How to take input as String with spaces in java using scanner
- Address already in use: JVM_Bind java
- java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [duplicate]
- How can I check if a value is of type Integer?
- Using regex to match any character except =
- Why is a ConcurrentModificationException thrown and how to debug it
- How to sort a HashSet?
- Variable used in lambda expression should be final or effectively final
- Populate int array with for loop in Java
- calling boolean method, heads or tails
- Android Color Picker
- How to fix an UnsatisfiedLinkError (Can’t find dependent libraries) in a JNI project
- How to Delete a topic in apache kafka [duplicate]
- Java Main Game Loop
- “Integer number too large” error message for 600851475143
- Adding JPanel to JFrame
- Append a single character to a string or char array in java?
- Java printf formatting to print items in a table or columns
- How to resolve “Syntax error on token “else”” in Java?
- What’s the difference between Jetty and Netty?
- Is there a way to simulate the C++ ‘friend’ concept in Java?
- Good way to encapsulate Integer.parseInt()
- What is the difference between “Double” and “double” in Java?
- What is the purpose of the default keyword in Java?
- In Java, how do I parse XML as a String instead of a file?
- Java Returning method which returns arraylist?
- String cannot be resolved to a type – Java RAD
- IntelliJ IDEA “The selected directory is not a valid home for JDK”
- Simple timeout in java
- Eclipse message saying List cannot be resolved to a type
- Can’t seem to disable Java Automatic Update