0% found this document useful (0 votes)
8 views5 pages

Java Theory Tokens and Data Types

Java theory

Uploaded by

Ishan Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
8 views5 pages

Java Theory Tokens and Data Types

Java theory

Uploaded by

Ishan Jadhav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Tokens & Data Types

1. What is token? Give example of 2 types of token?


Ans. The smallest individual entity used in the program is known as java token. When we
submit a Java program to the Java compiler, the compiler parses the text and extracts individual
tokens
Types of token:
i. Keyword
ii. Identifiers
iii. Literals
iv. Operators
v. Separator (or Punctuators)

1. What are keywords? Give an example.


Ans. Keywords are reserved words which convey special meaning to the compiler. They are
written in small letters. They cannot be used as names for identifiers, class or methods. E.g.:
class, void
abstrac double int switch char for protecte
t d
assert else interfac synchronize class goto public
e d
boolea extend long This const if return
n s
break false native Throw continu implement short
e s
byte final new transient default import static
case finally package True do instanceof super
catch float private Try void volatile while

1. What are identifiers in Java?


Ans.
Identifiers are tokens that represent names. These names can be assigned to variables,
methods, and classes to uniquely identify them to the compiler. Variables are declared with a
data type. E.g. n,x,val3,_age, $val etc,

A Java Identifier must have following characteristics

1
 Can consist of uppercase, lowercase, digits, $ sign and underscore (_) character (NO
SPACE)
 Begins with letter, $, or _ (NO NUMBER)
 Is case sensitive
 Cannot be a keyword
 Can be of any length

2. What are literals in Java?


Ans. Literals or constants are tokens which do not change its value during the execution of
the program.
There are 5 types of Literals
 Integer literal
E.g int WEEK = 7, color = 0x24, code = 0b0100, v = 10L;
 Floating Point literal – It is of type double and float
E.g. double d = 23.2, float PI = 3.14F;
 Boolean literal – It has 2 values true and false
E.g. boolean STATE = true;
 Character literal - It is always enclosed in single quotes
E.g char ANS = ‘Y’, A = 065, ch = ‘\n’;
 String literal - It is always enclosed in double quotes
E.g String CAPITAL = “New Delhi”;

3. What is difference between float literal and double literal?


Ans.
Float Literal double Literal
1. The float literal is a single-precision 1. The double literal is a double-
32-bit (4 bytes) floating point literal precision 64-bit (8 bytes) floating
that can store any number along point literal that can store any
with fractional part number along with fractional part.
2. It can handle 7 decimal places. 2. It can handle16 decimal places.

1. What do you mean by Punctuators? Give example?


Ans. Punctuators are also called as separators. They are specific symbols or tokens used to
indicate how group of code is divided and arranged.
Examples:
[ ] (used for creating array),
{} (used to make compound statement body),
() (used to perform arithmetic operations, used to enclose arguments of the
function, for type casting),

2
; (semicolon- used as statement terminator),
, (comma- used to separate list of variables),
. (dot or decimal- used make fraction value, include package, referencing
object).

2. What is the difference between Static and Dynamic Initialization?


Ans

STATIC Initialization: Variable is initialized during its declaration with defined constant.
E.g. int i = 10;
DYNAMIC Initialization: Variable is initialized at run time, i.e. during execution.
e.g. i = a + b;

3. What is the final keyword used for?


Ans. The final keyword converts the variable into constant whose value cannot be changed at
any stage in the program.
It is used when we need that the value of a variable should not change. Like value of
constants , Pi etc.
Example:
int x= 10;
final int y = 20;
x= x +7;
y= y+7; // it gives compilation error message that
// “cannot assign a value to final variable y" will appear.

2. How much space do the following take – bit, terabyte, nibble, kilobyte?
Ans.
Name Size
Bit 1 bit
Nibble 4 bits
Byte 8 bits
Kilobyte 1,024 bytes
Megabyte 1,024 kilobytes
Gigabyte 1,024 megabytes
Terabyte 1,024 gigabytes
Petabyte 1,024 terabytes
Exabyte 1,024 petabytes
Zettabyte 1,024 exabytes
Yottabyte 1,024 zettabytes

3
1. What is Data type?
Ans.

Data type refers to the type of data that can be stored in a variable. Java has two types
Primitive and Reference Data Types.

2. What is Primitive Data Type?


Ans.
Primitive Data Types are Data types which are built into Java language. E.g. int,char etc. Java
has following data types.

Type Bit Byte Type Limit


byte 8 1 Integer value -27 to 27 – 1
short 16 2 Integer value -215 to 215 – 1
int 32 4 Integer value -231 to 231 – 1
long 64 8 Integer value -263 to 263 – 1
float 32 4 Floating-point ~ +/- 1039
double 64 8 Floating-point ~ +/- 10317
char 16 2 Unicode char
boolean 1 true or false True or false

3. What is reference data type? Give 2 examples of reference data types?


Ans.
The data type formed with the help of primitive data type are known as reference data type.
Example: Class, Interface and Array.

1. Why is Java called as strongly typed language?


Ans.
Java is called as strongly typed language as variables can ONLY take value that matches the
variable’s declared type. The compiler ensures that the program does not try to assign data of
the wrong type to the variable.
For e.g. in statement below, you cannot assign double to an int variable. It will give an error.
int val1 = 12.6; // Error

2. What is Type casting? What are 2 types of casting available in Java?


Ans. To convert one predefined data type to another data type is known as Type Conversion
or Type Casting.
There are two type of type casting:

4
i. Explicit Type Casting
ii. Implicit Type Casting

3. What is Explicit and Implicit Type casting?


Ans.
Implicit Typecasting
When a data type of lower size (occupying less memory) is assigned to a data type of higher size, it is
done implicitly by the JVM. The lower size is widened to higher size. This is called as Implicit Typecasting
and is also named as automatic type conversion.

Implicit type casting occurs if you assign any of these data types to higher data types

byte –> short –> int –> long –> float –> double

e.g
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0

Note A boolean value cannot be assigned to any other data type.

Explicit Typecasting
A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size.
This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed
by the programmer. The higher size is narrowed to lower size.

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value
and may also lose precision and range.

e.g.
double x = 10.5; // 8 bytes
int y = x; // 4 bytes ; raises compilation error
int y = (int)x; // ok. Explicit Typecasting

You might also like