• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

== operator with promotion and autoboxing

 
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am having a tough time understanding the answer for this Question. Can anyone help?

Q:Which of the If statement be executed?

Answers:
i3 is autoboxed
i4 is promoted

What i want to know is " When will the autoboxing happen with == operator and when will it not."
Also I want to know " when will promotion happen with == operator and when will it not.

My understanding is that the == operator will throw an compiler error, if the operands are not of comparable type.
In line 8, we are comparing a object with an primitive.
In line 9 we are comparing a int variable with and float variable.

Thanks,
Tony Singarayar
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does this exercise come from? It has some mistakes: Int and If don't exist in Java and "i4 is promoted" will never be printed!
 
Tony Singarayar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This exercise is from enthuware, and i combined two Questions in to one for understanding purpose.

You are right, "i4 is promoted" was never printed. and i had typo's in the code.

I have attached the correct code here...
The output is "i3 is autoboxed.

Can you help me to understand the below:-
1.When are the times autoboxing kicks in when using == operator?

2. If we comparing primitives operands with == operator, I saw that code compiled fine when i compared int and float, shouldn't it fail to compile as int and float are different types.


Thanks,
Tony Singarayar
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code snippet looks a whole lot better! Let's answer your questions in reverse order.

Tony Singarayar wrote:2. If we comparing primitives operands with == operator, I saw that code compiled fine when i compared int and float, shouldn't it fail to compile as int and float are different types.


All primitives can be compared using the == operator. They are not considered to be different types. You can in fact compare a char with a float.


But when you use the appropriate wrapper classes, you'll be in trouble. Big trouble!


Tony Singarayar wrote:1.When are the times autoboxing kicks in when using == operator?


When == is used to compare a primitive to a wrapper, the wrapper will be unwrapped and the comparison will be primitive to primitive. So therefore the message "i3 is autoboxed" is actually wrong, because i3 is NOT autoboxed but I1 is unboxed (unwrapped). Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. If the conversion goes the other way, this is called unboxing.

That's why this code compiles without any problem:

If both fl1 and fl2 are unboxed (unwrapped) to compare with the primitive ch1. If ch1 would be autoboxed (as the printed message suggests), you would compare a Float with a Character which would not compile (as shown in the previous code snippet).

Hope it helps!
 
Tony Singarayar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Roel,
Understood 100% about using == operator for comparing wrapper objects...
And understood about unboxing and then how == operator compares a wrapper object and a primitive...

But i don't understand the below code.

ch1 == fl2 -> how is conversion happening here ? And how is the answer true? Can you please explain.

Thanks,
Tony Singarayar




 
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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Singarayar wrote:Understood 100% about using == operator for comparing wrapper objects...
And understood about unboxing and then how == operator compares a wrapper object and a primitive...


Hooray!


Tony Singarayar wrote:ch1 == fl2 -> how is conversion happening here ? And how is the answer true? Can you please explain.


As you know (and if you don't you should look into it) a char is an unsigned int with a range from 0 to 65535 (inclusive). So it stores its value as an integer under the hood. So you can assign a character (between single quotes), but you can also assign an integer (which is within range) to a char variable (which is of course not a good coding practice, but you definitely can).

So this code compiles successfully (and should help to clear your doubts and answer your question):


The integer you assign MUST (as with any primitive data type) be within its range otherwise compilation fails (unless you use an appropriate explicit cast):


Hope it helps!


Disclaimer: you do NOT need to know the integer value for a character.
 
Tony Singarayar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roel,
Understood how char value of '5' is stored and how can it be considered as int.Thanks.

But I am still confused about the comparison part.

When we are comparing a 5-of char with 53-of float...How is the answer worked out to be true.?
Is any promotion done, If so, wouldn't it just make char to 5.0 and when compared with 53-float, shouldn't the result be just false?


Thanks,
Tony Singarayar
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Singarayar wrote:When we are comparing a 5-of char with 53-of float...How is the answer worked out to be true.?
Is any promotion done, If so, wouldn't it just make char to 5.0 and when compared with 53-float, shouldn't the result be just false?


The char uses an integer under the hood to store its value. So the char doesn't have '5' (char, single quotes) as value, but 53 (integer, no quotes). So both the char and the float have 53 as value (and these are considered equal).

And to put the emphasis on "a char is stored internally as an integer" a little bit more, you can do calculations with chars: addition, subtraction, multiply,... Even using the modulo operator is allowed

Crazy cool stuff isn't it?

Hope it helps!
 
Tony Singarayar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The char uses an integer under the hood to store its value. So the char doesn't have '5' (char, single quotes) as value, but 53 (integer, no quotes). So both the char and the float have 53 as value (and these are considered equal).


ah.....This is the point that I wasn't able to understand!!!Now i got it

Thank You Roel for help!!!

Tony Singarayar
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your previous posts you used javascript:emoticon(':)'); to insert the :) emoticon. Is this something you typed yourself or was inserted for you when you clicked on the emoticon? Only the part between the single quotes is needed to show the emoticon, so :) or :thumbup: or :shock: will do the job. I already edited your previous posts accordingly.

Also nice to know: instead of making a "thank you" post, you could also the post(s) which you liked. It's easier, faster and other ranchers will see immediately which are the "starred" posts.
 
Tony Singarayar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah sure.
Thanks!!
Tony Singarayrar
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic