Chapter 7: Tokens In Java

 Chapter 7: Tokens In Java

Let's begin to actually learn how to make a program, aye!
A Java program usually comprises of tokens, operators, literals, identifiers etc.

Tokens are the smallest unit in a Java program. That's the literal meaning.
Tokens help in telling the compiler that something is beginning, or an act of storing information, or processing information is taking place.
For a better understanding let us skip to the good part-

1) Keywords: These are type of tokens (basically words which indicate different things in a program).
For example, the keyword class is used to tell the computer that a class is being defined. Similarly there are many keywords in java which hold special meaning and cannot be used for any other purpose.
All keywords and their function is listed at https://www.javatpoint.com/java-keywords

2) Identifiers or Variables: Ever thought how a calculator on your computer works? If not then what are you doing with your lif-. Anyways. When you perform tasks on your computer, a memory location is opened in the computer's memory (which you may have heard as RAM). 
    So in java, when you are storing or using values, you use variables to store them. Variables need to have a name, such that a memory location with that name is opened in  the computer's memory and can be accessed later.
    However, what if we try to make two memory locations with the same name and try to fool the computer. Well, computer is designed not to allow you to do this. Hence, more than one variable can never have the same name. 
    Variable are case sensitive- which means, variable_A and Variable_A are two different variables.
    Variables cannot have special characters (except _ ), don't ask me why, else....
    Variables cannot begin with a number.
    Variables cannot be a keyword as discussed earlier.
 
NOW LET ME TEACH YOU SOME MANNERS, AYE.

Variable Naming Conventions In Programming - (Everyone should learn some manners)
Ok so there are certain prescriptions to write variable names to make it easier among the whole community.
The naming conventions include-
Pascal Convention: It is used for naming classes. Example: CalculateSumClass 
In PascalConvention the first letter of each word is capital and rest are small.

camelCase: It is used for naming variables. Example: thisIsANumberVariable
In camelCase the first letter of the first word is small, and first letters of all other words are capital.

First letter of the word convention 
Please Please Please have some sense in naming single lettered variables. 
For example, if you are storing a number, let the variable be n1, n2, n3, n etc.
If you're storing a string, s1, s2, s3, s etc
If you're storing a variable which stores factors, f1, f2, f3

Please use the first, or the starting letters followed by some numbers to use variables which are not full names but short forms of what you are storing. 

3) Literals/Constants As the word itself says, they are values which are fixed (Constant) and do not change during the execution of the program. For example: 0.1, 2, 3455.

4) Operators:
They are the symbols used to specify what operation is to be performed. For example the symbol + denotes addition, / denotes division, * denotes multiplication.
They are of three types:
MATHEMATICAL: + - / * %
RELATIONAL: > < >= <= == !=
LOGICAL: &&(AND) ||(OR) !(NOT)

BEMDAS RULE: Bracket (1) Exponent (2) Multiplication (3) Division (3) Addition (4) Subtraction (4)
The numbers given in brackets are priority from 1 (Highest Priority) to 4 (Lowest Priority) means first all brackets will be solved, then all exponents will be solves, then multiplication/division will be solved as per they are used, and lastly addition and subtraction will be performed.
Example: 10+20-(10/2*5)
Output: 5

Shorthand Operators
More like shortcut operators lol
They are += -= *= /= %=
a += b means a = a + b
a -= b means a = a - b
a *= b means a = a * b
a /= means a = a / b
a %= means a = a % b 

5) Separators
;   used to terminate a statement
,    separate two variables
.    scope of function (used to access functions, packages, objects etc)
[ ] define an array (arrays will be explained later)
( ) define a function
{ } define a program segment



Comments

Popular posts from this blog

Chapter 10: Divide and Conquer

Chapter 1: Introduction To Java

Resources