Java Keywords, Datatypes, Variables and Literals ?

Java Keywords:-
Java reserved words are keywords that are reserved by Java for particular uses and cannot be used as identifiers (e.g., variable names, function names, class names, object name).
Some keywords (reserved words) in java:

Abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, false, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, null, package, private,protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, true, try, void, volatile, while,etc.
For example, if you try and create a new class and name it using a reserved word:

class finally                       // you can’t use finally as it’s a reserved word!
{
public static void main(String[] args)
{
//code of class
}
}
Using Finally as a identifier in the above examle will not compile, instead you will get the following error:
<identifier> expected

Java Data-types:-
These are Keywords in java. They are also known as Primitive data-types or Native data-types.
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 data types
2. non-primitive data types

Data TypesMemory SizeValue type
 byte 1 byte integer type
 short 2 byte integer type
 int 4 byte integer type
 long 8 byte integer type
 float 4 byte real values
 double 8 byte real values
 char 2 byte 1 Unicode char
 boolean 1 bit true/false
  • 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.

Java Variables:-
Variables are temporary memory locations used to store a value. The value stored in a variable can change/vary.
Rules for naming of variables in Java:-
1. It should start with an alphabet, underscore ( _ ) or dollar ($).
2. Can contain alphabets, digits, underscore or dollar.
3. No Spaces, NO Special Characters except underscore and dollar.
4. Should not be a keyword.
5. Should be unique within its scope.
6. Are case sensitive.

Declaration Syntax:
<Data-type> <variableName>;
Example:
int likes;             // here int is datatype and likes is variable name.
double d;             // here d is a variable name.
char tech;          // here tech is a variable name.
NOTE: In Java, all variables, objects and methods, make use of camel case. Example: MyVariable, EduTechLearners.

There are three types of variables in java:
1. local variable
2. instance variable
3. static variable
Local Variable: A variable that is declared inside the method or block or loop is called local variable.
Instance Variable: A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.
Static variable: A variable that is declared as static is called static variable. It cannot be local.

Example to understand the types of variables:
class A
{
int data=50;           //instance variable
static int m=100; //static variable
void method( )
{
int n=90;                 //local variable of the function method
}
}//end of class

Java Literals:-
In Java Literals are used to specify the type of value.
Different literals for data types:-
long—–> l
float—–> f
double—>d
char——> ‘ ’
String—-> “ ”
NOTE: There are NO Literals for “byte” and “short”.

Example:
float n = 22.5f;
in this above syntax, variable name is n and datatype is int and value is 22.5 and f literal shows the value is a float value. by default java takes double as decimal values.
int n = 22.5f;
here, variable n is int type but value type is float becoz of f literals. so becoz of f literals our variable type will not change. our variable is still int. it takes only integer part of float value.. that is 22.

Print Friendly, PDF & Email

Leave a Reply