Variables are named memory locations that your program can use to store values. Line 5 declares our variable “strCountry” as follows.
“String” is the data type of the variable. That means this variable can hold texts, numbers or set of different characters.
“strCountry” is the name of the variable and known as variable identifier.
Other than strings, you may want to declare variables that can hold only numbers, but not characters; or variables that can hold images. Java provides eight primitive variable types and three reference variable data types. (Primitive variable types are the building blocks of Reference data types.)
The primitive types (also known as bulletin data types) comprise of all the numeric types along with a boolean type. The numeric types are four integral types- byte
, short
, int
, long
, andchar
, and two floating-point types- float
and double
.
Primitive Data Type Usage Capacity Default Value Maximum Value able to hold Minimum Value able to hold byte Sign integer 1 byte 0 127 -128 char Single character 1 byte u\0000 - - short Sing integer 2 bytes 0 32, 767 -32,768 boolean True or false 2 bytes false - - int Sign integer 4 bytes 0 232-1 -232 float Floating point values 4 bytes 0.0f 3.4*1038-1 -3.4*1038 long Sing integer 8 bytes 0L 264-1 -264 double Floating point values 8 bytes 0.0 1.7*10308-1 -1.7*1-308
The variables of a reference type hold thereferences to the actual objects.
Reference Data Type Usage Capacity Default Value Maximum Minimum Array Object
Variable Naming Rules
- Variable name must be start with a letter, doller sign or underscore.
- No embedded space characters allowed within variable name.
- Cannot use keywords as variable names.
You can use access modifies like “private” with variable declaration. Default access modifier in “public” that means if you haven’t declare a access modifier, then java will assume it as a “public” variable.
Ex: private String strCountry;
Variable names usually start with lowercase letters to distinguish them from class names.
You can do the variable declaration and assigning values in one statement.
Ex: String strCountry = “
Also be aware how to indicate assignment of value. String variables need its assigning value within “”. Integer, double, float, long, short, byte need value directly. Char needs its assigning values within ‘’.
Ex:
String strCountry = “
Int intCountryCode = 94;
Char chrCountryLetter = ‘S’;
Every text you input using the keyboard into the program is receives as a String. And every thing you print to screen must be a string. You can print an integer on screen using the statement (If x is an integer): System.out.println(x);. Computer will convert integer ‘x’ into String data type and then prints on screen.
We’ll discuss about reference types later.
Tip: Java is a strongly typed language, which means that every variable and expression has a type that is known at compile-time itself
SCJP Tip: Variables declare inside the class declaration, but not inside a method known as ‘class data fields’ or ‘member variables’. Then variables declare inside methods known as ‘local variables’.
SCJP Tip: There’re two kinds of member variables. (1)The static variable is a member variable declared using the keyword static
within a class body. Static variables are also known as class variables. They are created when the class is loaded. (2) The instance variable is also a member variable, but it is declared without the keyword static
. Instance variables come into life when an instance (i.e. object) of a class is created and they remain associated with that instance. Instance variables are destroyed as soon as that particular instance is destroyed.
No comments:
Post a Comment