FLoat why won't you work???
Author |
Message |
Nathan4102
|
Posted: Sat Aug 24, 2013 9:34 pm Post subject: FLoat why won't you work??? |
|
|
Just playing around with Java, I finally decided I should learn it. Quick question, Why does code A compile, while code B doesn't? Float is a 32 bit decimal, while double is a 64 bit decimal, no? Why can't I use a float here?
Code A:
Java: | public class Variables {
public static void main (String[] args ) {
double pie = 3. 14;
System. out. println("Pie is approximately equal to "+pie );
}
} |
Code B:
Java: | public class Variables {
public static void main (String[] args ) {
float pie = 3. 14;
System. out. println("Pie is approximately equal to "+pie );
}
} |
Edit: Screwed up the title... :/ |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Sat Aug 24, 2013 9:53 pm Post subject: RE:FLoat why won\'t you work??? |
|
|
When you specify a float literal, it has to end in the letter 'f'. This will compile:
Java: |
public class Variables {
public static void main(String[] args) {
float pie = 3.14f;
System.out.println("Pie is approximately equal to "+pie);
}
}
|
Your environment should be telling you something about "type mismatch" or "double cannot be converted to float" or "narrowing". In the future, try including the exact error message when asking for help. |
|
|
|
|
![](images/spacer.gif) |
Nathan4102
|
Posted: Sat Aug 24, 2013 9:56 pm Post subject: RE:FLoat why won\'t you work??? |
|
|
Hmm... Weird.
Thanks for the help, and I will do in the future |
|
|
|
|
![](images/spacer.gif) |
Nathan4102
|
Posted: Sun Sep 01, 2013 1:01 pm Post subject: RE:FLoat why won\'t you work??? |
|
|
Another quick question, I won't bother posting a whole thread just for this. Im trying to use command line arguments, but for some reason, I can't get true when comparing them. Anyone know why?
Java: | public class Calculator {
public static void main (String args []){
String argument = args [0];
if (argument == "One") {
System. out. println("Two");
} else {
System. out. println("Huh?");
}
}
} |
Edit:
Fixed it. For some reason, == wouldn't work with strings, but .equals() did. |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Sun Sep 01, 2013 4:18 pm Post subject: RE:FLoat why won\'t you work??? |
|
|
The reason is actually really important.
In Java (but not always other languages), the == operator (for objects, but not primitives) is the "identity" operator. If a == b then a is exactly the same object as b. The equals() method compares the contents of a and b for equality.
The default implementation of equals, in Object.equals(Object), compares by the identity operator. The String class overrides that implementation with an implementation that compares by lexical (string) equality. Other objects may have their own overrides. |
|
|
|
|
![](images/spacer.gif) |
Nathan4102
|
Posted: Mon Sep 02, 2013 11:18 am Post subject: RE:FLoat why won\'t you work??? |
|
|
Thats weird, I've always used =/== to compare strings. Thanks for the info! |
|
|
|
|
![](images/spacer.gif) |
|
|