Sunday, 31 July 2011

JAVA Type Casting

Type Casting  refers to changing an entity of one datatype into another. This is important for the type conversion in developing any application. If you will store a int value into a byte variable directly, this will be illegal operation. For storing your calculated int value in a byte variable you will have to change the type of resultant data which has to be stored. This type of operation has illustrated below :
In this example we will see that how to convert the data type by using type casting. In the given line of the code c = (char)(t?1:0); illustrates that if t which is boolean type variable is true then value of c which is the char type variable will be 1 but 1 is a numeric value. So, 1 is changed into character according to the Unicode value. But in this line c = (char)(t?'1':'0'); 1 is already given as a character which will be stored as it is in the char type variable c.
Code of the program :
public class conversion{
  public static void main(String[] args){
  boolean = true;
  byte b = 2;
  short s = 100;
  char c = 'C';
  int i = 200;
  long l = 24000;
  float f = 3.14f;
  double d = 0.000000000000053;
  String g = "string";
  System.out.println("Value of all the variables like");
  System.out.println("t = " + t );
  System.out.println("b = " + b );
  System.out.println("s = " + s );
  System.out.println("c = " + c );
  System.out.println("i = " + i );
  System.out.println("l = " + l );
  System.out.println("f = " + f );
  System.out.println("d = " + d );
  System.out.println("g = " + g );
  System.out.println();
  //Convert from boolean to byte.
  b = (byte)(t?1:0);
  System.out.println("Value of b after conversion : " + b);
  //Convert from boolean to short.
  s = (short)(t?1:0);
  System.out.println("Value of s after conversion : " + s);
  //Convert from boolean to int.
  i = (int)(t?1:0);
  System.out.println("Value of i after conversion : " + i);
  //Convert from boolean to char.
  c = (char)(t?'1':'0');
  System.out.println("Value of c after conversion : " + c);
  c = (char)(t?1:0);
  System.out.println("Value of c after conversion in unicode : " + c);
  //Convert from boolean to long.
  l = (long)(t?1:0);
  System.out.println("Value of l after conversion : " + l);
  //Convert from boolean to float.
  f = (float)(t?1:0);
  System.out.println("Value of f after conversion : " + f);
  //Convert from boolean to double.
  d = (double)(t?1:0);
  System.out.println("Value of d after conversion : " + d);
  //Convert from boolean to String.
  g = String.valueOf(t);
  System.out.println("Value of g after conversion : " + g);
  g = (String)(t?"1":"0");
  System.out.println("Value of g after conversion : " + g);
  int sum = (int)(b + i + l + d + f);
  System.out.println("Value of sum after conversion : " + sum);
  }
}


output:

symbolic constants in java

symbolic constants are named constants
like :
final double PI = 3.14 ;
They are constants because of the 'final' keywords, so they canNOT be reassigned a new value after being declared as final
And they are symbolic , because they have a name

A NON symbolic constant is like the value of '2' in expression
int foo = 2 * 3
Variable Scope
You cannot refer to a variable before its declaration.
You can declare variables in several different places:
  1. In a class body as class fields. Variables declared here are referred to as class-level variables.
  2. As parameters of a method or constructor.
  3. In a method's body or a constructor's body.
  4. Within a statement block, such as inside a while or for block.
Variable scope refers to the accessibility of a variable.
The rule 1 is that variables defined in a block are only accessible from within the block. The scope of the variable is the block in which it is defined. For example, consider the following for statement.
public class MainClass {

  public static void main(String[] args) {
    for (int x = 0; x < 5; x++) {
        System.out.println(x);
    }
  }

}
Rule number 2 is a nested block can access variables declared in the outer block. Consider this code.
public class MainClass {

  public static void main(String[] args) {
    for (int x = 0; x < 5; x++) {
        for (int y = 0; y < 3; y++) {
            System.out.println(x);
            System.out.println(y);
        }
    }
  }

}
Variables declared as method parameters can be accessed from within the method body. Class-level variables are accessible from anywhere in the class.
If a method declares a local variable that has the same name as a class-level variable, the former will 'shadow' the latter. To access the class-level variable from inside the method body, use the this keyword.

Variable Scope in a block

public class MainClass {
  public static void main(String[] args) {
    int outer = 1;

    {
      int inner = 2;
      System.out.println("inner = " + inner);
      System.out.println("outer = " + outer);
    }

    int inner = 3;
    System.out.println("inner = " + inner);
    System.out.println("outer = " + outer);
  }
}
 
output:
inner = 2
outer = 1
inner = 3
outer = 1
    

Java Data Types

Java programming language is a language in which all the variables must be declared first and then to be used. That means to specify the name and the type of the variable. This specifies that Java is a strongly-typed programming language. Like
  int pedal = 1;
This shows that there exists a field named 'pedal' that holds a data as a numerical value '1'. The values contained by the variables determines its data type and to perform the operations on it.
There are seven more primitive data types which are supported by Java language programming in addition to int. A primitive data type is a data type which is predefined in Java. Following are the eight primitive data types:
int
It is a 32-bit signed two's complement integer data type. It ranges from -2,147,483,648 to 2,147,483,647. This data type is used for integer values. However for wider range of values use long.
byte
The byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127 (inclusive). We can save memory in large arrays using byte. We can also use byte instead of int  to increase the limit of the code.
short
The short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to 32,767. short is used to save memory in large arrays.
long
The long data type is a 64-bit signed two's complement integer. It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger range of values. 
float
The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float (instead of double) to save memory in large arrays. We do not use this data type for the exact values such as currency. For that we have to use java.math.BigDecimal class.
double
This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). This data type is generally the default choice for decimal values.
boolean
The boolean data type is 1-bit  and has only two values: true and false. We use this data type for conditional statements. true and false are not the same as True and False. They are defined constants of the language.
char
The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535. They are not same as ints, shorts etc.
The following table shows the default values for the data types:
 Keyword  Description   Size/Format
 byte  Byte-length integer  8-bit two's complement
 short   Short integer  16-bit two's complement
 int  Integer   32-bit two's complement
 long   Long integer   64-bit two's complement
 float  Single-precision floating point  32-bit IEEE 
 double  Double-precision floating point  64-bit IEEE 
 char   A single character   16-bit Unicode character
 boolean   A boolean value (true or false)   true or false
When we declare a field it is not always essential that we initialize it too. The compiler sets a default value to the fields which are not initialized which might be zero or null. However this is not recommended. 

Java - Variable, Constant and Literal

Variable : You can assign the values to the variable once it has been declared. The values of the variable can be changed anywhere in the program if the variable is accessible in that scope. In this example we have used the variable  intvariable to illustrate this.
Constants: Constants are declared using the final keyword. The values of the constant can't be changed once its declared.
Literal : literal is an explicit number or string constant used in Java programs. This specifies the syntax of your declaration of different types of values and operations. That mean literal is totally based on the syntax. Whenever you want to show the message with java special symbols then you have to use the literals. For example to show the message "Directory of this file : c:\code\varconstltr.java" then you can write as shown below
System.out.println("Directory of this file : c:\\code\\varconstltr.java");
There are three type of literals : Numeric Literals, Char type Literals, String Literals as follows.
Code of the Program : 
public class varconstltr{
  public static final int constint=5;
  public static void main(String[] args){
  int intvariable;
    for (int 0;i <= 10;i++){
  intvariable = i;
  System.out.println("All the values are : \n intvariable = " 

+ intvariable + "\nconstint = " + constint);
  }
  System.out.println("Directory of this file : c:\\code\\varconstltr.java");
  }
}