Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 FLoat why won't you work???
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nathan4102




PostPosted: 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... :/
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: 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.
Nathan4102




PostPosted: 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
Nathan4102




PostPosted: 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?");
                }
        }
}


Posted Image, might have been reduced in size. Click Image to view fullscreen.

Edit:

Fixed it. For some reason, == wouldn't work with strings, but .equals() did.
DemonWasp




PostPosted: 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.
Nathan4102




PostPosted: 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!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: