• Post Reply Bookmark Topic Watch Topic
  • New Topic

This operation made my cerebrum crash  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings ! How is everyone reading this post doing? Happy I hope.

I stumbled upon a weird case in Java,
Take a look at this operation: (Case 1)





In my judgment line 3 goes against the law of being unable to narrow then unbox (or something like that). However it is allowed ! What the.....

If that's allowed, Why the following is Not? [given lines 1 and 2] (Case 2):



Could somebody please guide me on understanding Why and how is case 1 allowed?

I'm clueless, so the more details the merrier,

Sincerely,
Jose
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Campana:





The code works because what it tries to do is as follows

1.As everyone knows c *= i; will yield c = c * i; operation
2.Then compiler will do a operation like this



this one is permitted by java

so that you are able to get the output in this statement

Whereas in the next case

the compiler will try to do operation like this one

Which is not permitted in java


This is the way i viewed your problem,If i am wrong let me clarify that one.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Balasubramanian Chandrasekaran:


Which is not permitted in java
You mean that is what the compiler would like to do, but it can't do it unless you actually write the (char) cast. Remember a compiler can automatically carry out a widening conversion eg char ----> int, but not a narrowing conversion like int ----> char unless you write a cast.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
You mean that is what the compiler would like to do, but it can't do it unless you actually write the (char) cast. Remember a compiler can automatically carry out a widening conversion eg char ----> int, but not a narrowing conversion like int ----> char unless you write a cast.


Actually here it is not converting from int----->char
It is conversion from Integer(Wrapper Class)---->char(Primitive Datatype) that is not possible directly
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day,

Originally posted by Balasubramanian Chandrasekaran:


The code works because what it tries to do is as follows

1.As everyone knows c *= i; will yield c = c * i; operation
2.Then compiler will do a operation like this



this one is permitted by java



Balasubramanian, How did you come by that knowledge ?

To me it is not obvious what the compiler does backstage when it come to multiplying.

And, by the way, What is the result of multiplying an Integer by another Integer?

-- Another Integer ?

Thanks for the replies !
Sincerely,
Jose
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Campana:
And, by the way, What is the result of multiplying an Integer by another Integer?

-- Another Integer ?

More likely an int.
Seeing as you use * for multiplication, and that is an operator not allowed for objects such as Integers, it will auto unbox both Integers to ints, then multiply, leading to an int result. Of course you can assign this to an Integer without a problem because of auto boxing.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Rob... Sorry, I have not been able to reply to your post until now,
That's excellent,
Thank you for the reply, You always prove to be large than life when it comes to teaching java!

The best to you friend,
Merry X-Mas.
Sincerely,
Jose
 
Bert Bates
author
Sheriff
Posts: 8954
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

as it's very unlikely you'll run across code like this, i'm going to slide this over to the intermediate forum...

ho, ho, ho
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Campana:
Thank you for the reply,   {to Rob}   You always prove to be large than life when it comes to teaching java!


Rob gets a lot of the book and compiler and Java language issues out of the fog and onto the Tallgrass Prairie , but what is sometimes missing in these conversations is that some of this is Computing in General, and it is simpler sometimes to just expand the code yourself than it is to rely on general-purpose libraries.

I did this last night while thinking about a posters question and came up with: Session Initiation Protocol (SIP) weighin in at several hundred lines of code.

I find that much simpler than trying to work in a linguistic that strictly enforces no unsigned data types. It would be of great utility for me right this red-hot moment if I could determine that a char could be cast to a short and do arithmetic with something that is natively numeric - the sign conversion issue is greatly different from the byte-char-byte converter that does MBCS conversions such as we see in the string class.

My suggestion is to code it fully and just be careful of carpal-tunnel.
[ December 25, 2007: Message edited by: Nicholas Jordan ]
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there !
It's good to read Mr. Bates reply to this thread, so relieved now...
Thanks for comforting me Mr. Bates. However, Does your reply applies to the SCJP exam as well? If your future reply happens to be affirmative, that'd be the Coolest ! (Not that I'm not taking measures in case I run into a question like that in the exam)
And for that matter I re-read some parts of your book and found in page 255, perhaps one of the many mentions that states that compound assignments do an implicit cast(How could I have forgotten that!). That brought back order to my thoughts, Because I was very confused at the time I asked that question.

And regarding to what Nicholas said, I do agree, and I always play with the compiler for a while before posting a question here; but the fact of the matter is that I want to understand how Java works, So much knowledge is concealed that can't be learned without asking; and thank god JavaRanch exists; If it didn't, then I'd probably have given up long ago...

Thanks Nicholas for the advice!
See you next time...
Best Regards,
Jose
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose, It is remarkably difficult to write for beginners. His remark does not really apply to an exam written by another author, but may have applied if he had written a paticular exam. It should apply to any exams in the book.

And regarding to what Nicholas said, I do agree, and I always play with the compiler for a while before posting a question here; but the fact of the matter is that I want to understand how Java works, So much knowledge is concealed that can't be learned without asking; and thank god JavaRanch exists; If it didn't, then I'd probably have given up long ago...


The only approach that I have found to be consistent is to completely redo any project several times. Even a simple Hello World. But what one needs to avoid and watch out for is rushing out new edits done early in the morning, these produce really really bad work, like when supermodels think really really hard.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ha ha ha ha ha,
Amen to that.
Thanks for the advice Nicholas, Keep up the fantastic work!

Sincerely,
Jose
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!