Downcasting is allowed when there is a possibility that it succeeds at run time:
Object o = getSomeObject(), String s = (String) o; // this is allowed because o could reference a String
In some cases this will not succeed:
Object o = new Object(); String s = (String) o; // this will fail at runtime, because o doesn't reference a String
When a cast (such as this last one) fails at runtime a ClassCastException
will be thrown.
In other cases it will work:
Object o = "a String"; String s = (String) o; // this will work, since o references a String
Note that some casts will be disallowed at compile time, because they will never succeed at all:
Integer i = getSomeInteger(); String s = (String) i; // the compiler will not allow this, since i can never refe
Related Posts:
- Casting variables in Java
- What is an instance variable in Java?
- super() in Java
- What’s the syntax for mod in java
- What does the Java assert keyword do, and when should it be used?
- Convert String to double in Java
- How do I make a delay in Java?
- max value of integer
- Converting double to integer in Java
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- Explanation of ClassCastException in Java
- What is the difference between == and equals() in Java?
- What does “?” mean in Java?
- Java – No enclosing instance of type Foo is accessible
- Why I am getting java.lang.AbstractMethodError errors?
- fix java.net.SocketTimeoutException: Read timed out
- Convert an integer to an array of characters : java
- How to override equals method in Java
- java.text.ParseException: Unparseable date
- Java – String cannot be converted to int
- java.io.IOException: Broken pipe
- java.util.NoSuchElementException: No line found
- If Java is Strongly typed then why does this code compile?
- Even though JRE 8 is installed on my MAC -” No Java Runtime present,requesting to install ” gets displayed in terminal
- Reason for the exception java.lang.VerifyError: Bad type on operand stack
- The specified DSN contains an architecture mismatch between the Driver and Application. JAVA
- Java “params” in method signature?
- How do I time a method’s execution in Java?
- repaint() Method in Java
- Exception in thread “main” java.lang.reflect.InvocationTargetException
- How to set String’s font size, style in Java using the Font class?
- How to use the command update-alternatives –config java
- java array error “array required but int found”
- this: Cannot use this in static context
- What’s the difference between SoftReference and WeakReference in Java?
- How to draw lines in Java
- How to cast ArrayList<> from List<>
- NoClassDefFoundError in Java: com/google/common/base/Function
- Convert boolean to int in Java
- How to cast ArrayList<> from List<>
- In Java, what is a shallow copy?
- Good way to encapsulate Integer.parseInt()
- Eclipse IDE – Error: Build path specifies execution environment Java SE 1.7
- Java: Not a statement
- ArrayList of int array in java
- Why cannot cast Integer to String in java?
- How to get the filename without the extension in Java?
- Java rules for casting
- How can I upgrade to Java 1.8 on an Amazon Linux Server?
- How can I convert MP3 file to a Base64 encoded string? [closed]
- How to format strings in Java
- What is a sanity test/check
- What exactly is Apache Camel?
- Difference between StringBuilder and StringBuffer
- What does ERR_SPDY_PROTOCOL_ERROR mean in nginx?
- bad operand types for binary operator “&” java
- How can I use pointers in Java?
- What is a bitmask and a mask?
- Ping – Request timed out
- How do I remove version tracking from a project cloned from git?
- How do I limit the number of decimals printed for a double?
- Unfortunately MyApp has stopped. How can I solve this?
- exception in thread ‘main’ java.lang.NoClassDefFoundError:
- java.io.FileNotFoundException: the system cannot find the file specified
- How do I get a list of built-in data sets in R?
- Explain the use of a bit vector for determining if all characters are unique
- Difference between .o and .ko file
- R error: all arguments must have the same length
- Convert java.util.Date to String
- How do I do a deep copy of a 2d array in Java?
- Error: Selection does not contain a main type
- Java, “Variable name” cannot be resolved to a variable
- JRE 1.7 – java version – returns: java/lang/NoClassDefFoundError: java/lang/Object
- Java and Windows – error: illegal escape character
- How should I copy Strings in Java?
- Bank Account Java Program
- List of all special characters that need to be escaped in a regex
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
- java.lang.ArithmeticException: / by zero
- No function matches the given name and argument types
- Eclipse error, “The selection cannot be launched, and there are no recent launches”
- STS launch error – Java was started but returned exit code=13
- RecyclerView onClick
- difference between equals() and hashCode()
- Adding days to a date in Java
- The C# Shorthand getters and setters
- What’s the reason I can’t create generic array types in Java?
- Arithmetic Overflow in mips
- How to pass an object from one activity to another on Android
- How to change angular port from 4200 to any other
- Finding the size of a char array in Java
- fixing errors on a program to call methods in java
- How to Set the Background Color of a JButton on the Mac OS
- What is “pass-by-name” and how does it work exactly?
- How to easily convert a BufferedReader to a String?
- intellij idea – Error: java: invalid source release 1.9
- How fix Error:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
- Is it okay to throw NullPointerException programmatically?
- Android: how to create Switch case from this?
- Rendering javascript in render_callback php Code