Java Token, Constants and Variables

Published by StudyMuch on

studymuch.in Java Variables

Java Token, Constants and Variables

In this tutorial you will learn about the Java Token, Java Constant & Java Variables, you also learn about types of Tokens like Keywords, Identifiers, Operator, Literal and Separator. And learn types of Constants and Backslash Character of Constants. Here you cover all the topic given above with definition in details.

Java Token

A Token is a smallest individual or basic unit of program are knowns as Token. The compiler recognizes them for building up expressions and statements. In simple terms, a Java programs is a collection of Tokens, Comments and white space.

Java languages includes five types of tokens, that are classified as: –

  1. Keywords
  2. Identifiers
  3. Operators
  4. Literals
  5. Separators

Now, you learn one by one all the Types of Tokens in details.

Keywords

The words which have predefined meaning in Java are knows as Keywords. They are reserved for specific purpose in Java so, they are also called as Reserved Words.

Keywords are an essential part of a language definition. The keywords are combined with operators and separators according to a syntax. Keywords have specific meaning in Java, we cannot use them as name for variables, classes, and methods, all keywords are written in lower-case letter. Java have reserved 50 important keywords that are given below in table.

Java Keywords

byte

abstract

assert

catch

class

extends

case

else

do

instanceof

const

finally

for

protected

double

if

import

synchronized

final

int

long

transient

goto

new

short

boolean

native

public

switch

continue

static

strictfp

throws

implements

while

this

volatile

interface

private

try

return

package

enum

break

super

throw

float

char

void

default

Identifiers

Identifiers are programmer designed tokens. They are used for naming classes, methods, variables, objects labels, interface and packages in a program.

Java Identifiers follow the Rules, are given below.

  • Uppercase and lowercase letters are distinct.
  • They can be of any length.
  • They must not begin with a digit.
  • They can have alphabets, digits, underscore and doller sign characters.

Operators

An operator is a symbol that specify the operations or activity to be performed. That takes one or more arguments and operates on them to produce a result. Operators are many types in java and this is explained in detail, in Java Operator Tutorialyou can visit and learn Operators.

Separators

Separators are symbols used to indicate where groups of code are divided and arranged. They basically define the shape and function of our code. Given below in Table, list of separators and their functions.

Name

Used/Functions

Braces { } Braces is used to contain the value of automatically initialized array and to define a block of code for classes, methods and local scopes.
Parentheses ( ) It is used to enclose parameter in method definition and invocation, also used for defining precedence in expressions, containing, for flow control and surrounding cast types.
Bracket [ ] It is used to declare array and for dereferencing array values.
Comma , It is used to separate consecutive identifiers in a variable declaration, also used to chain statements together inside ‘for’ statement.
Period . It is used to separate package names from sub-packages and classes; also used to separate a variable or method from a reference variable.
Semicolon ; Semicolon is used to separate statements.

Literal

Literal in Java is a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variable. Java language specifies following types of literals are given below.

  • Integer literal
  • Floating point literal
  • String literal
  • Character literal
  • Null literal
  • Boolean literal

Now you will learn Constant and Variable in detail with types of constant and his examples also.

Java Token, Constant and Variable studymuch.in

Constant

Constants in Java refers to fixed value that do not change during the execution of a program. Java supports several types of constants are as given below in detail. You learn all the types of Constant one by one.

Integer Constant

An integer constant refers to a sequence of digits. there are three types of integers, namely, Decimal Integer, Octal Integer and Hexadecimal Integer.

Decimal integer consists of a set digits 0 to 9, preceded by an optical minus sign, valid examples of decimal integer are given below;

123 -432 0 4578496

An Octal integer constant consists of any combination of digit from the set 0 through 7, with a leading zero, some examples of octal integer are given below.

035    0 435    0881

Hexadecimal Integer is also called as Hex Integer. A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets letter from A through F represents the number 10 through 15. Following example are given below.

0A2    0BXF 2xbcf 0x

Real Constants

Integer number are inadequate to represent quantities that vary continuously, such as distance, heigh, temperature price etc. These quantities are represented by numbers containing fictional parts like 14.249. Such numbers are called real constant or (floating point constant).

Following examples of real constant are given below.

0.2135    .85    -.78

Single Character Constant

A single character constant or (simply character constant) contains a character enclosed within a pair of single quote marks. Examples of single character constant are given below.

‘5’    ‘X’    ‘:’    ‘A’

String Constants

A String Constant is a sequence of characters enclosed between double quotes. The characters may be alphabets, digits, special characters and blank spaces. Given below are the examples of string constant.

“Shubham”        “Java”    “1992”     “5+7”  

Java Token, variable studymuch.in

Backslash Character Constants

Java supports some special backslash character constant, that are used in output methods. List of such backslash character constant is given below in table.

Backslash Character constants in Java

Constant Meaning
‘\f’

Form feed

‘\b’

Back space

‘\n’

New line

‘\r’

Carriage return

‘\t’

Horizontal line

‘\”‘

Double quote

‘\”

Single quote

‘\\’

Backslash

Variables

A variable is an identifier that denotes a storage location use to store a data value. Unlike constants that remain unchanged during the execution of program. A variable may take different values at different times during the execution of the program.

A variable name can be chosen by programmer in a meaningful way so as to reflect what it represents in the program. Some examples of variables are given below.

  • studymuch
  • average
  • total_calss
  • study_now_studymuch
  • mycollege

Mentioned above variable, variable mist may consists of alphabets, digits, the underscore ( _ ) and doller characters.

Following Rules of creating Variable

  • It should not be a Keyword.
  • White space is not allowed.
  • Variable names can be of any length.
  • They must not begin with a digit.
  • Uppercase and lowercase are distinct. That means the variable StudyMuch is not same as studymuch or STUDYMUCH.

So, in this tutorialJava Token, Constants and Variables” we have discussed Java Token, types of Tokens like Keywords, Identifiers, Literal, Operator and Separators we also discussed here Constants, Variables and types of constants with examples and also see backslash character constant. I hope you learned this tutorial well and learned something new.

Read Also –


3 Comments

Etta Beckim · September 23, 2022 at 8:32 am

Nice blog here! Also your website loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol

marizonilogert · October 10, 2022 at 2:05 am

This web site is my aspiration, real excellent layout and perfect articles.

devisings · December 14, 2022 at 1:16 am

Veгy ԛuickⅼу this website will be fаmous amid all
blogging vieweгs, due to it’s fastidious content

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *