Translate

Showing posts with label Basic errors in assignments. Show all posts
Showing posts with label Basic errors in assignments. Show all posts

Monday, 18 August 2014

Basic errors in assignments

1. Local variables of a method are never initialised automatically. It would be a compile time error if we try to access them without initialisation.

2. int a;
   System.out.print(a);
  This is compile time error as a is not initialised.

3. int a=4, b;
    if(a==4)
        b=5;
   System.out.print(a);
   This is compile time error because variable expression a==4 will be checked at run time. So b will be uninitialised at compile time.

4.  int a=4, b;
     if(a==4)
        b=5;
    System.out.print(a);
    This is right as due to final a==4 is a constant expression and will be checked at compile time.

5. String str = "25";
    int no;
    no = str;   //Compile Time error
 This will be a compile time error. To remove error we should use
    no= Integer.parseInt(str);
 parse is a static method in Integer class which returns the integer conversion of the string.

Total Pageviews