Showing posts with label int. Show all posts
Showing posts with label int. Show all posts
Monday, February 25, 2013
What is a variable?
Variables are used to store values in a program. A value might be a name, a number, an address, and so on. These values can then be displayed or processed to create new information.
Think about a calculator. Each time you press a button, the calculator remembers what you have pressed. When you press the equals button, the calculator forms a mathematical equation using your series of key presses.
There are many types of values. Some values are made up of letters and numbers (such as an address) and some values are simply numbers. Each type of data can be stored in a program using a variable.
For instance, in a Java program, we could do the following:
String userName = “C2012000”;
String password = “monkey5000”;
String address = “30 Happy Place, Uptown”;
int numberOfLogins = 12;
The words String and int are types of Java variables - or data types.
Now we have 4 stored values that can be used again later in the program. Perhaps we need to print these values to the screen or perhaps we need to know where a letter must be mailed to. Perhaps we need to take an average of how many times a user has logged in over a particular time frame.
Alphanumeric & Numeric data.
Alphanumeric data is any collection of keyboard characters. It may contain letters, numbers & other special characters. Values such as these are stored in an variable that can hold alphanumeric values. This type of variable is called String.
String whatTheDogSaid = “Right on partner number 2!”;
A String may contain numbers, such as a house number in an address. But if we need to store a number so that we can do some kind of maths on that number, we need to use a variable type that can store numbers as numbers.
Numeric data are numbers. Generally speaking, data is considered numeric where it is being used for maths purposes - such as counting, adding, subtracting, and so on. Some numbers are whole numbers, such as the number of students on a bus. Some numbers contain a floating point, such as currency values, or body weight. For this reason, there is more than 1 type of variable that can hold numeric data. For instance, an int variable can store a whole number and a Double variable can store a floating point number.
int numOfStudentsOnBus = 22;
double dogWeight = 12.2;
The number of students on a bus is going to be stored as a whole number, so we store it in a variable that can store whole numbers, such as int.
A number representing weight contains a floating point so we store this number in a variable type that can store floating point numbers such as double.
There are many data types in the Java language!
Here's a cat:
Labels:
cat,
Data Types,
floating-point,
int,
string,
Variables,
whole number
Tuesday, January 8, 2013
Incrementing a number ++
Increment - increase a value.
The increment operator is a double plus ++
The increment operator increases the value to it's left by 1.
Example use with int:
int one = 10;
one ++;
In the first line, an int variable called one is declared and assigned a value of 10.
In the second line, the increment operator is applied to the variable called one to increment it's value.
The result is that the variable called one, now has a value of 11.
Example use with double:
double one = 12.55;
one ++;
In the first line, a double variable called one is declared and assigned a value of 12.55.
In the second line, the increment operator is applied to the variable called one to increment it's value.
The result is that the variable called one, now has a value of 13.55.
Note that where the increment operator is used with the double data type, the increment value is 1 significant number (the number on the left of the floating point is incremented).
See also Decrementing a number
Decrementing a number --
The decrement operator is a double minus --
The decrement operator decreases the value to it's left by 1.
Example use with int:
int one = 10;
one --;
In the first line, an int variable called one is declared and assigned a value of 10.
In the second line, the decrement operator is applied to the variable called one to decrement it's value.
The result is that the variable called one, now has a value of 9.
Example use with double:
double one = 12.55;
one --;
In the first line, a double variable called one is declared and assigned a value of 12.55.
In the second line, the decrement operator is applied to the variable called one to decrement it's value.
The result is that the variable called one, now has a value of 11.55.
Note that where the decrement operator is used with the double data type, the decrement value is 1 significant number (the number on the left of the floating point is decremented).
See also Incrementing a number
The decrement operator decreases the value to it's left by 1.
Example use with int:
int one = 10;
one --;
In the first line, an int variable called one is declared and assigned a value of 10.
In the second line, the decrement operator is applied to the variable called one to decrement it's value.
The result is that the variable called one, now has a value of 9.
Example use with double:
double one = 12.55;
one --;
In the first line, a double variable called one is declared and assigned a value of 12.55.
In the second line, the decrement operator is applied to the variable called one to decrement it's value.
The result is that the variable called one, now has a value of 11.55.
Note that where the decrement operator is used with the double data type, the decrement value is 1 significant number (the number on the left of the floating point is decremented).
See also Incrementing a number
Wednesday, September 5, 2012
Comparing int using the equality operator
Two int values may be compared for equality by using the equals = = operator.
The equals operator returns True or False (boolean).
int one = "10";
int two = "10";
if (one == two) {
System.out.println("The int values are equal.");
}
else {
System.out.println("The int values are NOT equal.");
}
In this example, the equals operator would return true, causing the statement "The int values are equal" to be printed to screen.
The equals operator is used to test equality on all primitive types such as int.
If you need to compare two strings, read here....
Comparing String using the .equals method
Two String objects may be compared for equality by using the .equals function.
The equals method returns True or False (boolean).
String one = "Apples";
String two = "Oranges";
if (one.equals(two)) {
System.out.println("The strings match.");
}
else {
System.out.println("The strings DO NOT match.");
}
In this example, the equals method would return false, causing the statement "The strings DO NOT match" to be printed to screen.
The .equals function is available in all objects and Strings.
If you need to compare two primitive types such as int, read here....
Saturday, June 2, 2012
Switch
CODE DOWNLOAD: UsingSwitch.zip
A Switch can be used to evaluate a number of numeric types such as byte, short, char & int.
Let's say you want to check the value of a single variable and based on this value, one of many things may happen.
In the example below, the user enters a number. This number is then checked by the switch. The first case to match, executes a statement - in this case, printing a message to let the user know what the entered value was.
Of course, we could use a number of if then else statements to achieve the same result, however, as we are only checking the value of a single variable, the switch is a more elegant and readable solution (subjective).
Here's a cat:
"The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/y2TVYE )
A Switch can be used to evaluate a number of numeric types such as byte, short, char & int.
Let's say you want to check the value of a single variable and based on this value, one of many things may happen.
In the example below, the user enters a number. This number is then checked by the switch. The first case to match, executes a statement - in this case, printing a message to let the user know what the entered value was.
If the user entered a value not listed in the cases, the default section executes. It is good practice to always include a default case in your switch.
Notice that the final statement in each case is break. This causes the switch to stop checking down the list.
We can also execute a common statement for multiple cases, as in the example below:
Here's a cat:
"The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/y2TVYE )
int data type
An int is a numeric data type
An int is a whole number between -2,147,483,648 and 2,147,484,647 (a pretty big number). The number cannot contain a floating point (no decimal).
Where an int has not been assigned a value, by default it's value is 0.
The format for declaring an int variable is:
Type identifier;
Where the Type is int and the identifier is maxDamage:
int maxDamage;
...and where a value of 100 has been assigned:
int maxDamage = 100;
You can declare an int in one statement and assign it a value in another statement:
int maxDamage;
// other statements...
maxDamage = 100;
See also:
"Java For Complete Beginners - Variables: the int type" ( http://bit.ly/ZA1aE8 )
Sunday, April 29, 2012
Convert String to int
You may convert a String to an int, provided the String contains number only.
The following example will work in cases where an int number is contained in the string called thisString:
String thisString = "100";
int x;
x = Integer.parseInt(thisString);
The following example will work in cases where an int number is contained in the string called thisString:
String thisString = "100";
int x;
x = Integer.parseInt(thisString);
Tuesday, March 27, 2012
A handful of data types & variables
String
Any collection of keyboard characters.
Boolean
A true or false value.
Char
A single keyboard character.
int
A whole number between -2,147,483,648 and 2,147,484,647
Any collection of keyboard characters.
Boolean
A true or false value.
Char
A single keyboard character.
int
A whole number between -2,147,483,648 and 2,147,484,647
Sunday, March 18, 2012
Default value of primitive data types
Labels:
boolean,
byte,
char,
Data Types,
Default values,
double,
float,
int,
long,
Primitive data types,
short,
string
Tuesday, January 31, 2012
Subscribe to:
Posts (Atom)





