• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Incompatible types assignment

 
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I want to know why does line 14 even compile ? But it throws an exception. But why should it compile !  In the below code, String and char are incompatible it says so cannot be cast. But in the above code, a similar situation is seen, where, String type and Integer type is being attempted to be cast to a char . isn't that wrong then ?



 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that issue is that a String is actually, internally an array of char. And you cannot convert an array of char into a single char, just like you cannot put an array of int into an single int.
Sure, your second example uses only one character "a", but that is still an array of char internally.
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got me wrong Pete.

My question is only about line 14 in the first code snippet. the second snippet is just a similar case exhibiting a different behaviour.
and my question is not about why can't a string with a single character be stored a char type.

my question is : in the first code snippet, how can String or Integer be cast to a char without any compiler errors ?

in second snippet it shows an error as expected because its an incompatible cast. then why it doesn't happen in the first snippet.
this is what i am asking.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that Lilou.

I think that the first snippet compiles due to the fact that the java compiler is not sure what the out come of the Ternary statement is.
However if this becomes an IF statement then the compiler knows as shown below, but commented out:

So then I think that question has to do with how the java compiler teats the Ternary statement.
I was told that the Ternary statement can be used instead of an IF statement to get the same result.
This does not appear to be the case.
Maybe one has to look into the Java Language Specification https://docs.oracle.com/javase/specs/ to get the definitive answer.
 
Enthuware Software Support
Posts: 4468
42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because the type of the conditional operator when operands 2 and 2 are objects is Object. Specification defines it clearly in Section 15.25, Table 15.25-E. The compiler, therefore, has no choice but to accept the assignment.
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see.

I have a doubt  !

Is the below rule/working applicable only for overloading of methods and not for all numeric binary promotions ?


The explanation given is :Here we have a problem. Java is happy to convert the int 4 to a long 4 or an Integer 4.
It cannot handle converting in two steps to a long and then to a Long. If we had public
static void play(Object o) { }, it would match because only one conversion would be
necessary: from int to Integer.


so is this one level conversion only applied during overloading ?
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after experimenting with some code I have come to the following conclusions :

• In overloading, the conversion involving any kind of boxing has to be done in only 1 step. Unboxing (anywhere) need not follow this. (but make sure that the conversions are valid to be performed implicitly)
• Boxing other than this situation,  will follow only one step conversion plus the rules below :

              • if the expression is a constant expression of type byte, short, char or int:A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
                      Byte and the value of the constant expression is representable in the type byte.
                      Short and the value of the constant expression is representable in the type short.
                      Character and the value of the constant expression is representable in the type char".

Am I right ?
 
Lilou Laure
Ranch Hand
Posts: 117
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Anilprem wrote:This is because the type of the conditional operator when operands 2 and 2 are objects is Object. Specification defines it clearly in Section 15.25, Table 15.25-E. The compiler, therefore, has no choice but to accept the assignment.



and should this table be learnt for the exam ?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:I want to know why does line 14 even compile ? But it throws an exception. But why should it compile !  In the below code, String and char are incompatible it says so cannot be cast. But in the above code, a similar situation is seen, where, String type and Integer type is being attempted to be cast to a char . isn't that wrong then ?


The reason why line14 successfully compiles is you

If you remove the cast (to char) you'll get two compiler errors (and probably the ones you were expecting): "Type mismatch: cannot convert from Integer to char" and "Type mismatch: cannot convert from String to char".

Because you have added the cast (to char) you are actually telling the compiler: "Hi compiler, sit back and relax! No need to worry about this line! I am the Java expert here and I know what I am doing" And therefore the compiler treats the result of the ternary operator as an Object and it's allowed to cast to char. These assignments are also allowed, so the compiler might treat the result of the ternary operator as these types as well
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lilou Laure wrote:and should this table be learnt for the exam ?


No! I can't remember having encountered an exam question with a ternary operator with different (reference) types being assigned to another reference type. Maybe you might see such a ternary operator being used in a print statement, so the type of the result is less important. Or maybe with different primitive types.

As a general rule (and probably much easier to remember than that table ): you can assign a ternary operator with two different reference types to a reference variable of any type which is shared between both reference types. So in your example String and Integer share class Object and interfaces Serializable and Comparable<?>. So any of the following assignments are allowed
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic