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

CHAPTER 2 - Values and Data Types - NEW - PDF PDF

This document discusses Java tokens, data types, variables, literals, comments, and escape sequences. It defines keywords, identifiers, literals, and punctuation as the five types of tokens in Java. There are two categories of data types - primitive (such as int, char, boolean) and non-primitive/reference (such as String, class, array). Variables can be global, local, instance, or static. Comments allow programmers to communicate thoughts independent of code and come in single-line, multi-line, and documentary formats. Escape sequences control printed output by preceding characters with a backslash.

Uploaded by

CHINMAY BAGADIYA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views

CHAPTER 2 - Values and Data Types - NEW - PDF PDF

This document discusses Java tokens, data types, variables, literals, comments, and escape sequences. It defines keywords, identifiers, literals, and punctuation as the five types of tokens in Java. There are two categories of data types - primitive (such as int, char, boolean) and non-primitive/reference (such as String, class, array). Variables can be global, local, instance, or static. Comments allow programmers to communicate thoughts independent of code and come in single-line, multi-line, and documentary formats. Escape sequences control printed output by preceding characters with a backslash.

Uploaded by

CHINMAY BAGADIYA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 2

VALUES AND DATA TYPES


Q1. What are Java tokens?
Ans: A token is the smallest element of a program that is meaningful to the compiler.
There are five types of tokens.
1) Keywords
2) Identifiers or Variables
3) Literals
4) Punctuations
5) Operators

Q2. Define keywords.


Ans: These words have pre-defined meanings to compiler. These reserved words are called
keywords.

Q3. Define data types.


Ans: Each value in memory is associated with a specific data type. This data type determines
what operations to be performed on the data.

Q4. How many categories of data types are there?


Ans: There are two categories of data types as follows,

1) Primitive data types (byte,short,int,long,float,double,char,boolean)

2) Non primitive or reference data types(String,class,array)

Q5. Explain comments with their types.


Ans: Comments allows programmers to compose and communicate their thoughts independent
of the code.
There are three types of comments,
1) Single line comment : This is also called as inline comment which is written in as single line.
‘//’ is used to give a single line comment.

For example,

X = A +B +C; // Adding three numbers

2) Multiple line comment: To create a multiple line comment place ‘/* ‘ at the beginning of the
line and place ‘*/’ after one or more than one lines. For example,

/* Write a description of class xyz here. Enter time and print in words @author (xyz)
@version (a version number or a date) */
3) Documentary comment:- These are long comments given on top of program which starts
with ‘\**’ symbol and ends with ‘*/’ and every statement will start with a ‘*’ symbol
between first and last statement.

For eg:
/** Write a program to check
*whether the number is
*even or odd */
Q6. Explain escape sequences in Java.
Ans: Escape sequences are used to control printed output. The characters preceded by a
backslash(\) are called escape sequences .

Character Escape
Sequence
Backslash \\
Backspace \b
Carriage return \r
Double quote \”
Horizontal tab \t
Newline \n

EXAMPLE : System.out.println(“The school will remain close for tomorrow.\n Students are
suppose to come by next week.”);

Q7. Define constants or literals. Give example for all the primitive data types.
Ans: Constants are data values that never change their values during program execution.

Q8. Define separators.


Ans: Separators are as periods, semicolons, question marks, pairs of parenthesis, and other
punctuation add structure to written text, Java separators help the Java compiler interpret
components of Java source code.

Examples: ( )
{}
[]
;
,
.
Q9. What are operators? Explain in detail.

Ans: Operators are special symbols that perform a particular function on operands.
Example:
a = b+c;
In above expression ‘+’ is the operator and (a , b ,c) are operands.

Q10. Define variable .


Ans: A variable is the defined memory location to include the data.

Q11. What are the different types of variable?


Ans:
1. Global variable: It is declared out of all the function and it’s accessibility is possible in
all the functions.
2. Local variable: It is declared inside the functions and it’s accessibility is only inside the
function.

3. Instance variable: The variables are declared inside a class but not within a function.
They are created when a number of objects are created from the same class. Each object
has its own copy of instance variable.

4. Static variable: Variable that have ‘static’ modifiers in their declaration are called static
variable. Static variables are associated with the class rather than any object. That is, all
the objects share the single copy of the variable.

Example of types of variables:

public class demo


{
int a; // instance variable or global variable
static int b;// static variable
void calc()
{
int x,y; // local variable
a=x+y;
System.out.println (a);
}
public static void main(String arg[]){
demo obj=new demo();
obj.calc();
demo.b=8;
System.out.println(“value of b is ”+demo.b);
}
}
Q12. What is NAN?
Ans: NAN stands for Not a Number. It is a java constant which is produced
due to an operation which results in an undefined number. For example:
System.out.println(0.0/0);

Q13: What is the significance of keyword ‘final’ in the following statement,


final int a = 10;
a = a +1;

Ans: final int a = 10;


The keyword ‘final’ indicates that the variable value 10 be treated as a constant. It cannot be
changed within the program. So, a = a+1; would be an incorrect statement.

Q14. What are the default values and size of data types in Java ?

Data Type Default Value (for Size


fields)
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

char ‘u0000’ 2 byte

String (or any null


object)
boolean false 1 bit
JAVA DATA TYPE STRUCTURE

DATA TYPES
STRING

PRIMITIVE NON PRIMITIVE CLASS

ARRAY
Numeric

BOOLEAN
Character

boolean Floating - point


char

float double
Integer

byte short int long

Q15. Define identifiers.


Ans: Identifiers are the identifiable names given to any variable ,class,object,function or array.
class xyz{
int a,b;
void enter( ){ }
}

You might also like