Java Tokens & Data Types
- Categories Tokens & Data Types, Values & Data Types, Java
Here we are going to learn above Java Tokens & Data Types. You can watch our video on Java Tokens & Data Types- Click Here
Q. 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:
- Keyword
- Identifiers
- Literals
- Operators
- Separator (or Punctuators)
Q. 2 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
abstract | double | int | switch | char | for | protected |
---|---|---|---|---|---|---|
assert | else | interface | synchronized | class | goto | public |
boolean | extends | long | this | const | if | return |
break | false | native | throw | continue | implements | short |
byte | final | new | transient | default | import | static |
case | finally | package | true | do | instanceof | super |
catch | float | private | try | void | volatile | while |
Q. 3 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
- 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
Q. 4 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”;
Q. 5 What is difference between float literal and double literal?
Ans.Float Literal double Literal The float literal is a single-precision 32-bit (4 bytes) floating point literal that can store any number along with fractional part The double literal is a double-precision 64-bit (8 bytes) floating point literal that can store any number along with fractional part. It can handle 7 decimal places. It can handle16 decimal places.
Q. 6 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),
; (semicolon- used as statement terminator),
, (comma- used to separate list of variables),
. (dot or decimal- used make fraction value, include package, referencing object).
Q. 7 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;
Q. 8 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-
// “cannot assign a value to final variable y”
Q. 9 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
Q. 10 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.
Q. 11 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 type
Q. 12 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.
Q. 13 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
Q. 14 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:
- Explicit Type Casting
- Implicit Type Casting
Q. 15 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.
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