What are Keywords, Datatypes, Variables, Literals in java?

Before start Java Programming, we have knowledge about these important words :

Keywords: In java Programming , A set of reserve word that is define for special purpose.  These words can not be identifiers (e.g. Variable name, function name, class name,object name etc.).

In Java , There are 50 keywords.



abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while


For example, if you try and create a new class and name it using a reserved word:
// you can't use try as it's a reserved word!
class try{
public static void main(String[] args) {
//class code..
}
}

In above example I am using finally as class name. It will not compile, instead you will get the following error:
<identifier> expected

Datatypes - We can define data type in a programming language is a set of data with values having predefined characteristics.
Data-types in any language specify the type of data to be stores and the size of memory to be allocated.
In Java, There are two types of data types.
1. primitive datatypes
2. Non-primitive datatypes



 Primitive datatypes- Primitive data types are predefined types of data, which are supported by the programming language. For example, integer, character, and string are all primitive data types.

Non-primitive datatypes - Non-primitive data types are not defined by the programming language, but that is  created by the programmer.It is  sometimes called "reference variables," or "object references.

Lets see memory allocated  and  default value of data type.

Data Type Memory Size Default value Declaration
byte 8 bits 0 byte a=9;
short 16 bits 0 short b=99;
int 32 bits 0 int c=999;
long 64 bits 0 long=999999;
float 32 bits 0.0f float b=37.8f;
double 64 bits 0.0 double c =27.098
char 16 bits '\u0000' char a ='e';
boolean JVM Dependent false boolean a =true;

The true & false are keywords in java which form as value for Boolean data variable.
ASCII (American Standard Code for Information Interchange) System- Supports 256 characters.
Unicode (universal international standard character encoding) System- Supports 65535 characters
C/C++ supports ASCII system and Java Supports Unicode system. So we can say java supports multiple language's characters.

Variable - Variables are temporary memory locations used to store a value. The value stored in a variable can change or vary.

Naming conversion of Variable:-
  • It should start with an alphabet, underscore ( _ ) OR dollar ($).
  • It can contain alphabets, digits, underscore OR dollar.
  • No Spaces, NO Special Characters except underscore and dollar.
  • we can not use keyword as Variable.
  •  It should be unique within its scope
 Syntax for Initialization Variable:
<variableName> = <value>;
 Example:
num = 10;
Initialization with Declaration Syntax:
   <data-type> <variableName> = <value>;
    Example:
     int num = 10;
There are Three types of Variable:
. local variable: A variable that is declared inside the method or block or loop is called local variable.
for example:
public void sum()
{
int a=10;
}
local variables are only visible to the method or block in which they are declared.

Instance Variable:
A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.Instance variable are also non-static variable because their values are unique to each instance of class.
for example:
 class A{
int a=6;
}

Static variable:
An instance variable is any field declared with static modifier, it tells the compiler that there is exactly only one copy of this variable. It cannot be local.
for example 
static  int num = 5;


Here example of all variable in a class

class MyClass{
int i=50; //instance variable
static int s=100; //static variable
void method(){
int loc=90; //local variable
}
}

Literals :
In Java Literals are used to specify the type of value. Literals in java are a sequence of characters (digits,letters and other characters) that represent constant values to be stored in variables.
 Types of literal

Data-types                            Literals
long                                          l
float                                          f
double                                     d
char                                         ‘ ’
String                                       “ ”


Examples
int i=10                                        //by default int value
long l = 10l                                  //long value with l literal
double d =10.5                           //by default double value
double d =10.5d                        //double value with d literal
float f =10.5f                             //float value with f literal
char c =‘A’                                //char value with ‘ ’ literal
String str = “apple”                   //String value with “ ” literal

SHARE

About prabhakar jha

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment