0% found this document useful (0 votes)
62 views

1b. Explain The Characteristics of The Given Java Token.

This study material covers Java programming tokens, identifiers, data types, variables, and operators. Key topics include the definition of a token as the smallest element of a Java program, rules for defining identifiers, primitive and non-primitive data types and their default sizes, and an overview of variable types in Java. Concepts are explained with examples and diagrams.

Uploaded by

Hemant Thawani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

1b. Explain The Characteristics of The Given Java Token.

This study material covers Java programming tokens, identifiers, data types, variables, and operators. Key topics include the definition of a token as the smallest element of a Java program, rules for defining identifiers, primitive and non-primitive data types and their default sizes, and an overview of variable types in Java. Concepts are explained with examples and diagrams.

Uploaded by

Hemant Thawani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Template: Study Material

Insert the details within the < quotes >


<CO1>: <22412>: <Java Programming>: <Basic Syntactical Constructs in Java>: <LO2>: <Study Material>

<Yogita Jore> <01/02/2021> <Vijay Patil>

Key words Learning Objective: Diagram/ Picture


Nesting of Methods and This student should understand characteristics of the given Java
Keyword Token.
Smallest individual units in a
program are known as
tokens.
Key Questions Concept Map
Rules to define identifier:
1) All identifiers must start
with either a letter (a to z or A
to Z) or currency character ($)
Identify the identifier from or an underscore.
following program. 2) They must not begin with a
digit
3) After the first character, an
identifier can have any
combination of characters.
4) A Java keyword cannot be
used as an identifier.
Explanation of Concept 5) Identifiers in Java are case
sensitive, foo and Foo are two
Java Token different identifiers.
Ans: 6) They can be any length
A token is the smallest element of a program that is
meaningful to the compiler. Simply we can say that java Constant Type of
program is also collection of tokens, comments and Value
whitespaces. Stored

A class is defined by a set of declaration statements and Integer Literal Literals


methods containing executable statements. Most statements s which
contain expressions, which describe the actions carried out stores
on data. The word formed from the character set is building integer
block of java and are known as token. These tokens value
represent the individual entity of language.
Floating Literals
Literals which
stores
float
value

Character Literals
Literals which
stores
character
value

String Literals Literals


which
stores
string
value

Boolean Liter Literals
als which
stores tru
e or false
Key Definitions/ Formulas
Constructors are used to
Token Meaning Example assign initial values to instance
variables of the class.
Keyw A variable int distance;
ord is a continue There are many types of
meaningfu break operators in java which are
l name of public given below:
data return  Unary Operator,
storage do
location in while  Arithmetic Operator,
computer for  shift Operator,
memory.  Relational Operator,
When  Bitwise Operator,
using a  Logical Operator,
variable
you refer
 Ternary Operator
to memory and
address of  Assignment
computer. Operator

Const Constants final int a=20;


ant are
expression
s with a
fixed
value.
They are
also called
as literals
Syntax:
final
data_type
variable_n
ame;

Identi Identifiers MYVARIABLE


fier are the x
names of i
variables, _myvariable
methods, $myvariable
classes, _9pins
packages andros
and ??????
interfaces.

String Sequence char[] ch={'a','t','n','y'


of ,'l','a'};
characters. String s=new String(
ch);

Speci Symbols [] () {}, ; * =


al other than
Data Types:
Data types specify the different sizes and values that can be
stored in the variable.

Data types specify the different


sizes and values that can be
stored in the variable.

Java has two categories of data: 


Primitive Data Type: such as boolean, char, int, short,
byte, long, float, and double
Non-Primitive Data Type or Object Data type: such as
String, Array, etc.

Data Default Value Default size


Type

boolea False/true 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte


Variables: Variable is name of reserved
A variable is a container which holds the value while the Java area allocated in memory.
program is executed. A variable is assigned with a data type. In other words, it is a name of
Variable is a name of memory location. There are three types memory location. It is a
of variables in java: local, instance and static. combination of "vary + able"
that means its value can be
changed.
Rules for naming variables

 The name of a variable needs to be meaningful,


short and without any embedded space or symbol
like ? ! @ # % ^ & * ( ) [ ] { } . , ; : “ ‘ / and \.
However underscore can be used wherever a space
is required for example basic_salary.
 Variable name must be unique. For example to
store four different numbers, four unique variable
names need to be used.
 A variable name must begin with a letter, a dollar
symbol (‘$’) or an underscore (‘_’) , which may be
followed by a sequence of letters or digits (0-9), ‘$’
or ‘_’.
 Keywords cannot be used for variable names. For
example, you cannot declare a variable called
switch.
 Variable names must be meaningful. The names
must reflect the data that the variables contain.
Example to store the age of an employee, the
variable name could be employeeage.
 Variable names are nouns and begin with a
lowercase letter.
 If a variable name contains two or more words, join
the words and begin each word with an uppercase
letter. The first word, however, starts with a
lowercase letter.
 Valid variable names: address1, studentname,
total_salary
 Invalid variable names: $salary, 1stname

How to declare variables?


Syntax: datatype var1, var2, var3, ………., varn;

The declaration should be terminated by semicolon (;)


whereas all the variable names of same datatype are
separated by commas;

Example:

float simpleInterest; //Declaring float variable


int time = 10, speed = 20; //Declaring and Initializing
integer variable
char var = 'h'; // Declaring and Initializing character variable

Types of variables/scope of variable:


There are three types of variables in Java:

 Local Variables
 Instance Variables
 Static Variables

Local Variables: A variable defined within a block or


method or constructor is called local variable.
 These variable are created when the block in entered or
the function is called and destroyed after exiting from
the block or when the call returns from the function.
 The scope of these variables exists only within the
block in which the variable is declared. i.e. we can
access these variable only within that block.
 Initialization of Local Variable is Mandatory.

Instance Variables: Instance variables are non-static


variables and are declared in a class outside any method,
constructor or block.
 As instance variables are declared in a class, these
variables are created when an object of the class is
created and destroyed when the object is destroyed.
 Unlike local variables, we may use access specifiers for
instance variables. If we do not specify any access
specifier, then the default access specifier will be used.
 Initialization of Instance Variable is not Mandatory. Its
default value is 0
 Instance Variable can be accessed only by creating
objects.

Static Variables: Static variables are also known as Class


variables.
 These variables are declared similarly as instance
variables; the difference is that static variables are
declared using the static keyword within a class outside
any method constructor or block.
 Unlike instance variables, we can only have one copy
of a static variable per class irrespective of how many
objects we create.
 Static variables are created at the start of program
execution and destroyed automatically when execution
ends.
 Initialization of Static Variable is not Mandatory. Its
default value is 0
 If we access the static variable like Instance variable
(through an object), the compiler will show the warning
message and it won’t halt the program. The compiler
will replace the object name to class name
automatically.
 If we access the static variable without the class name,
Compiler will automatically append the class name .

Instance variable Vs Static variable


 Each object will have its own copy of instance variable
whereas We can only have one copy of a static variable
per class irrespective of how many objects we create.
 Changes made in an instance variable using one object
will not be reflected in other objects as each object has
its own copy of instance variable. In case of static,
changes will be reflected in other objects as static
variables are common to all object of a class.
 We can access instance variables through object
references and Static Variables can be
accessed directly using class name.
Dynamic initialization:
Initialization is the process of providing value to a variable at
declaration time. A variable is initialized once in its life time.
Any attempt of setting a variable's value after its declaration
is called assignment. To use a local variable, you have to
either initialize or assign it before the variable is first used.
But for class members, the compulsion is not so strict. If you
don't initialize them then compiler takes care of the
initialization process and set class members to default values.
Java allows its programmers to initialize a variable at run
time also. Initializing a variable at run time is called dynamic
initialization. Java allows variables to be initialized
dynamically, using any expression valid at the time the
variable is declared.
Example:
public class MainClass
{
public static void main(String args[])
{
double a = 3.0, b = 4.0;
 // c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}

Array:
An array is a collection of similar types of data.
For example, if we want to store the names of 100 people
then we can create an array of the string type that can store
100 names.

Syntax: datatype[ ] arrayname;


Example: double[ ] percentage;

In Java, we can initialize arrays during declaration.


For example,
int[ ] age={12,4,5,2,5};

String:
In Java, string is basically an object that represents sequence
of char values. An array of characters works same as Java
string. For example:
char[] ch={‘M’,’S’,’B’,’T’,’E’};
String s=new String(ch);

Java String class provides a lot of methods to perform


operations on strings such as compare(), concat(), equals(),
split(), length(), replace(), compareTo(), intern(), substring()
etc.

Application of Concept/ Examples in real life:


 Used to declare variables
 Assigned to values to variables and constants

Key Take away from this LO:

Concept of java tokens, data types, constants, type casting, variables

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy