java ppt2

11
Java Tokens: Keywords, Identifiers, Literals, Operators, and Seperators

Upload: nikhilsh66131

Post on 18-Aug-2015

1 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Java ppt2

Java Tokens: Keywords, Identifiers, Literals, Operators, and Seperators

Page 2: Java ppt2

A smallest individual unit in source code is known as Token

Java Tokens

Keywords Identifiers Literals Operators Seperators

Page 3: Java ppt2

Keywords are special tokens in the language which have reserved use in the language.Keywords may not be used as identifiers in Java — you cannot declare a field whose name is akeyword, for instance.Java has 50 keywords among which 48 are in use but 2 (goto and const) are currently not in useHere is a list of keywords in the Java programming language. You cannot use any of thefollowing as identifiers in your programs. The keywords const and goto are reserved, eventhough they are not currently used. true, false, and null might seem like keywords, but they areactually literals; you cannot use them as identifiers in your programs.

abstract continue for new switchassert*** default goto* package synchronizedboolean do if private thisbreak double implements protected throwbyte else import public throwscase enum**** instanceof return transientcatch extends int short trychar final interface static voidclass finally long strictfp** volatileconst* float native super while

* not used

** added in 1.2

*** added in 1.4

**** added in 5.0

Page 4: Java ppt2

Identifiers are the names of variables, methods, classes, packages and interfaces.

• Don’t use a keyword• Don’t use a space• Don’t use a special symbol other than ( _ and $ )• Cannot start with digit• Any length

• If only a single word then use a lowercase• If more than one word then start first word with lowercase and other words start with first

letter as CapitalFor example:int balance=500; //Single wordint bankBalance=500; //Multiple Words

Page 5: Java ppt2

• Use lowercaseFor example:package pack1;package mypack1;

• Every word start with first letter as CapitalFor example:class Balance{}class BankBalance{}

• Use Uppercase and Underscore for separating the wordsFor example:int PI=3.14;int MAX_VALUE=100;

Page 6: Java ppt2

Fixed value assigned to variables or used in expressions is known as Literals

• Integer Literals: for example: 1, -2, 8523, -2105 etc.

Data Type Size Range

byte 1 Byte -27 to 27 -1

short 2 Bytes -215 to 215 -1

int 4 Bytes -231 to 231 -1

long 8 Bytes -263 to 263 -1

• Floating Literals: for example: 1.02, -2.689, 8523.369, 2105 etc.

Data Type Size Range

float 4 Byte -3.4 * 1038 to 3.4 * 1038

double 8 Bytes -1.7 * 10308 to 1.7 * 10308

Page 7: Java ppt2

• Character Literals: for example: ‘A’, ‘B’, ‘@’, ‘&’ etc.

Data Type Size Range

char 2 Bytes All Unicode characters

• String Literals: for example: “Hello”, “Gurpreet Singh”, “ACET” etc.

Data Type Size Range

String 2 * No. of characters As much memory available

• Boolean Literals: true and false.

Data Type Size Range

boolean 1 Byte true or false

• Null Literals: null can be assigned to any reference type

Page 8: Java ppt2

Operators operates on operands. For example: In expression a+b , a & b are operands and +is operator

• Arithmetic Operator: +, - , *, /, %. For example:5+4 will result in 9 5-4 will result in 1 5*4 will result in 20 5/4 will result in 1

5%4 will result in 1 (remainder) -5%4 will result in -1 5%-4 will result in 1 -5%-4 will result in -1

Note: Sign of result in % operator depends on the sign of dividend(numerator)

• Relational Operator: <, >, <=, >=, ==, != Output will be either true or false5>6 will result in false5>=6 will result in false (either greater than or equal to)5<6 will result in true5<=6 will result in true (either less than or equal to)5==6 will result in false (equal to)5!=6 will result in true (not equal to)

Page 9: Java ppt2

• Assignment Operator: = Assign the value on left hand side to right hand side. For example:A=10+5, will assign 15 to A

• Logical Operator: returns true or falseLogical AND (&&)Logical OR (||)Logical NOT (!)

A B A&&B A||B !A

true true true true false

true false false true false

false true false true true

false false false false true

• Increment/Decrement Operator: increases or decreases the value by 1Pre-increment & Pre-decrement: ++a, --aPost-increment & Post-decrement: a++, a--

Page 10: Java ppt2

• Bitwise Operator: Performs operations on bitsBitwise AND(&)Bitwise OR(|)Bitwise XOR(^)Bitwise Complement or Not (!)

A B A&B A|B A^B !A

0 0 0 0 1 1

0 1 0 1 0 1

1 0 0 1 0 0

1 1 1 1 1 0

• Shift Operator: shift the bits to left or rightLeft Shift(<<): shifts the bits to left and fill the vacant spaces with 0Unsigned Right Shift(>>>): shifts the bits to right and fill the vacant spaces with 0Signed Right Shift(>>): shifts the bits to right and fill the vacant spaces with leftmost bit, i.e. ifleftmost bit is 0 then fill with 0, and if the leftmost bit is 1 then fill with 1

• Conditional(Ternary) Operator:Syntax: condition(relational expression) ? Expression 1 : Expression 2If condition evaluates to true then result will be Expression 1 otherwise Expression 2

Page 11: Java ppt2

{ } used to define block or to declare array with static elements[ ] used to declare array variable( ) used to define/call function or used in expressions; used to terminate a statement, used to separate variables in declaration. used to access data members and member functions of an object or class