Showing posts with label keywords. Show all posts
Showing posts with label keywords. Show all posts

Sunday, 18 May 2014

Basics OF Java

It  is very important to keep in mind the basic syntax of java program.
Basic Syntax:

Case Sensitivity - Java is case sensitive, which means identifier Test and test would have different meaning in Java.

Class Names - Class names should be start with upper case letter.If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
E.g. class MyFirstJavaProgram

Method Names - All method names  should be start with a Lower Case letter.If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.
E.g public void myFirstMethod()

Source  File Name - Name of the program file should exactly match the class name.
When saving the file, you should save it using the class name  and append '.java' to the end of the name.If the file name and the class name do not match your program will not compile.
E.g. : Assume 'Test' is the class name. Then the file should be saved as 'Test.java'

public static void main(String args[]) - Java program execution  starts from the main() method which is a mandatory part of every Java program.The main() method should have public and static  access modifier,void return type and string array as argument.
If we supply return type other than void then program compile successfully but it's not execute.

Java Identifiers:
All Java components require names. Names used for classes, variables and methods are called identifiers.
In Java, there are several points to remember about identifiers. They are as follows:
1. All identifiers should contains only character(A to Z or a to z),digit (0 to 9) and ( $ , _ ).
2. All identifiers should begin with a letter (A to Z or a to z), dollor character ($) or an underscore ( _ ).
3. After the first character identifiers can have any combination of characters,digits and ($) ,( _ ).
4. A key word cannot be used as an identifier.
5. Identifiers are case sensitive.

Example of identifiers
Legal identifiers: age, $salary,  _value , __1_value , abc123,abc_ xyz
Illegal identifiers: 123abc, -salary,@abc,abc!,abc@xyz

Java Modifiers:
There are two types of modifiers in java:
1. Access Modifiers: default, public , protected, private
2. Non-access Modifiers: final, abstract, strictfp,transient,native .
We will see into more details about modifiers in other post.

Java Variables:
We would see following type of variables in Java:
1.Local Variables
2.Class Variables (Static Variables)
3.Instance Variables (Non-static variables)
Class and Instance variables are global variables.

Java Keywords:
The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names.

abstract assert byte boolean
break case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while
Comments in Java:
Java supports single-line and multi-line comments . All statements  available inside any comment are ignored by Java compiler.

public class Test
{
public static void main(String []args)
{ // This is an example of single line comment /* This is also an example of multi line comment. This is also an example of multi line comment. This is also an example of multi line comment This is also an example of multi line comment */
       System.out.println("Hello Collegeone users !"); }
}

Saturday, 17 May 2014

Some Terminology

Token In C 

In a C source program, the basic element recognized by the compiler is the "token." A token is source-program text that the compiler does not break down into component elements.
A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
So, C tokens are of six types. They are,
        1. Keywords               (eg: int, while),
        2. Identifiers               (eg: main, total),
        3. Constants              (eg: 10, 20),
        4. Strings                    (eg: “total”, “hello”),
        5. Special symbols  (eg: (), {}),
        6. Operators              (eg: +, /,-,*)

 For example, the following C statement consists of five tokens:

printf("Hello, World! \n");


The individual tokens are:

printf                 //first token
(                       //second
"Hello, World! \n"  //third
)                       //fourth     
;                       //fifth 

Semicolons:

In C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity or ended of the execution of that statement. It is very powerful token,in future we will see about this.

For example, following are two different statements:

printf("Hello, World! \n");
return 0;

Comments

Comments are like helping text in your C program and they are ignored by the compiler. In a software company, this comment will help to understand of your code to the other. There are two types of showing the comment :

1. Multiple line comment

                       /* my first program

                           in C*/ 

2. Single line comment
                      //my first program  in C


Declarations and Initialization:

All variables must be declared before use, although certain declarations can be made
implicitly by content. A declaration specifies a type, and contains a list of one or more
variables of that type, as in:

int  lower, upper, step;
char c, line[1000];
A variable may also be initialized in its declaration. If the name is followed by an equals sign and an expression, the expression serves as an initializer, as in:
int   i = 0;
int   limit = MAXLINE+1;
float eps = 3.14;
The initialization is done once only, conceptionally before the program starts executing, and the initializer must be a constant expression.

 Lvalues and Rvalues in C:
There are two kinds of expressions in C:     
lvalue : An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.     
rvalue : An expression that is an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side. Following is a valid statement:
int g = 20;
But following is not a valid statement and would generate compile-time error:
10 = 20;

Thursday, 15 May 2014

Constants, Variable, Keywords and Data Types

C Keywords 

Keywords are the words whose meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords cannot be used as variable names because if we do so we are trying to assign a new meaning to the keyword, which is not allowed by the computer. Some C compilers allow you to construct variable names that exactly resemble the keywords. However, it would be safer not to mix up the variable names and the keywords. The keywords are also called ‘Reserved words’. Some keywords are:
                                         
DATA TYPES:
    
DATA TYPES describes us which type of data is used and how much memory will allocated for the data. C is rich in data types. The verity of data type allow the programmer to select appropriate data type to satisfy the need of application as well as the needs of different machine.

TYPES:
• Primary Data Type:                                                                                                                                 C compiler support five type of Predefined or fundamental data types:

 


• Derived Data Type:
              Array, pointer, structure and union are called derived data type in C language.

• User Defined Data Type:
            A user can define his her own identifiers to represent a data types. TYPEDEF keyword is used.
                              typedef  data_type  identifier

For more detail please go to the Data Type post or Click Here

Constant:
 
    A Constant is a quantity the value of which will not be changed when the program is being executed. It can be stored in the memory at the specified location. Or we can say that a "constant" is a number, character, or character string that can be used as a value in a program. Use constants to represent floating-point, integer, enumeration, or character values that cannot be modified.
 
There may be a situation in programming that the value of certain variables to remain constant during execution of a program. In doing so we can use a qualifier const at the time of initialization.
 
1.    const float pie =2.14; 2.    const int radius =3; 3.    const char c = 'V'; 4.    const char name[] = "Vishnu";  
When declaring a const variable, it is possible to put const either before or after the type: that is, both  
1.     int const a = 15; 
Or    
1.     const int x = 15; 

In C constant can also be used using preprocessor directive.
 
For example:  #define FIRST_NUMBER 1    // FIRST_NUMBER will be constant throughout the program. 
 
Types of Constants:



VARIABLE:
  
 A variable is a name given to the memory location where the data can be stored and accessed from that location. To indicate the memory location, each variable should be given a unique name called identifier. Variable names are just the symbolic representation of a memory location. Examples of variable name: sum, a_sum, count etc.

Rules for writing variable name in C 
1.    Variable name can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only.
2.    The first letter of a variable should be either a letter or an underscore. But, it is discouraged to start variable name with an underscore though it is legal. It is because, variable name that starts with underscore can conflict with system names and compiler may complain.
3.    There is no rule for the length of length of a variable. However, the first 31 characters of  a variable are discriminated by the compiler. So, the first 31 letters of two variables in a program should be different.