Saturday 2 March 2013

Java Basic Language Elements

In this post I am going to cover keywords, comments, variables and data types in Java.

Keywords in Java

     There are certain words which are reserved which helps the java compiler to figure out what it is suppose to do.Keywords cannot be used as names for class,variable or function.As mentioned previously Java is a case sensitive language and all keywords are in lower case.Specially if you have worked in C you write NULL but in java it is null. Following is the list of keywords in Java.


Comments in Java

     Comments in java are very similar to comments in C. For a single line comment we use 
//this is a single line comment where as in multi line comment we do the following
/*
This is multiline comment.
 This is part of Java Tuts.
*/
Comments are used for better understanding of Code.When compiler encounters these comments it just ignores them and goes ahead with compilation.

There are another type of comments. They are called JavaDocs comments.Syntax for that is as following
/**
@param someParameter
This is a JavaDoc comment
*/

Variable and Data Types

     Variables are used to hold data.All variables have a name, a type, and a scope.Name of variable is what programmers assign.Type can be a user defined data type or primitive data types.Scope is the portion of code in which the variable is visible.
Java has four main primitive data types built into the language. We can also create our own data types.

  • Integer - byte, short, int, and long.
  • Floating point - float and double.
  • Character - char.
  • Boolean -    variable with a value of true or false.

Rules for naming variable

  • Can consist of upper and lower case letters, digits, dollar sign ($) and the underscore ( _ ) character.
  • Must begin with a letter, dollar sign, or an underscore
  • Are case sensitive
  • Keywords cannot be used as identifiers
  • Within a given section of your program or scope, each user defined item must have a unique identifier
  • Can be of any length.

Example

Lets take a simple example
int number;  //Variable declaration
number = 10; //Variable assignment
int number defines variable with name "number" of type integer.Next statement assigns a value 10 to the variable number.

No comments:

Post a Comment

t> UA-39527780-1 back to top