You are on page 1of 8

TOKEN OR LEXICAL ELEMENTS : y Smallest individual units in a paragraph are known as tokens.

y Exp: Java has following categories of tokens. KEYWORDS OR RESERVE WORDS : exp class, while, double. IDENTIFIERS:  Refers to name of variables, functions, classes etc.  It is sequence of letters (including _ and $ ) and digits.  First character must be a letter or underscore _ or $ .  Case sensitive.  No limits on length of identifier.  Could be simple (like I, j) or compound (like System.out.println). VARIABLE: A symbolic name given to variety of values.  A variable is not a name for the data itself but for a location in memory that can hold data.  You can think a variable as a container or box where you can store data that you will need to use later.  A variable in java is designed to hold only one particular type of data; it can legally hold that type of data and no other.  Java is a STRONGLY TYPED language because it enforces this rule. The compiler will give a SYNTAX ERROR if you try to violate this rule. JAVA DATA TYPES

BUILD IN TYPES

USER DEFINED TYPES OBJECTS

INTEGER TYPE

REAL TYPE

BYTE

SHORT INT LONG y y y y y y y y

FLOAT

DOUBLE

CHAR

BOOLEAN

Java has 8 primitive (built in) types. Byte, short, Int, long hold integers. The 4 integer types are distinguished by the ranges of integers they can hold. BYTE 1 byte (can represent- 128 to 127). SHORT - 2 byte (can represent- 32768 to 32767). INT - 4 byte (can represent -2147483648 to 2147483647). LONG 8 byte (can represent - 9223372036854775808 to 9223372036854775807). Java does not support unsigned integers. Any numerical literal that contains a decimal point or exponential is a literal of type double (by default).

 To make a literal of type float, you have to append an F or f to the end of the number. Exp. Float a = 32.3989231134f; System.out.println(a); o/p is = 32.398923. y Ordinary integer s literals of type byte, short or int, depending on their size.  You can make a literal of type long by adding L as a suffix. For example: 17L or 728476874368L. y A zero at the beginning of an integer makes it an octal literal, e.g. 045 (37 in decimal). y A hexadecimal literal begins with 0x (e.g. 0x45). y For the type Boolean, there are two literals : true and false. OPERATORS: +, -, * etc. y All non primitive types represent objects in java. Exp: String is a predefined object type. A string is a sequence of characters. A string literal: Hello World! .

VARIABLE DECLARATION: A member variable can also be initialized at the point where it is declared. Public class DynamicInitialization { Public static void main (String args[]) { double a=3.0, b=4.0; //static initialization double c=Math.sqrt(a*a+b*b); //dynamic initialization System.out.println( Hypotenuse is +c); } } Public class Bank { Static double interestRate = 0.05; Static int maxWithdrawal = 200; } O/P - Hypotenuse is 5.0 y y y y A static member variable is created as soon as the class is loaded by the java interpreter, and the initialization is also done at that time. Declaration statements are the only type of statement that can occur outside of a subroutine. Assignment statements cannot. The float and double types hold real numbers and distinguished by their range and accuracy. float 4 byte (max: 1038)

y y

 A float can have about 7 significant digits. ( So that 32.3989231134 and 32.3989234399 would both have to be rounded off to about 32.398923 in order to be stored in a variable of type float.) double 8 byte (max: 10308 and about 15 significant digits). char holds a single character from the Unicode character set ( range 0 to 65,536).  Unicode character set, includes characters from many different languages and different alphabets, Public class CharDemo { Public static void main(String args[]) { char c=1,c=2; c1=88; //ASCII and unicode value of X is 88. C2= Y ; System.out.println( c1 and c2 : +c1+ +c2); C1++; System.out.println( c1 is now : +c1); } }

O/P = c1 and c2 : X Y y And a variable of type boolean holds one of the two logical values true or false. Public class BoolDemo { Public static void main (String args[]) { boolean b; b=false; System.out.println( b is : +b); b=true; if(b) System.out.println( This is executed ); System.out.println( 10>9 is : +(10>9)); } } O/P = b is : false This is executed 10>9 is : true CONSTANTS (LITERAL): y Refers to fix value that doesnot change during program execution.

 Exp: 20- integer constant, a - char constant, hello - string constant, 3.14- floating point constant. Public class bank { Static double interestRate; interestRate = 0.05; // ILLEGAL; } y If no initial value is provided for a member variable, then a default initial value is used. For example, static int count: is equivalent to static int count = 0 ; NAMED CONSTANTS AND THE FINAL MODIFIER y To declare a constant in java, the final modifier is used. Ex. Final static double interestRate = 0.05; y It is legal to apply the final modifier to local variables and formal parameters, but it is most useful for member variables. Final static double INTEREST_RATE = 0.05 y Forexample, the Math class defines a named constant PI to represent the mathematical constant of that name. Since it is a member of the math class, you would have to refer to it as Math.PI in your own programs. VARIABLES AND STORAGE CLASSES :y The storage class of a variable determines  Which part or programcan access it and (Scope)  How long it stays in existence (Life Time) AUTOMATIC VARIABLES (LOCAL): y Variables defined within a function body or block are called automatic variables. y The name automatic is used because the variables are automatically created when a function is called and automatically destroyed when it returns. y The scope of automatic variable is within the function where they are defined. y It initialized with any arbitrary values,which may be 0. But probably will be something else. y Automatic variable is stored in stack segment. MEMBER VARIABLES: y Member variables are defined outside any function but within a class. STATIC VARIABLES: y Scope of static automatic variable is a function where defined,  But it remains in existence for the life of the program.  It automatically initializesto 0.

 They are not reinitialized each time when the function is called, unlike ordinary automatic variables. y Scope of static member variable entire program. y It is legal to have a local variable or a formal parameter that has the same name as a member variable. In that case, within the scope of the local variable or parameter, the member variable is hidden. Public class game { Static int count; Static void playGame() { int count=0; // local variables count ++; System.out.println( Local variable count value : +count); Game.count+=2; System.out.println( Member variable count value : +Game.count); } Public static void main(String args[]) { Game.playGame(); } } O/P = Local variable count value : 1 Member variable count value : 2 y It is not legal to redefine the name of a formal parameter or local variable within its scope, even in a nested block. For examole, this is not allowed: Void badSub(int y) { Int x; While (y>0) { Int x; // ERROR: x is already defined. } } Void goodSub(int y) { While(y>10) { Int x; // the scope of x ends here }

While(y>0) { Int x; } }

//OK: Previous decleration of x has expired.

y You could legaly have a class called Insanity in which you declare a function Static Insanity Insanity( Insanity Insanity ) { } 1. The first Insanity is the return type of function. 2. The second is the function name. 3. The third is the type of the formal parameter. 4. And the fourth is a formal parameter name. 5. However, please remember that not everything that is possible is a good idea. WRAPPER CLASSE: Primitive data types may be converted into object types using wrapper classes contained in java.long packages.

Java.long.Double * Double (double)() * double double Value()() * String toString(double)() * Double valueOf(String)()

Java.long.Integer * Integer(int)() * int int Value()() * String toString(int)() * Integer valueOf(String)() * Integer parselnt(String)()

<< abstract >> Java.long.Number

Java.long.Float * float(float)() * float float value()() * String toString(float)() * Float value of(String)()

Jova.lang.Byte * Byte(byte)()

Jova.lang.Byte * Short(Short)()

Java.long.Lang * Long(long)() * long long value()() * String toString(long)() * Long valueof(String)() * Long parseLong(String)()

Public class InfiniteNaN { Public static void main(String args[]) { Double d1= new Double (1/0); Double d2= new Double (0/0); System.out.println(d1+ : +d1.isInfinite()+ is Not a Number: +d1.isNaN()); System.out.println(d2+ : +d2.isInfinite()+ is Not a Number: +d2.isNaN()); } } O/P = Infinity : true is Not a Number : false NaN : false is Not a Number: true Public class NumberStringConversion { Public static void main (String args[]) { String str = 2345 ; Int num = Intger.parselnt(str); System.out.println( Number : +num ); System.out.println(num + in binary : +Integer.toBinaryString(num)); System.out.println( num + in octal : +Integer.toOctalString(num); System.out.println(num + in hex : +Integer.toHexstring(num)); } } O/P = Number : 2345 2345 in binary : 100100101001 2345 in octal : 4451 2345 in hex : 929 TYPE CONVERSION AND CASTING: Public class Conversion { Public static void main(String args[]) { byte b; int i=257; doule d=323.142; b=(byte)i;

System.out.println( i and b +i+ +b); i= (int)d; System.out.println( d and i +d+ +i ); b=(byte)d; System.out.println( d and b +d+ +b); } } O/P = i and b 257 1 d and i 323.142 323 ARITHMETIC TYPE PROMOTION IN EXPREESSIONS: byte a = 40; byte b=50; int c=a*b; // java automatically promotes each byte or short to int when evaluating an expreession, because a*b exceeds range of byte operand. byte b=50; b=b*2; //error , cannot assign int to a byte, because byte is promoted to int. TYPE PROMOTION RULES: y Bytes and shorts to int. y If one operand is long, whole expreession is promoted to long. y If one operand is float the entire exp is float.

You might also like