• 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

Return Types

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

How do we know which datatypes will be implicitly converted to other and which require explicit casting. For example:- char can be converted to int. Is there any rules to check which primitive types are convertible?
 
Ranch Hand
Posts: 472
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 - this about widening conversion

byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double


you can`t asign byte to char, and short to char because byte and short can be negative and char is positive.

also you can assign final variable from int and smaller to smaller variable (if value fit):

pay note that last rule will work to return value and assign to variable but this last rule about final variable will not work with method invocation

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.3

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion



http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.5 - this about casting, also pay note about casting wrappers.
 
Jyoti Kaushal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergej,

I checked the oracle doc you asked to refer. There are many details linked to this and few more topics too in that. Do we need to go through that doc too for the OCA/OCP exam?

Thanks,
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sergej's link is to the specification which is extremely detailed and thorough. That's why people have written books and study guides. To show what you actually have to know.

You do need to know what types can be cast to each other explicitly and implicitly.
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no requirement go through JLS, but i will write some tips and advantages of do this:
1. You may write concept what not compile - and this will help you better understand "not compile question"
2. When you try what is write in jls - you definitely write code because you try understand what will not compile, or same concepts.
3. You will see more code example for some topic. (some of this not come in my head when i read study guide)
4. When you read jls you will better (much better understand unreachable code)
5. You will think little different, many question born (about this thing will work? and this ? and this?)

Some disadvantages:
1. You will spend more time.
2. It hard to read some time and understand.

Some tips:
0. My first read was after guide book. (and i think this is right - because you like a new tree you need water near surface before you take root deeper, there is no need to break your root on (some time) stone (some easy concepts may be quite hard to read from jls if you not understand little of it )
1. I sent to my mail whole sentence that contain: compilation error and same (understand what this mean (when can)) and than conspectus it.
1.1 I try code concept from it.
2. I rewrite as i understand some concepts from jls.
3. I ask question that i can`t understand to forum (you may find it) many of it was not for OCA.
4. Don`t read it many time - make conspectus, this book is not substitute for study guide. (for OCA i read it once)

For my opinion this is very very good book - it not substitute study guide, and i think it must be learn (after study guide) because it make your understanding better - that`s why i read it second time for preparing to 804 exam. (And after all you must be good programmer not only pass exam)

For casting (and others topic you study) try find template (and write short rules)
for example
Table 5.2. Casting conversions to reference types - you can cast primitive to its wrapper or upward to Object. (example: boolean only to Boolean, Number or Object) (about Number i found when i did mock test)
 
Jyoti Kaushal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am thinking of taking OCA exam by end of this month or start of next month that's why little worried whether I will be able to cover all the things in this time span or not. Feeling little relaxed with Jennea's comment that details are not required for OCA. @Sergej your point also seems quite emphasizing to me. I will definitely get some time to go through them too.

Thanks guys :-)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic