Languange Fundamentals of Java Part1
Languange Fundamentals of Java Part1
-----------------------
To prepare java application,we need some fundamentals provided by Java programming
language
1.Tokens
2.Data types
3.Type Casting
4.Java Statements
5.Arrays
1.Tokens:
---------
Smallest logical unit in java programming is called as "Lexeme".
int a=b+c*d;
Lexemes: int,a,=,b,+,c,*,;----->9
Tokens:
--------
1)Data Types:int
2)Identifiers: a,b,c,d
3)Operators:=,+,"*
4)Special Symbol:;
Types of tokens:4
------------------
1.Identifiers
--------------
Identifiers is a name assigned to the programming elements like
variables,methods,classes,abstract classes,interfaces,....
EX:
---
int a=10;
int---->Data Types
a------>variable[Identifier]
=------>Operator
10----->constant
;----->Terminator
To provide identifiers in java programming,we have to use the following rules and
regulations.
1.Identifiers should not be started with any number, identifiers may be started
with an alphabet,'_' symbol,'$'symbol, but,the subsequent symbols may be a
number,an alphabet,'_'symbol,'$'symbol.
int eno=111;---->Valid
int 9eno=999;---->Invalid
String_eaddr="Hyd";---->Valid
float $esal=50000.of;---->valid
String emp9No="E-9999";---->Valid
String emp_Name="Durga";---->Valid
float emp$Sal=50000.0f;---->Valid
2.Identifiers are not allowing all operators and all special symbols
except'_'and'$' symbols.
int empNo=111;---->valid
int emp+No=111;---->Invalid
String emp*Name="Durga";---->Invalid
String #eaddr="Hyd";---->Invalid
String emp@Hyd="Durga";---->Invalid
float emp.sal=50000.0f;---->Invalid
String emp-Addr="Hyd";---->Invalid
String emp_Addr="Hyd";---->Valid
5.In java applications,we can use all predefined class names and interface names as
identifiers.
EX1:
----
int Exception=10;
System.out.println(Exception);
Status:No Compilation Error
OP:10
EX2:
----
String String="String";
System.out.println(String);
Status:No Compilation Error
OP:String
EX3:
----
int System=10;
System.out.println(System);
Status: COmpilation Error
Reason:
-------
Once if we declare "System"[Class Name] as an integer variable then we must use
that "System" name as integer variable only in the remaining program,in the
remaining program if we use "System" as class name the compiler will rise an error.
In the above context,if we want to use"System" as class name then we have to use
its fully qualified.
Note:
-----
Specified class names and interface names along with package names is called as
Full Qualified Name.
EX:java.io.BufferedReader
java.util.ArrayList
EX:
int System=10;
java.lang.System.out.println(System);
System=System+10;
java.lang.System.out.println(System);
System=System+10;
java.lang.System.out.println(System);
status:No Compilation Error
OP:10
20
30
Along with the above rules and regulations,JAVA has provided a set of suggessions
to use identifiers in java programs
1.In java applications, it is suggestible to provide a set of suggessions to use
identifiers with a particular meaning.
Ex:
---
String xxx="abc123";------>Not Suggestible
String accNo="abc123";------>suggestible
2.In java applications,we dont have any restrictions over the length of the
identifiers,we can declare identifiers with any length,but,it is suggestible to
provide length of the identifiers around 10 symbols.
Ex:
---
String permanentemployeeadress="Hyd";---->Not Suggestible
String perEmpAddr="Hyd";--->Suggestible
2.Literals:
-----------
Literal is a constant assigned to the variables.
EX:
---
int a=10;
int----->data types
a------->variables/identifier
=------->operator
10------->constant[Literal]
;-------->Special symbol.
To prepare java programs,JAVA has provided the following set of literals.
1.Integer/Integral Literals:
-----------------------------
byte,short,int,long---->10,20,30....
char---->'A','B',....
3.Boolean Literals:
-------------------
boolean----->true,false
4.String Literals:
------------------
String---->"abc","def",....
Note:
-----
JAVA7 has given a flexibility like to include '_' symbols in the middle of the
literals inorder to improve readability.
EX:
---
float f=12345678.2345f;
float f=1_23_45_678.2345f;
Note:
-----
Binary Number Systems is not supported by all the java versions upto JAVA6,but
,JAVA7 and above versions are supporting Binary Number Systems, because, it is a
new feature introduced in JAVA7 version.
EX:
---
int a=10;----->It is decimal number,it is not octal number.
int b=012345;---->valid
int c=O234567;---->Invalid,number is prefixed with O,not zero
int d=04567;---->Valid
int e=05678;----->Invalid,8 is not octal number systems alphabet.
Ex:
---
int a=10;--->valid
int b=20;--->valid
int c=30;--->valid
EX:
---
int a=10;----->It is not hexa decimal number,it is decimal number.
int b=0x102345;----->Valid
int c=0X56789;------>Valid
int d=0x89abcd;----->valid
int e=0x9abcdefg;----->Invalid
Keywords/Reserved Words:
-------------------------
If any predefined word having both word recognization and internal functionality
then that predefined word is called as Keyword.
If any predefined word having only word recognization without internal
functionality then that predefined word is called as Reserved Word.
Eg:goto,const.
but c lang is return type is optional and default return type is int.
h)unused keyword:(2)
goto,const
about goto:
usage of goto created several problems in old languages and hence SUN people
band this keywod in java.
About const:
use final instead of const.
So goto and const are unused keywords and if we are trying to use we will get
compile time error.
i)Reserved Literals:(3)
true,false,null
Conclusions:
------------
1. All 53 reserved words in java only lowercase alphabet symbols.
2. in java we have only new keyword and there is no delete keyword in java, because
destruction of useless object is the responsibility of garbage collector.
3. the fallowing are new keywords in java
Strictfp(1.2v),assert(1.4v),enum(1.5v)
4. strictfp but not strictFP
instanceof but not instanceOf
sycronzed butnot synchronize
extends butnot extend
implements butnot implement
import butnot imports
const butnot constant
Operators:
----------
Operator is a symbol,it will perform a particular operation over the provided
operands.
1. Arithmetic Operators:
------------------------
+,-,*,/,%,++,--
2.Assignment Operators:
-----------------------
=,+=,-=,*=,/=,%=,......
3.Comparsison Operators:
------------------------
==,!=,<,>,<=,>=,.......
6.Short-Circuit Operators:
--------------------------
&&,||
7.Ternary Operator:
-------------------
Expr1?Expr2:Expr3;
Ex1:
----
class Test
{
public static void main(String[]args)
{
int a=10;
System.out.println(a);
System.out.println(a++);
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
System.out.println(a);
}
}
Status:No Compilation Error
OP: 10
10
12
12
10
10
Ex:
---
class Test
{
public static void main(String[]args)
{
int a=5;
System.out.println(++a-++a);
}
}
Status:No Compilation Error
OP:-1
Ex:
---
class Test
{
public static void main(String[]args)
{
int a=5;
System.out.println((--a+--a)*(++a-a--)+(--a+a--)*(++a+a++));
}
}
EX:
---
class Test
{
public static void main(String[]args)
{
boolean b1=true;
boolean b2=false;
System.out.println(b1&b1);//true
System.out.println(b1&b2);//false
System.out.println(b2&b1);//false
System.out.println(b2&b2);//false
System.out.println(b1|b1);//true
System.out.println(b1|b2);//true
System.out.println(b2|b1);//true
System.out.println(b2|b2);//false
System.out.println(b1^b1);//false
System.out.println(b1^b2);//true
System.out.println(b2^b1);//true
System.out.println(b2^b2);//false
}
}
T---->1
F---->0
EX:
---
class Test
{
public static void main(String[]args)
{
int a=10;
int b=2;
System.out.println(a&b);
System.out.println(a|b);
System.out.println(a^b);
System.out.println(a<<b);
System.out.println(a>>b);
}
}
int a=10;----->1010
int b=2;------>0010
a&b--->10&2-->0010---->2
a|b--->10|2-->1010----->10
a^b---->10^2--->1000----->8
a<<b--->10<<2---->00001010
001010000--->40
-->Remove 2 symbols at leftside and append 2 0's at right side.
a>>b--->10>>2---->00001010
00000010
Note:
-----
Removable Symbols may be 0's and 1's but appendable symbols must be 0's.
Short-Circuit Operators:
------------------------
The main intention of Short-Circuit operators is to improve java applications
performance.
EX:
--
1.&& 2.||
| vs ||
In the case of Logical-OR operator,if the first operand value is true then it is
not required to check second operand value,directly,we can predict the result of
overall expression is true.
In the case of'||' operator,if the first operand value is true then JVM will get
the overall expression result is true with out evaluating second operand value,here
JVM is not evaluating second operand expression unneccessarily,it will reduce
execution time and it will improve application performance.
Note:
-----
If the first operand value is false then it is mandatory for JVM to evaluate
second operand value inorder to get overall expression result.
Ex:
---
class Test
{
public static void main(String[]args)
{
int a=10;
int b=10;
if((a++==10)|(b++==10))
{
System.out.println(a+" "+b);//OP:11 11
}
int c=10;
int d=10;
if((c++==10)||(d++==10))
{
System.out.println(c+" "+d);//OP:11 10
}
}
}
& Vs &&
---------
In the case of Logical -AND operator,if the first operand value is false then it
is not required to check second operand value,directly ,we can predict the result
of overall expression is false.
In the case of '&' operator,even first operand value is false,still JVM evalutes
second operand value then only JVM will get the result of overall expression is
false,here evaluating second operand value is unneccessary,it will increase
execution time and it will reduce application performance.
In the case of '&&'operator,if the first operand value is false then JVM will get
the
overall expression result is false without evaluating second operand value,here
JVM is not evaluating second operand expression unneccessarily,it will reduce
execution time and it will improve application performance.
Note:
-----
If the first operand value is true then it is mandatory for JVM to evaluate second
operand value inorder to get overall expression result.
EX:
---
class Test
{
public static void main(String[]args)
{
int a=10;
int b=10;
if((a++!=10)&(b++!=10))
{
}
System.out.println(a+" "+b);//OP:11 11
int c=10;
int d=10;
if((c++!=10)&&(d++!=10))
{
}
System.out.println(c+" "+d);//OP:11 10
}
}
Data Types:
-----------
Java is strictly a typed programming language, where in java applications before
representing data first we have to confirm which type of data we representing.In
this context,to represent type of data we have to use "data types".
Ex:
---
i=10;--->invalid,no data type representation.
int i=10;---->valid,type is represented then data is represented.
Reason:
-------
'byte' data type is providing a particular range for its variables like -128 to
127,in this range only we have to assign values to byte variables.
If we want to identify range values for variablers on the basis of data types then
we have to use the following formula.
n-1 n-1
-2 to 2-1
Where 'n' is no of bits.
7 7
-2 to 2 -1
-128 to 128-1
-128 to 127