This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes simple question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple question" Watch "simple question" New topic
Author

simple question

Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
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!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If I were you, I'd write a few short test programs and see what happens. So, what happens?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
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! =)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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);


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
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

Joined: Dec 10, 2001
Posts: 7023
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.)


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.
 
Don't get me started about those stupid light bulbs.
 
subject: simple question
 
Similar Threads
wrappers
Primitive Casting Exercise
Casting hierarchy
float or double
Doubt in Array