Showing posts with label Primitive data types. Show all posts
Showing posts with label Primitive data types. Show all posts

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....

Saturday, June 2, 2012

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, March 18, 2012