Thursday, December 4, 2014

Without value, I am none of use...

A variable without value is just wastage of memory.

We know about variables and their different sections, data type, variable name and value. We are capable of declaring a variable with its data type and name. Now, the question is what purpose it is going to serve.

Well, as I said earlier, any program is a combination of data and instruction to use the data.

Yes, that's it, any variable we create, we'll use it in our program. Whatever the variable it is may be primitive, may be reference, it has something to do with the instructions.

So, how do I give value to a variable and how do I use them ?
The first part answer will be given in this section, for the second section, you have  to wait for some more articles to be published.

Remember, variable declaration syntax,
data_type variable_name [=value];

Value is in brackets, which tells us that this part is optional. It is legal in Java to declare a variable and not assigning it a value. But, if you are trying to use a variable in any operation, you must have to assign a value to work with the it.

Assigning a variable can be done in the following ways,

  • Use an operation to assign the value. Just keep in mind that, the output of the operation must have to be of similar type that is of the variable type. You simply can not assign to an int any operation that executes to be double.
    For example,
    int a=5, int b=5; int c= a+b;
    This is valid. But following is not valid.
    double a=10, int b=2; int c= a+b;
    Compiler will raise an error while compiling this program.
  • You can use literals to assign value(like, 5, 10, 10.5f, 12.3d, 'C', "Java" etc. More on literals in the next section.
  • For reference type variables, you can assign a reference to an object of the same type. There is a long explanation of this. Please don't bother now. For now, just forget this part.
Literals: Literals are source code representation of any fixed value, like 5, 6, 'C' etc. So, these values are directly represented in code without any computation. Primitives are special types built into the language, they have the facility to use this direct representation. As mentioned earlier only java.lang.String can also use the same facility apart from primitives. From Java 1.5, in this list added some more Classes, which use the same facility. But for now, don't hesitate over these. These all will be discussed in later articles.

Literals in Java can be divided into 3 subsections,

Integer literals: Whole number representation in source code. Such as 5, 10. 20, 40, 50 etc. All the whole number data types (byte, short, int, long) can be created from these literals.
Integer literals can be suffixed with a 'L' or 'l' to show that this is a long variable.
Integer literals can be expressed in either of the following number systems,

  • Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day. Its normal number, we use every day. But keep in mind that numbers can not be prefixed by 0.
    For example, 017 and 17 are different in Java
  • Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F. Number literal prefixed with 0x (0X) are known as Hexadecimal.
    For example, 0xFF is 255 in decimal.
  • Octal: Base 8, whose digits consist of the numbers 0 through 7. Number prefixed with a 0.
    For example, 011 is 9 in decimal. (This is why in decimal number literals with 0 is not allowed)
  • Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later). Literals prefixed with 0b (0X).
    For example, 0b11 is 3 in decimal.
Following program shows different Integer literals.
 public static void main(String[] args) throws IOException {  
           int a = 5, b = 0X1F, c = 0xFF, d = 0b11, e = 0B11, f = 017;  

Floating - point literals: Real number representation in Source code. Such as 1.2f, 2.4d, 5.2, 4.3F etc. All the real number data types (float, double) can  be created from these literals. By default a floating-point literal is treated as a double value. To mention it explicitly as float, you need to suffix the literal with 'f' or 'F'. 'E' or 'e' can be used as scientific notation in floating-point literals.

Following program shows different floating-point literals.
 public static void main(String[] args) throws IOException {  
           float a = 5.2f, b = 2.4F, c = 3.2e2f, d = 1.6E3F;  
           double f = 1.2d, e = 4.2D, g = 1.4;  

Character literals: This is the literal for Unicode(UTF-16) character in different representation. We use single quote to assign a character value to a variable. We can also use Unicode escape in these literals.

Following program show different character literals,
 public static void main(String[] args) throws IOException {  
           char c = 'c', D = 'D', nullValue = '\u0000';  

String literals: This the literal for sequence of characters. We use double quotes to assign String value. Different escape characters can also be part of String literal.

Following program shows. different String literals,
 public static void main(String[] args) throws IOException {  
           String a = "a";  
           String nullValue = null;  
           String newLine = "\n";  
           String backSpace = "\b";  

Note: null can also be used for any reference type and not only String.
Class literal: We can use class types directly in source code representation too. These are called Class literal. This refers to the object (of type Class) that represents the type itself. We'll be looking into this later.

Using '_' in numeric literal: For large numbers, it is difficult to understand the value of a numeric literal. So, from Java SE 7 onwards Java allows to separate the number using '_' in a numeric literal. The use of '_' is bound by some rules. Below are the rules,
  • At the beginning or end of a number
  • Adjacent to a decimal point in a floating point literal
  • Prior to an F or L suffix
  • In positions where a string of digits is expected
Apart from the cases mentioned, you can use '_' in numeric literals.

Following program shows. use of '_' in numeric literals

 public static void main(String[] args) throws IOException {  
           int a = 12_45_56_78;  
           int b = 0xFF_45_84_96;  
           float pi = 3.14_15F;  

That's all about literals. We'll be looking on the methods on next articles.

Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment