• 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

simple question

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got some question when reading the java book:
1) I just wonder what the a valid declaration of float: can we do
float a = 1.0, float a = 1.01f, float a = 3.04d,
or float a = 0x0333
2)

what will happen?
3
is that valid to declare something like:
a) char a = '\u10100'
b) char a = '\uaaaa'
thanks!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I were you, I'd write a few short test programs and see what happens. So, what happens?
 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)
only float a = 1.01f has on compile error?
2)
i think there won't be error coz i have assign b value to a? although it doesn't make sense
3)
actually i am confused about the uni code.
thanks dirk! =)
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with (2) is that the if statement requires a boolean. You are saying if(2) which doesn;t work in Java.
This will print true... see if you can tell why?
boolean a = false;
boolean b = true;
if (a=b) System.out.println(a);
 
Jackie Wang
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder why
float a = 1.0, float a = 3.04d,
or float a = 0x0333
doesn't work
i got what u mean for (2) now.
thanks tom!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, floating point literals (numbers like 1.0, 2.4, 0.002) are by default considered to be doubles. You cannot implicitly downcast a double to a float. You must explicitly declare that you really want only float data type precision.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder why
float a = 1.0, float a = 3.04d,
or float a = 0x0333
doesn't work

The last one of those does work. Well, if you put it on a separate line ending in a semicolon. 0x0333 is an integer literal, and the compiler's perfectly happy to implicitly cast an int to a float, with no explicit cast from you.
For the other two:
You must explicitly declare that you really want only float data type precision.
Adding to Dirk's point - you can do this by either casting, or using an "F" or "f" to create a float literal. E.g.
float a = 1.0F;
or
float a = (float) 1.0;
(Personally I prefer the former, but either works fine.)
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the Unicode example:
char a = '\u10100';
If you read about the \u notation, you see it always uses exactly four hexadecimal digits after the \u. So in \u10100, only \u1010 is considered an escape sequence, and it's equivalent to a single letter. This letter turns out to be part of the Myanmar alphabet - let's call it letter "X" since most of us don't have the correct fonts installed to display it anyway. Anyway in \u10100 the final 0 is still left over, not part of the Unicode escape. And the whole line of code is equivalent to
char a = 'X0';
The problem now is that you have two characters - X and 0 - inside the single quotes of a character literal. This makes no sense - a character literal can have only one character in it. So you get the message "unclosed character literal" because the sompiler expected 'X to be followed with a closing ' rather than 0.
 
reply
    Bookmark Topic Watch Topic
  • New Topic