Variables

1 comments - Post a comment

An object stores its state in variables.




Definition: A variable is an item of data named by an identifier.




You must explicitly provide a name and a type for each variable you want to use in your program. The variable's name must be a legal identifier — an unlimited-length sequence of Unicode characters that begins with a letter. You use the variable name to refer to the data that the variable contains. The variable's type determines what values it can hold and what operations can be performed on it. To give a variable a type and a name, you write a variable declaration, which generally looks like this:

type name

In addition to the name and type that you explicitly give a variable, a variable has scope. The section of code where the variable's simple name can be used is the variable's scope. The variable's scope is determined implicitly by the location of the variable declaration, that is, where the declaration appears in relation to other code elements. You'll learn more about scope in the section Scope.

/*** Java Program For Displaying Byte values of various Datatypes */
public class MaxVariables
{
public static void main(String args[])
{

//For integers
byte largestByte = Byte.MAX_VALUE;
short largestShort = Short.MAX_VALUE;
int largestInteger = Integer.MAX_VALUE;
long largestLong = Long.MAX_VALUE;

//For real numbers
float largestFloat = Float.MAX_VALUE;
double largestDouble = Double.MAX_VALUE;

//For other primitive types
char aChar = 'S';
boolean aBoolean = true;

//Output
System.out.println("The largest byte value is "
+ largestByte);
System.out.println("The largest short value is "
+ largestShort);
System.out.println("The largest integer value is "
+ largestInteger);
System.out.println("The largest long value is "
+ largestLong);

System.out.println("The largest float value is "
+ largestFloat);
System.out.println("The largest double value is "
+ largestDouble);

if (Character.isUpperCase(aChar)) {
System.out.println("The character " + aChar
+ " is upper case.");
} else {
System.out.println("The character " + aChar
+ " is lower case.");
}
System.out.println("The value of aBoolean is "
+ aBoolean);
}
}

/****** OUTPUT ******
The largest byte value is 127
The largest short value is 32767
The largest integer value is 2147483647
The largest long value is 9223372036854775807
The largest float value is 3.4028235E38
The largest double value is 1.7976931348623157E308
The character S is upper case.
The value of aBoolean is true */

 
This Post has 1 Comment Add your own!
Anonymous - March 14, 2010 at 3:00 AM

I read about it some days ago in another blog and the main things that you mention here are very similar

Post a Comment