This site requires JavaScript, please enable it in your browser!
Greenfoot back
Peach
Peach wrote ...

2012/12/15

declaring a variable

Peach Peach

2012/12/15

#
how do you declare a variable?
danpost danpost

2012/12/15

#
Please refer to Learning the Java Language in the Java tutorials.
Peach Peach

2012/12/18

#
I've read it but I still don't understand how to do it
danpost danpost

2012/12/18

#
This page in the tutorial, What is a class?, tells you of three fields (whose lines are declared near the beginning of the class code; each one starting with the keyword 'int') that are used to keep track of certain states of the bicycles created in the class.
Entity1037 Entity1037

2012/12/18

#
Declaring a variable is the most simple thing you can do. So here's how to do it: Import Greenfoot.*; Public class extends Actor { = ; } So the code to declare a variable does not go in the void act, it can only go in the public class. So for an integer (number) it would be: int i = 5; just an example. A string would be: string whatever = "Hello world!"; A double is probably the most important because it can carry a decimal value (unlike the integer). double Pi = 3.14; These three variables are the most important, but there are others you can use, although I don't really find any use for them. I hope this helps.
danpost danpost

2012/12/18

#
Entity1037 wrote...
These three variables are the most important, but there are others you can use, although I don't really find any use for them. I hope this helps.
in reference to 'int', 'String' and 'double'. The 'boolean' type is also very important. Actually, I would say that it is more important than 'double'. All conditions are based on 'boolean' values. Most anything done with 'double's can be accomplished by using 'int's. Also, 'String' is not considered a primary type. 'String' objects are actually 'char' arrays. They do have a special place in the language and can pretty much be treated as a primary type because of it. So, I would say the three most important types would be 'boolean', 'int' and 'char'/'String' (putting those two in the same category).
Entity1037 Entity1037

2012/12/18

#
Thanks for the correction. I should probably start using boolean data types instead of using an integer for 0's and 1's. I'm not sure if there's a lag difference if you do this or not, but its just better to not use something that's unnecessary.
Zamoht Zamoht

2012/12/18

#
'int's uses 32 bits of space, where i guess 'boolean's only takes 1 bit of space. This is very small amounts of memory, but personally I think 'boolean's make the code more readable aswell.
SPower SPower

2012/12/18

#
Zamoht wrote...
'int's uses 32 bits of space, where i guess 'boolean's only takes 1 bit of space.
Sorry, that's not true. A boolean still takes up 32 bits. Brief explanation: otherwise the CPU has to know how much memory something takes up, but it's way simpler to just have default value for each one! (And still, you would only save 31 bits.... not that much considering the average RAM size is 2 to 4 gb...)
Zamoht Zamoht

2012/12/18

#
Okay then declare booleans to make your code more readable. Still booleans take 8bits / 1byte (looked it up this time), and yes saving 3 bytes isn't much.
SPower SPower

2012/12/18

#
Zamoht wrote...
Okay then declare booleans to make your code more readable. Still booleans take 8bits / 1byte (looked it up this time), and yes saving 3 bytes isn't much.
I think you're wrong again: it takes up 32 bits (4 bytes) and 32 - 1 = 31 bits more than your first theory ;)
danpost danpost

2012/12/18

#
I found the following in the Java tutorials:
  • boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
  • SPower SPower

    2012/12/18

    #
    Owww, I guess I should search google some more..
    Zamoht Zamoht

    2012/12/18

    #
    Found someone who said that it represents one bit of information, but it uses 1 byte of memory since it's easier for the proccesor to handle or something like that, but he might be wrong.
    You need to login to post a reply.