Reading From Keyboard and Assigning a Variable in Groovy
Next: Simple output Up: 1 Programming Nuts Previous: Comments Contents
Variables -- declaring and assigning values
Before discussing input and output, including Organisation.out.println(...) which you used in Hello.coffee, it will be helpful to encompass the topic of variables. You will have come beyond the idea of variables in mathematics, and the concept of a variable in programming is like.A variable is associated with space in the estimator's retentivity that tin can hold a value (oft a number). Each variable has a name, which you choose, and afterwards use to refer to it.
You lot must start by declaring the variable, this gives the variable a name and reserves some retention infinite to store whatever value the variable takes. It also tells the compiler what yous intend the variable to represent. This is known equally the variable's type. For example a variable which always takes integer values, can be declared every bit blazon int in Coffee. For instance the line of code
int i;
declares an integer variable (i.east. a variable of type int) which we accept chosen to call i. As an int, it can store any integer value between -2147483648 and 2147483647.
Coffee provides types to represent several kinds of number, due east.grand. integer and floating signal, non-numerical things like text, and other more abstract things. These are listed on folio
. Y'all can give a variable a longer name if you similar, and it is usually a adept idea to choose a word that explains what the variable is for. The convention is for variables to be named using lower instance letters, or if the proper noun consists of more one discussion, that a capital be used at the start of each give-and-take other than the outset. You may also apply numbers or an underscore _ in your variable names, just not at the beginning of the proper name. Examples of some well chosen and valid variable names might be total, maxValue, answer1.
Practice i.ii
(one mark)
Now try the elementary program below. Equally it contains
public class VarTryyou must type information technology in to a file called `VarTry.java'.
Compile and run VarTry in the same way that y'all did with the Hello program to check it prints out the number nine. Print the output and put information technology in your lab book. Read the explanation that follows and try to understand how it works.
/** * Name: * Date: * Exercise: 1.ii * Purpose: To demonstrate the declaration and simple uses of variables. */ public class VarTry { public static void main(Cord[] argv) { int i; // declares integer variable named i i = ix; // gives i the value 9 System.out.println(i); // print the value of i to the screen } } You volition see hither how each line of code must finish with a semicolon, ; , (if you demand to write a line of code that is longer than the width of the window you tin continue it on the adjacent line by omitting the semicolon until the terminate of the statement). The line i = 9 gives the value 9 to variable i. This is known as assigning a value to a variable -- i is assigned the value ix. It means that the infinite in the figurer's retentiveness associated with i at present holds this value. The showtime time a variable is assigned a value, it is said to exist initialised. The = symbol is known as the assignment operator.
Information technology is besides possible to declare a variable and assign it a value in the same line, so instead of int i and then i = 9 you can write int i = 9 all in one get. If you lot accept more than 1 variable of the same type yous tin also declare them together e.k.
int i, j; // or int i=i, j, grand=2;which can exist a useful style of saving space. Where necessary yous should add together comments explaining the pregnant of the variables, both so it is clear to you lot if yous come to await at your program at a later date, and to those mark your programs.
You volition of course want to use many quantities which are non integers, and at that place are several different variable types which encompass these possibilities. For real numbers there are two possibilities -- float and double. As double uses twice equally much retention every bit float to store values, it is more authentic. For real numbers y'all will probably more often than not want to employ double. All the variable types are described below.
- byte Integer variable allocated only eight $.254 (i.e 1 byte) of retention. May store values from -128 to 127.
- brusk Short integer, allocated but 16 bits of retention. May take values from -32768 to 32767.
- int Most commonly used form of integer variable, 32 $.25. May concur whatever value in the range -2147483648 to 2147483647.
- long Used if working with particularly big integers, 64 bits. Up to nineteen digits and a sign.
- bladder Floating bespeak real number, which must be written in the format 3.45, or 3.0e-five -- that is they must include a decimal identify and may include the letter due east. What follows e is the ability of ten, so the two examples mean iii.45 and 3.0 x 10-5 respectively. A float variable is 32 bits and holds a value between
1.four x 10-45 and
three.4 10 1038, to eight significant figures. - double A 16 significant figure, floating indicate, existent number, 64 $.25. Values are written equally for float, east.g. 5.8e59, and may accept value between
iv.9 ten 10-324 and
i.viii x 10308. - boolean May accept one of only two possible values, true and imitation. This is a logical truth variable (1 bit).
- char A single character (this may be an upper or lower case letter, number or other keyboard symbols like :, # or ! for example.)
Y'all can also create a String of characters. A String is like a word or a line of text and can include spaces, upper and lower instance letters, numbers and other keyboard symbols. Strings are created in a like way to variables, and the double quote symbol " is used to mark the start and end of the text. You may for example write
String proper name = "Bob1";This creates a String called name which stores `Bob1'.
Y'all should exist aware that there are some words which you may not utilise as names for variables (or methods or classes for that matter) every bit they have a special meaning in Java. These are:
abstract, boolean, suspension, byte, byvalue, instance, cast, catch, char, class, const, continue, default, do, double, else, extends, false, final, finally, float, for, time to come, generic, goto, if, implements, import, inner, instanceof, int, interface, long, native, new, nothing, operator, outer, package, private, protected, public, rest, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, truthful, effort, var, void, volatile, while, widefp.
Exercise one.3
(half-dozen marks)
With reference to the section above, say what type of variable would be suitable for the post-obit, and suggest a name for it too.
It may help to think of possible values and the range which would exist valid before y'all choose a type.
Example: the temperature of a room in C. Room temperature is likely to vary between fifteen and 35 C but information technology can take any value inbetween including not-integers, e.g 20.37 C, so the type must exist float or double depending on the accuracy required. A suitable variable proper name, following the conventions described in a higher place, would be roomTemp .
- (i)
- the number of pages in a book;
- (2)
- the number of atoms in a book;
- (three)
- the length of a side of a triangle in metres;
- (iv)
- your name;
- (v)
- whether or not a nucleus has decayed;
- (vi)
- the probability that information technology could have decayed.
Next: Simple output Up: ane Programming Basics Previous: Comments Contents RHUL Dept. of Physics
Source: https://www.pp.rhul.ac.uk/~george/PH2150/html/node9.html
0 Response to "Reading From Keyboard and Assigning a Variable in Groovy"
ارسال یک نظر