![]() a public variable called checkCollision with initial value true.a public variable called score with initial value 0.a private variable called isEmpty with initial value false.a public variable called numberSquare with initial value 4.Write code to declare and initialise the following variables: storing the value 0.4 in the new variable.Īctivity 5-1 Declaring and Initialising variables.declaring a public variable of type double and name speedFactor and.Just remember what this is actually doing is two steps: We can actually shortcut this process a little by putting this all on one line: To declare a public double called “speedFactor” we would write:Īnd to assign the value 0.4 to it, we could write: Java provides several other types, which we will meet during this unit, but the most common for these kinds of numbers is the type double. You may have been wondering how we represent decimal numbers such as 0.5 or -123.8037. For int this is usually 0, and boolean it is false, but you can’t count on this being the case, so please make sure you initialise your variables. If you don’t initialise the variable, what is stored in it? By the Java documentation, it is “a reasonable default”. To do that, we need to assign a value to the variable – called initialising the variable. Note that the code above doesn’t actually store anything in the variable (memory location). The value in this variable will be referred to by this name in our code. This variable will store a value that is an integer. ![]() This variable will only be used within the class code. If this is too complex at the moment, that’s fine – you just need to know that it is legal to leave off the modifier – although we don’t recommend it at the moment. In this case this variable would only be able to be accessed by this class’s code and code in any classes which are subclasses of it. In fact, in some cases we can leave off the ‘public’ or ‘private’ – if we do this, then it will default to another modifier “protected”. We would expect that this “declaration” was going to be complicated as it does all these three things, however it’s quite simple.įor example, a new variable of type “int” with name userAge that we wish to use only inside the class could be declared by:Ī boolean value that we would like other classes’ code to be able to use, called “isFinished’ would be declared as:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |