Why this is not possible:
Because String and Integer are not in the same Object hierarchy.
Object / \ / \ String Integer
The casting which you are trying, works only if they are in the same hierarchy, e.g.
Object / / A / / B
In this case, (A) objB
or (Object) objB
or (Object) objA
will work.
Hence as others have mentioned already, to convert an integer to string use:
String.valueOf(integer)
, or Integer.toString(integer)
for primitive,
or
Integer.toString()
for the object.
Related Posts:
- Convert String to double in Java
- int cannot be converted to string?
- Converting String Array to an Integer Array
- Converting a string to an integer on Android
- Cannot invoke toString() on the primitive type int
- How do I convert a String to an int in Java?
- Java – Convert integer to string [duplicate]
- How do I compare strings in Java?
- How to convert/parse from String to char in java?
- Reverse a string in Java
- Reverse a string in Java
- Java String Split by “|”
- How to check if a String contains another String in a case insensitive manner in Java?
- String concatenation: concat() vs “+” operator
- How to remove the last character from a string?
- Java string to date conversion
- How to replace � in a string
- Change date format in a Java string
- Converting String to “Character” array in Java
- Best way to convert an ArrayList to a string
- How do I generate random integers within a specific range in Java?
- Best way to convert an ArrayList to a string
- String interpolation in Java 14 or 15
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- How to convert any Object to String?
- How can I properly compare two Integers in Java?
- How to split a string in Java
- Illegal Escape Character “\”
- Can I multiply strings in Java to repeat sequences?
- What does regular expression \\s*,\\s* do?
- Left Hand Side of an Assignment must be a Variable CharAt
- catDog string problem at Codingbat.com
- Java way to check if a string is palindrome
- Convert an integer to an array of characters : java
- Return string Input with parse.string
- What is the difference between String.subString() and String.subSequence()
- How do I apply the for-each loop to every character in a String?
- Java – removing first character of a string
- Java compressing Strings
- Explain the use of a bit vector for determining if all characters are unique
- Convert array of strings into a string in Java
- How to check whether a int is not null or empty?
- Declaring an unsigned int in Java
- Sort a single String in Java
- How to remove single character from a String
- How to remove single character from a String
- What is Java String interning?
- What is Java String interning?
- Why is there no String.Empty in Java?
- Java split string to array
- What is the easiest/best/most correct way to iterate through the characters of a string in Java?
- Exception in thread “main” java.lang.NumberFormatException: For input string: “S”
- If Java is Strongly typed then why does this code compile?
- How to check if my string is equal to null?
- what is Ljava.lang.String;@
- Convert String to int array in java
- How to implement infinity in Java?
- How to sort an array of objects in Java?
- How to sort an array of objects in Java?
- How to compress a String in Java?
- How to check if a String is numeric in Java
- How to replace a substring of a string
- Immutable class?
- How to check if the value is integer in java?
- Java end of file
- Unclosed Character Literal error
- Java: how to initialize String[]?
- Regular Expressions on Punctuation
- Add String Array to ArrayList
- StringIndexOutOfBoundsException String index out of range: 0
- How to extract a substring using regex
- How can I check if a single character appears in a string?
- Difference between String replace() and replaceAll()
- Convert an integer to an array of digits
- How to cast ArrayList<> from List<>
- Determine if a String is an Integer in Java [duplicate]
- How can I check if a value is of type Integer?
- How can I remove a substring from a given String?
- Java get String CompareTo as a comparator object
- How to use regex in String.contains() method in Java
- How to swap String characters in Java?
- Get an OutputStream into a String
- Difference between string object and string literal
- “Integer number too large” error message for 600851475143
- How to convert a String into an ArrayList?
- java: use StringBuilder to insert at the beginning
- How do I get the last character of a string?
- How to cast ArrayList<> from List<>
- Append a single character to a string or char array in java?
- Why int[] a = new int[1] instead of just int a?
- How to insert multiple tabs string in java?
- Append a single character to a string or char array in java?
- String is immutable. What exactly is the meaning?
- Removing double quotes from a string in Java
- Occurrences of substring in a string
- Append a single character to a string or char array in java?
- Java rules for casting
- In Java, how do I parse XML as a String instead of a file?
- Fastest way to put contents of Set
to a single String with words separated by a whitespace? - How to know if a given string is substring from another string in Java