0% found this document useful (0 votes)
55 views

Languange Fundamentals of Java Part1

Tokens, data types, type casting, statements, and arrays are some of the fundamental concepts in the Java programming language. The document discusses these concepts in detail, including defining tokens as the smallest logical units in Java and describing the different types of tokens. It also explains identifiers, literals, keywords, and number systems used in Java.

Uploaded by

aypra Murala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Languange Fundamentals of Java Part1

Tokens, data types, type casting, statements, and arrays are some of the fundamental concepts in the Java programming language. The document discusses these concepts in detail, including defining tokens as the smallest logical units in Java and describing the different types of tokens. It also explains identifiers, literals, keywords, and number systems used in Java.

Uploaded by

aypra Murala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Language Fundamentals:

-----------------------
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".

The Collection of Lexemes come under a particular group is called as "Token"

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
------------------

To prepare java applications,java has provided the following list of tokens.


1.Identifiers
2.Literals
3.Keywords/Reserved words
4.Operators

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

3.Identifiers are not allowing spaces in the middle.


concat(--)---->Valid
forName(--)---->Valid
forName()---->Invalid
getInputStream()---->Valid
getInputStream()---->Invalid

4.Identifiers should not be duplicated with in the same scope,identifiers may be


duplicated in two different scopes.
class A{
int i=10;---->Class level
short i=20;---->Error
double f=33.33---->No Error
void m1(){
float f=22.22f---->local variable
double f=33.33;---->Error
long i=30;---->No Error
}
}

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

3.If we have multiple words with in a single identifier then it is suggestible to


seperate multiple words with special notations like'_'symbols.
EX:
---
String permEmpAddr="Hyd";---->Not Suggestible
String perm_Emp_Addr="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',....

2.Floating Point Literals:


--------------------------
float----->10.22f,23.345f,.....
double----->11.123,456.345,....

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;

If we provide'_'symbols in the literals then compiler will remove all'_' symbols


which we provided,compiler wil reformate that number as original number and
compiler will process that number as original number.

Number System in Java:


----------------------
IN general, in any programming language,to represent numbers we have to use a
particular system.

There are four types of number systems in programming languages.


1.Binary Number Systems[BASE-2]
2.Octal Number Systems[BASE-8]
3.Decimal Number System[BASE-10]
4.Hexa Decimal Number Systems[BASE-16]

In java,all number systems are allowed,but,the default number system in java


applications is"Decimal Number Systems".

1.Binary Number Systems[BASE-2]


--------------------------------
If we want to represent numbers in Binary Number Systems then we have to use 0's
and 1's,but,the number must be prefixed with either '0b' or'0B'.

int a=10;----->It is not binary number,it is decimal num.


int b=0b10;----->valid
int c=0B1010;---->valid
int d=0b1012;----->Invalid,2 symbol is not binary numbers alphabet.

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.

2.Octal Number Systems[BASE-8]:


------------------------------
If we want to prepare numbers in Octal Number Systems then we have to use the
symbols like 0,1,2,3,4,5,6 and 7,but, the number must be prefixed with '0'[zero].

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.

3.Decimal Number System[BASE-10]:


---------------------------------
If we want to represent numbers in Decimal number system then we have to use the
symbols like 0,1,2,3,4,5,6,7,8 and 9 and number must not be prefixed with any
symbols.

Ex:
---
int a=10;--->valid
int b=20;--->valid
int c=30;--->valid

4.Hexa Decimal Number Systems[BASE-16]:


---------------------------------------
If we want to prepare numbers in Hexa decimal number system then we have to use the
symbols like 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e and f, but the number must be prefixed
'0x' or'0X'.

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.

To prepare java Applications,Java has provided the fallowing list of keywords.


------------------------------------------------------------------------------
a) Keywords for Data types:(8)
byte,short,int,long,float,double,char,boolean.
b) keywords for flow control:(11)
if,else,switch,case,default,while,do,for,break,continue,return
c) keywords for modifiers:(11)
public,private,protect,static,final,abstract,synchronized,native,strictfp,
trancient,volatile.
d) keywords for exception handling:(6)
try,catch,finally,throw,throws,assert (1.4 ver came)
e) class related keyword:(6)
class,interface,extends,implements,package,import.
f) Object related keywords:(4)
new,instanceof,super,this
g) return tpe keyword:(1)
void

return type is mandatory.If a method wont return anything then we have to


declare that with void return type .

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

true and false :values for Boolean data type


null: default value for object reference.

j)enum keyword:(1)(1.5 version)


we can use enum to define a group of named constants.
Enum month
{
Jan,feb,mar,apr��dec
}

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.

To prepare java applications,JAVA has provided the following list of operators.

1. Arithmetic Operators:
------------------------
+,-,*,/,%,++,--

2.Assignment Operators:
-----------------------
=,+=,-=,*=,/=,%=,......

3.Comparsison Operators:
------------------------
==,!=,<,>,<=,>=,.......

4.Boolean Logical Operators:


----------------------------
&,|,^

5.Bitwise Logical 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++));
}
}

Status:No Compilation Error


OP: 16

A B A&B A|B A^B


----------------
T T T T F
T F F T T
F T F T T
F T F T T
F F F F F

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
}
}

A B A&B A|B A^B


----------------
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

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

-->Remove 2 symbols at right side and append 2 0's at left side.

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,even first operand value is true,still,JVM evalutes


second operand value then only JVM will get the result of overall expression is
true,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 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.

In java applications,data types are able to provide the following advantages.

1.We are able to identify memory sizes to store data.


Ex: int i=10;--->int will provide 4 bytes of memory to store 10 value.

2.We are able to identify range values to the variable to assign.


Ex: byte b=130;--->Invalid
byte b=125;--->Valid

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.

To prepare java applications,JAVA has provided the following data types.


1.Primitive Data Types/Primary Data types

1.Numeric Data Types

1.Integral data types/Integer Data types:


byte----->1 bytes---->0
short----->2 bytes---->0
int------->4 bytes----->0
long------->8 bytes---->0
2.Non-Integral Data Types:
float--->4 bytes---->0.0f
double---->8 bytes---->0.0

1. Non-Numeric Data types:


char---->2 bytes---->''[single space]
boolean----> 1 bit--->false

2.User defined data types/Secondary Data types


All classes,all abstract classes,all interfaces,all arrays,......

No fixed memory alloction for User defined data types

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.

Ex:Data type:byte,size=1 byte=8 bits.


8-1 8-1
-2 to 2 - 1

7 7
-2 to 2 -1

-128 to 128-1

-128 to 127

You might also like