- C Language (4)
- Java (7)
Chapter 3 – Identifiers ,Keywords and Types
January 9th, 2010 by Ajay Comments »What are Identifiers?
These are nothing but the names given to a variable class or method.
Note: An identifier can start with a letter, underscore( _ ) or a dollar sign ( $ ) .
- Identifiers are case sensitive and have no word limits.
- Identifier can not be a keyword.( can be used as a part of the name)
What are Keywords?
These are words that provide some special meaning to the Java Compiler. So they can not be used as an identifier.
Eg: public static void main(String args[]). Here public, static, void, main, String are all keywords.
Language Types:
1. Primitive type (8)
Logical - Boolean( either true or false-default value)
Textual – char (given in single quotes ( ‘ ’ )) String( “ ”)
Integral – byte,short,int,long
Floating point – double, and float- represented with 3.414f.
2. Reference Type:
Those variables that refer to a object are reference variables.
Common mistakes in C : Problem in Coding Part 4
January 4th, 2010 by Ajay Comments »10.Underconstrained fundamental types:
Different compilers, or even different options of the same compiler, define the fundamental type int as either 16 or 32 bits. “Sizeof” is your friend!!!
11.Utterly Unsafe Arrays:
C’s arrays and associated memory management are completely, utterly unsafe, and even obvious cases of error are not detected.
Some times it work and not always.
Don’t do this
int thisIsNuts[4];
int i;
for (i = 0; i < 10; ++i) {
thisIsNuts[ i ] = 0;
/* Isn’t it great ? I can use elements
* 1-10 of a 4 element array, and no one
* cares */
}
12.Octal Numbers:
In C, numbers beginning with a zero are evaluated in base 8.
If there are no 8’s or 9’s in the numbers, then there will be no complaints from the compiler, only screams from the programmer when he finally discovers the nature of the problem.
/* Line up numbersfor typographical clarity lose
* big time
*/
int numbers[] = { 001,
010, /* 8 not 10 */
014 }; /* 12, not 14 */
Common mistakes in C : Problem in Coding Part 3
January 3rd, 2010 by Ajay Comments »7.Unsafe Returned Value:
char *f() {
char result[80];
sprintf(result,”anything will do”);
return(result); /* Oops! result is allocated
on the stack. */
}
int g() {
char *p;
p = f();
printf(”f() returns: %s\n”,p);
}
The “wonderful” thing about this bug is that it sometimes seems to be a correct program.
As long as nothing has reused the particular piece of stack occupied by result.
8.Uninitialized Local Variables:
Actually, this bug is so well known
Consider the simplest case:
void sample(a)
{
int b;
if(b) {/* bug! b is not initialized! */ }
}
The value of b is not initialized.
It can be any garbage value.
void sample(int a)
{
int *b;
if(a)
b=malloc(a);
if(b)
{
/* BUG! b may have valid address */
/* or may have a garbage value */
*b=a;
}
}
9.Cluttered Compile time environment:
Consider the following code
#include <stdio.h>
#define BUFFSIZE 2048
long foo[BUFSIZ]; /* Note: Spelling of* BUFSIZ != BUFFSIZE */
This compiles without an error, but will fail in predictably awful and mysterious ways, because BUFSIZ is a symbol defined by stdio.h.
Common mistakes in C : Problem in Coding Part 2
January 3rd, 2010 by Ajay Comments »4.Mismatched Header Files:
Suppose sample.h contains:
struct sample { BOOL a};
file F1.c contains
#define BOOL char
#include “sample.h”
file F2.c contains
#define BOOL int
#include “sample.h”
now, F1. and F2 disagree about the fundamental attributes of structure “sample”. If they talk to each other, You Lose!
5.Phantom Returned Values:
Suppose you write this
int sample (a)
{ /* buggy, because */
if (a) return(1); /* sometimes no */
} /* value is returned */
- Generally speaking, C compilers, and C runtimes either can’t or don’t tell you there is anything wrong.
- What actually happens depends on the particular C compiler.
- Depending on how unlucky you are, the program may even appear to work for a while.
- Now, imagine the disaster that can result if “sample” was thought to return a pointer!
6.Easily Changed Block Scope:
if( … )
sample();
else
bar();
which, when adding debugging statements, becomes
if( … )
sample(); /* the importance of this semicolon can’t
be overstated */
else
printf( “Calling bar()” ); /* oops! the else */
bar(); /* stops here */
/* oops! bar is always executed */
There is a large class of similar errors, involving misplaced semicolons and brackets.
Common mistakes in C : Problem in Coding Part 1
January 3rd, 2010 by Ajay Comments »1.Non Terminated Content:
“Accidentally” terminated by some subsequent comment, with the code in between swallowed.
a=b; /* this is a bug
c=d; /* c=d will never happen */
2.Accidental Assigment/ Booleans:
if(a=b) c; /* a always equals b, So c will be executed all the times */
The assignment operator is too easy to confuse with the equality operator
But we need to be very careful to check the Boolean expression.
if( 0 < a < 5) c; /* this “boolean” is always true! */
Always true because (0<a) generates either 0 or 1 depending on if (0<a), then compares the result to 5, which is always true, of course.
3.Unhygienic Macros:
#define assign(a,b) a=(char)b
assign(x,y>>8)
becomes
x=(char)y>>8 /* probably not what you wanted */
Packages in Java
January 2nd, 2010 by Nachi Comments »Packages
There are no definite rules to create packages they are grouped via related classes.
package <package name>[sub_package]*;
Note: It must be at the beginning of the source code.
Only one package declaration is permitted per source file.
Dot operator is used to refer the hierarchy of the package.
Import Statement
The use of import statement is to inform the compiler- where to the classes so that the current class makes use of the pre-written class.
Eg:
import java.util.List;
import java.io.*;
Note:The star(*) is used to access all the classes in the package.
Quick brush up with the Object Oriented Programming Concepts – Classes are the blueprint for objects
January 2nd, 2010 by Nachi Comments »Classes are the blueprint for objects.
What does a class define?
Class consists of attributes(data elements that describe a object) and methods (what the object can do) . Together they are called members.
Java supports
- Encapsulation
- Inheritance
- Polymorphism
[For understanding purpose:
- Encapsulation - restricting data access
- Inheritance - using the base class properties
- Polymorphism - more than one form ]
Every class has atleast one constructor. Even if the programmer has not written one it is defined by Java programming environment (Default Constructor). The generated constructor takes no arguments eg: new abc()
Note: The constructor takes no return type and the class name should be the constructor name.
Lets understand Java from basics – Coding Stuff
January 2nd, 2010 by Nachi Comments »CODING STUFF:
Sample.java
public class Sample
{
public void printfn()
{
System.out.println(“Sample program for java”);
}
}
Sampletest.java
public class sampletest
{
public static void main (String args[])
{
Sample s= new Sample();
s.printfn();
}
}
Note :
- The class name and the file name should be the same.
- Every method should return a value if not void keyword should be placed.
- The statements should be ended with a semicolon(;).
public static void main(string args())
{
// prg code;
}
The execution of the program starts from the above statement. Beginners should understand the need to write the above line.
- Public defines that the access to the main method.
- Static defines that, once the class is loaded into the memory the static variables and methods are loaded automatically.
- String args[] this defines the argument that the main method accepts. The need of this will be seen once we go in to hardcore programming.
Lets understand Java from basics – Task of JVM
January 2nd, 2010 by Nachi Comments »Loads code: Performed by class loader- loads the classes need for execution of the program and the memory layout us determined here.
Verifies code: Performed by byte code verifier: checks for illegal code or conversions (like attempts to change object type/access rights for the object )
Executes Code: Performed by Runtime interpreter.
Lets understand Java from basics – Java Architecture(How is Java Designed)
January 2nd, 2010 by Nachi Comments »Lets understand java from basics :
How is java designed :
Java Architecture
1. The JVM :- (Java Virtual Machine) Hardware platform specification to which you compile all the Java technology . (converts source code to byte code)
2.Garbage Collection:- Avoids memory leak.
What is memory leak?- In C/C++ : programmer is responsible for de-allocating the memory so when the allocated memory is not de allocated then it lead to state that where NO memory is left for the new allocation that state is called memory leak.
(How java avoids memory leak?)- Using System level thread tracks each memory allocation.
3.The JRE:- The java runtime environment . (any compiled code in java needs a JRE to run the byte code.)
4.JVM tool interface.
