• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Conversion question

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, while practiticing for the test I came across this question (found on Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 5342-5345). Enthuware. Kindle Edition. ):

Which of the following declarations are valid? Select 3 options

A.
B.
C.
D.
E.



I answered C,E and I was torn between B and D. Eventually I went for D as D felt smaller than B. I got it right, because apparently B is a double but, I wonder, are these kind of questions likely to be appearing on the exam? I thought that conversion wasn't a topic. If it is, or if it could appear in the exam, is there a way to quickly convert numbers?
thanks

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a computer with a JDK installed, then you can very quickly find the answer to this question yourself. Just write a very small program and try to compile it, and see which of these compiles and which ones give you an error.

Whether an assignment to a float is valid has nothing to do with if the value of the number is big or small. It's about the types.

The Java Language Specification specifies the rules about what types of values can be assigned to a different type of variable without casting or explicit conversion.

The literal values on the right side of the = in answers A and B are of type double. Converting these values to a float means you are doing a narrowing primitive conversion, which requires a cast. The cast is required because such a conversion throws away part of the value (bits are discarded), so, with a cast, you must confirm to the compiler that that's OK.

The literal values on the right side of the = in answers C, D and E are of type int. Converting these values to a float means that you are doing a widening primitive conversion, which does not require a cast.

Whether these questions will be on the exam, is something I don't know for sure.
 
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I wonder, are these kind of questions likely to be appearing on the exam?  



No. Exam doesn't ask you to convert between one number system value to another numbering system value . Hexadecimal , Octal, binary, Decimal number system values are considered  as integer values in Java. Integer values can be automatically promoted to long, float,double types.  Exam doesn't tests about data types range.  Even If question contains those number system values , Keep in mind they are integer values and can be automatically promoted to long, float,double.

Hope it helps !
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, but if all hex are ints why do they say that 43e1 is  a double? If it's an int then an int can fit in a float so there is no problem
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:Thanks guys, but if all hex are ints why do they say that 43e1 is  a double? If it's an int then an int can fit in a float so there is no problem



43e1 is *not* a hex literal. Hexadecimal literals starts with "0x".

Henry
 
Narayana Bojja
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
43e1 is decimal number system value and it's value is 430.0 which is double in Java.

Hope it helps !
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, sorry it's that I've never come across that notation before, is there a way to convert that decimal 43e1 into a more readable decimal? If so what's the general rule?
thanks
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:Right, sorry it's that I've never come across that notation before, is there a way to convert that decimal 43e1 into a more readable decimal? If so what's the general rule?



It's scientific notation ... https://en.wikipedia.org/wiki/Scientific_notation

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
43e1 Stands for a double because  "e" is a scientific notation that represent "times ten raised to the power of" (which would be written as "× 10n").
in other words, 43e1 = 43x10 and is followed by the value of the exponent 1.
This is a double because the notation is used to represent either a very big or a very small number this means in java it would be a double.
 
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Michael Haile wrote:. . . because the notation is used to represent either a very big or a very small number . . . it would be a double.

That is logical, but not correct. It is a double because all floating‑point literals without a final f are doubles. 43e1 is 430.0, which is in the range for a float. The gory details are to be found in the Java® Language Specification (=JLS).
If I remember correctly, a “narrow” integer primitive can be assigned to from a compile‑time constant expression in the appropriate range for its target with an implicit narrowing conversion, but I don't think that applies to floating‑point numbers, nor int and long declarations.Both the right operands for the = operators are int literals in the range of their targets for assignment.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic