[go: up one dir, main page]

0% found this document useful (0 votes)
44 views11 pages

Tokens

The document discusses a simple Java program that prints "Hello Java" and explains the key components of the program. It contains a class with a main method that uses System.out.println() to output text. The document then provides details on tokens in Java, including keywords, identifiers, literals, operators, and their meanings and uses.

Uploaded by

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

Tokens

The document discusses a simple Java program that prints "Hello Java" and explains the key components of the program. It contains a class with a main method that uses System.out.println() to output text. The document then provides details on tokens in Java, including keywords, identifiers, literals, operators, and their meanings and uses.

Uploaded by

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

Simple java program:

class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}

Parameters used in First Java Program


Meaning of class, public, static, void, main, String[], System.out.println().

o class keyword is used to declare a class in Java.


o public keyword is an access modifier that represents visibility. It means it is
visible to all.
o static is a keyword. If we declare any method as static, it is known as the static
method. The core advantage of the static method is that there is no need to
create an object to invoke the static method. The main() method is executed by
the JVM, so it doesn't require creating an object to invoke the main() method. So,
it saves memory.
o void is the return type of the method. It means it doesn't return any value.
o main represents the starting point of the program.
o String[] args or String args[] is used for command line argument. We will
discuss it in coming section.
o System.out.println() is used to print statement. Here, System is a class, out is an
object of the PrintStream class, println() is a method of the PrintStream class. We
will discuss the internal working of System.out.println() statement in the coming
section.
Tokens
 The tokens are the small building blocks of a Java program that are meaningful to
the Java compiler.
 The Java compiler uses it for constructing expressions and statements.
 These tokens are separated by the delimiters.

Types of java tokens:

Java token includes the following:

o Keywords
o Identifiers
o Literals
o Operators
o Separators

Keywords:

 These are the pre-defined reserved words of any programming language.


 Each keyword has a special meaning.
 It is always written in lower case.

Java provides the following keywords:

01. abstract 02. boolean 03. byte 04. break 05. class

06. case 07. catch 08. char 09. continue 10. default

11. do 12. double 13. else 14. extends 15. final

16. finally 17. float 18. for 19. if 20. implements


21. import 22. instanceof 23. int 24. interface 25. long

26. native 27. new 28. package 29. private 30. protected

31. public 32. return 33. short 34. static 35. super

36. switch 37. synchronized 38. this 39. thro 40. throws

41. transient 42. try 43. void 44. volatile 45. while

46. assert 47. const 48. enum 49. goto 50. strictfp

Identifier:

 Identifiers are used to name a variable, constant, function, class, label, and array.
 It usually defined by the user.
 It uses letters, underscores, or a dollar sign as the first character.

There are some rules to declare identifiers are:

o The first letter of an identifier must be a letter, underscore or a dollar sign. It


cannot start with digits but may contain digits.
o The whitespace cannot be included in the identifier.
o Identifiers are case sensitive.
o Reserved Words can’t be used as an identifier.

Examples:

1. PhoneNumber
2. PRICE
3. phonenumber
4. $circumference
5. 12radius //invalid

Literals

 literals are the constant values that appear directly in the program
 It can be assigned directly to a variable.
The following figure represents a literal.

Types of Literals in Java


There are the majorly four types of literals in Java:

1. Integer Literal
2. Character Literal
3. Boolean Literal
4. String Literal

Integer Literals
 Integer literals are sequences of digits.
There are four types of integer literals:

 Decimal Integer
 Octal Integer
 Hexa-Decimal
 Binary Integer

Decimal integer:
These are the set of numbers that consist of digits from 0 to 9. It may have a positive (+) or
negative (-).
For example, 5678, +657, -89, etc.

int decval = 26;

Octal Integer:
It is combinations of number have digits from 0 to 7 with a leading 0.
For example, 045, 026,

int octVal = 067;

Hexa-Decimal:

The sequence of digits preceded by 0x or 0X is considered as hexadecimal integers. It may also


include digits from 0 to 9 and a character from a to f or A to F that represents numbers
from 10 to 15, respectively. For example, 0xd, 0xf.

int hexVal = 0x1a;

Binary Integer:
Base 2, whose digits consists of the numbers 0 and 1.
Prefix 0b represents the Binary system.

For example: 0b11010.

Character Literals:
A character literal is expressed as a character or an escape sequence, enclosed in a single quote
('') mark.

For example, 'a', '%', '\u000d', etc.

Boolean Literals:
Boolean literals represent the logical value of either true or false.
Boolean literals can also use the values of “0” and “1.”

Examples:
boolean b = true;
boolean d = false;

String Literals:
String literals are sequences of characters enclosed between double quote ("") marks. These
characters can be alphanumeric, special characters, blank spaces, etc.

Examples: "John", "2468", "\n", etc.

Operators:
Operators are special symbols that perform specific operations on one, two, or three operands,
and then return a result.

Different type of operators:


Operators in Java can be classified into 6 types:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Bitwise Operators

1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and values.
Operator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

Modulo
Operation
%
(Remainder
after division)

2. Assignment Operators
Assignment operators are used in Java to assign values to variables.

Operator Example Equivalent to

= a = b; a = b;

+= a += b; a = a + b;

-= a -= b; a = a - b;

*= a *= b; a = a * b;

/= a /= b; a = a / b;

%= a %= b; a = a % b;
3. Relational Operators
Relational operators are used to check the relationship between two operands.

Operator Description Example

== Is Equal To 3 == 5 returns false

!= Not Equal To 3 != 5 returns true

> Greater Than 3 > 5 returns false

< Less Than 3 < 5 returns true

>= Greater Than or Equal To 3 >= 5 returns false

<= Less Than or Equal To 3 <= 5 returns true

4. Logical Operators
Logical operators are used to check whether an expression is true or false.

Operator Example Meaning

&& (Logical expression1 && true only if both expression1 and expression2 are
AND) expression2 true

|| (Logical OR) expression1 || expression2 true if either expression1 or expression2 is true

! (Logical NOT) !expression true if expression is false and vice versa

5.Unary Operators
Unary operators are used with only one operand.

Operator Meaning

+ Unary plus: not necessary to use since numbers are positive without using it

- Unary minus: inverts the sign of an expression

++ Increment operator: increments value by 1

-- Decrement operator: decrements value by 1

! Logical complement operator: inverts the value of a boolean

6. Bitwise Operators
Bitwise operators in Java are used to perform operations on individual bits.

Operator Description

~ Bitwise Complement

<< Left Shift

>> Right Shift

>>> Unsigned Right Shift

& Bitwise AND

^ Bitwise exclusive OR

Java instanceof Operator


The instanceof operator checks whether an object is an instanceof a particular class.
Ex:
String str=”Java Programming”;
beelean val;

val=str instanceof String;

Separators:
Separators help us defining the structure of a program .

Symbol Name Purpose

 list of parameters in method definition


 Method invocation.
 Defining precedence in expressions in control statements.
() Parentheses  surrounding cast types

 Define a block of code, for classes, methods and local


scopes.
{} Braces  the values automatically initialised to array.

 declares array types


[] Brackets  dereferencing array values

; Semicolon  Terminates statements

 Separates package names from sub-package and class


names.
. Period  selects a field or method from an object

 Separates consecutive identifiers in variable declarations.


 also used to chains statements in the test, expression of a for
, Comma loop

: Colon  Used after labels

You might also like