Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Primitive Data Type promotion.

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
           I am a Java greenhorn and this is my first post. I am using OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide book by  Jeanne Boyarsky and Scott Selikoff. On page 139 in Chapter 2 (in Numeric Promotion rules) the last paragraph seems confusing to me.  In the example float y = 13; but since it is not postfixed with f it will be treated as a double (as explained in the preceding paragraph). But the text says "... The promoted x value will then be automatically promoted to a float so that it can then be multiplied with y..."   As per my understanding, x will be promoted to double & not float. Please help.
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prabir Sarkar wrote:   As per my understanding, x will be promoted to double & not float. Please help.



First, welcome to the ranch.

Anyway, why would the promotion be to a double type?  It is an int literal, that is assigned to a float variable -- so, the promotion should be from int to float.  

Henry
 
Prabir Sarkar
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. I somehow overlooked the fact that 13 is actually an int literal which is getting automatically updated to float as it is being assigned to float.
 
Prabir Sarkar
Greenhorn
Posts: 22
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The statement float x = 16;

is interpreted as an integer literal '16' being assigned to a float datatype variable x. This happens automatically since Java by default allows automatic datatype promotion but Java doesn't allow implicit narrowing primitive conversion.

Now, my question is :-  short x = 16;  Isn't this a  Narrowing Primitive Conversion where an integer literal '16' is being assigned to a short datatype variable x. Java seems to be okay with this implicit narrowing primitive conversion!! Please explain.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prabir Sarkar wrote:Now, my question is :-  short x = 16;  Isn't this a  Narrowing Primitive Conversion where an integer literal '16' is being assigned to a short datatype variable x. Java seems to be okay with this implicit narrowing primitive conversion!! Please explain.


You are correct! But because the compiler knows 16 is within the range of short and the compiler is a lovely friend (after preparing for the OCAJP certification exam probably your new BFF ), the compiler is happy with it and an implicit conversion is made.

What do you think will happen if you try to compile these code snippets (seperately)? And even more important: can you explain why?
 
Prabir Sarkar
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As I was expecting the code snippet two didn't compile. Error - Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: possible lossy conversion from int to short. But totally surprised with snippet. Can't figure out how it compiled without errors!
 
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

Prabir Sarkar wrote:As I was expecting the code snippet two didn't compile. Error - Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: possible lossy conversion from int to short.


If you try to compile snippet2, you'll get indeed a compiler error Which IDE/tool are you using because given the error it seems to be a runtime exception (which it definitely is not, it's a definite compiler error)?

Prabir Sarkar wrote:But totally surprised with snippet. Can't figure out how it compiled without errors!


The sole purpose of snippet1 was to surprise you, so mission accomplished The implicit conversion I mentioned actually applies to compile-time constants of type int (and an int literal is by definition a compile-time constant of type int). This topic discusses compile-time constants in more detail.
 
Prabir Sarkar
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using NetBeansIDE 8.0.2. Is it better to use Eclipse instead? Thanks for the link with more conversion.
 
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

Prabir Sarkar wrote:I am using NetBeansIDE 8.0.2. Is it better to use Eclipse instead? Thanks for the link with more conversion.


For me using an IDE during the preparation for the certification exam is definitely a don't. Why? Very easy: on the actual exam you also don't have an IDE (and compiler) to help you verify and run code. You are all on your own. So my first advice would definitely be: get rid of your IDE, only use your favourite text editor, javac and java. Other opinions about using an IDE while preparing for the OCA exam can be found here.
 
Prabir Sarkar
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. I give up my IDE and return to good old notepad.
 
Enthuware Software Support
Posts: 4453
41
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prabir Sarkar wrote:I am using NetBeansIDE 8.0.2. Is it better to use Eclipse instead? Thanks for the link with more conversion.


Using an IDE for certification preparation is actually dangerous. IDEs add on a lot of stuff in terms of hints, compilation errors/warnings, and error messages that may be different from the official javac/java. Hell, they may also execute a class that has a compilation error! So don't rely on them. Always try to compile and execute your test programs from the command line using standard oracle's javac and java tools.
 
Prabir Sarkar
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the info! I will mend my ways.
 
    Bookmark Topic Watch Topic
  • New Topic