CH-3 Values and Data Types · Identifiers are used to name different components of a program such...

12
CH-3 Values and Data Types 4.1 Character Set A character set is a defined list of characters recognised by the Java language. It is comprised of: a. Alphabets: letters A-Z (upper-case) and a-z (lower-case) b. Numerics: digit 0-9 c. Special Characters: !, %, }, >, & etc. Collectively, the alphabets and numerals are known as the alphanumeric character set. Two standard character sets are ASCII and Unicode.

Transcript of CH-3 Values and Data Types · Identifiers are used to name different components of a program such...

Page 1: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

CH-3

Values and Data Types 4.1 Character Set A character set is a defined list of characters recognised by the Java language. It is comprised of: a. Alphabets: letters A-Z (upper-case) and a-z (lower-case) b. Numerics: digit 0-9

c. Special Characters: !, %, }, >, & etc.

Collectively, the alphabets and numerals are known as the alphanumeric character set. Two standard character sets are ASCII and Unicode.

Page 2: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

Escape Sequences , •A

An Escape Sequence is a set of characters that has a special meaning to the Java compiler. In

an escape sequence, a character is preceded by a backslash(\). Earlier you wrote a simple

program which displayed the Hello World! message on the Terminal window. What if you

wanted to say Hello "World!" i.e. you want to put double quotes around "World!"? How

would you do that? For this you need to use an Escape Sequence.

For example, if you wanted to put quotation marks within the quotation marks, you must use

the escape sequence\" as displayed in the following statement:

System.out.println( "Hello \"World!\"");

Tokens All characters in a Java program are grouped into symbols called Tokens. A computer program consists of a set of instructions called statements. A statement is composed of various components. Each individual component of a programming statement is referred to as a token. There are five categories of tokens in Java. These are:

1. Keywords 2. Identifiers 3. Literals 4. Punctuators

5. Operators

Page 3: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

Keywords Keywords, also known as Reserved Words, are the words that have a special meaning to the Java compiler. Java compiler reserves these keywords for its own use and hence they are not available as names for variables or methods. Some of the Java keywords are listed below:

Identifiers Identifiers are used to name different components of a program such as variables, methods, objects, etc.

Java Identifiers naming conventions It is recommended to follow these naming conventions. Identifiers are fundamental building blocks of a program and are used to name different components of

a program.

l. An identifier can consist of any combination of letters, digits, the underscore character (_)

and the dollar sign ($). 2. An identifier cannot begin with a digit. 3. Identifiers may be of any length. 4. Both uppercase and lowercase letters can be used in naming an identifier. S. Java is case sensitive, which means that two identifier names that differ only in upper and lower case characters are considered to be different identifiers. Therefore, total, Total, ToTaL, and TOTAL are all different identifiers. Examples of some valid identifiers are:

♦ student_Name ♦ $M1English + file3 ♦ _Age ♦ TOTAL ♦ this_is_a_ very_long_identifier

Examples of some invalid identifiers are:

♦ this - it is a Java keyword.

♦ Total amount - space is not allowed in the identifier name.

♦ lM_Eng - identifier name cannot begin with a digit.

+ total-Marks - special character other than (_) and ($) are not allowed.

Literals Literals are a sequence of characters that represent values in a program and are stored in variables. Literals are made of digits, letters and other characters. Java supports the following types of literals:

Page 4: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

Numeric literals

Numeric literals are values that may consist of digits o to 9, a decimal point(.), a positive(+) or

negative(-) sign. a. Integer Literals: Integer literals are values, which represent whole numbers only. They can be positive or negative values. The integer value that contains no sign represents a positive value by default. Examples of some valid Integer literals are:

1) 100 2) +78

b. Real Literals: Real literals are also called floating point literals as they represent values with a decimal point. These can be written either in fractional or exponential form.

A real literal in the fractional form consists of signed or unsigned digits, including a decimal point

between the digits. Examples of some valid real literals in the fractional form are:

1) 12.31 2) -7.90

2. Non-numeric Literals Java offers two types of non-numeric literals. These are:

a. Character Literals: Literals of this type represent exactly one character inside single quotes.

Some examples are:

+ 'K' '·' I + '@' + '6' b. String Literals: One or more characters enclosed inside double quotes are string literals. For example:

+ "xyz" + "New York" + "Baby\'s day out" + "x" + "x+3" + "3+4"

3. Boolean Literal The logical values true and false are the boolean literals. During a logical evaluation, the true/false literals represent on/ off, yes/no, presence/absence of something.

4. The Null Literal This is a special kind of literal which is represented by the keyword null or escape sequence' \0'.

Punctuators Punctuators (also called separators) are symbols used for grouping and separating the code. Table 5 lists the various punctuators and their purpose:

Operators

0 erators are symbols which are used to perform arithmetic or logical operations in a given expression. The variables, n!rals, or values are combined to build an expression. The variables or literals on which an operator is applied are known as operands. An operator needs one or more operands to perform any operation.

Page 5: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

Data Types You had a glimpse of some of the basic data types while studying literals. Java has a variety of data types that allows programmers to pick the data type suitable to the needs of the application. These data types can be broadly divided into the following two categories: Primitive D.ata Types: The fundamental data types that are an integral part of the Java programming language are called primitive data types. They are also called intrinsic or built-in data types because these are part of the programming language. There are eight primitive data types in Java. These are: byte, short, int, long, char, float, double, and boolean. Non-Primitive Data Types: The data types that are derived from the primitive types are called non-primitive types. They are also called reference types, derived types or composite data types. There are three non-primitive types in Java: classes, arrays and interfaces. The storage mechanism of non-primitive types is different from the primitive types. You will learn about this later.

Integer Data Types

Integer data types are u sed to store whole numbers, for example, 345, 1000 and -

323. There are further four types of integers .m J ava as sh o wn in Figure 4 ·4 · These are byte ' short, int, and long. The values that can be stored in these depend on which data type is being used in the program.

Page 6: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

Floating Point Data Types As you know, the integer data types can store only whole numbers so, they cannot be used to store decimal values. Thus, the floating point data types are used to store fractional numbers i.e. numbers with decimal values. Floating point data types can be further divided into float and double data type.

To designate a literal constant of the type float, you must append the letter for F for example, l.5f or 1.5F.

character Data Types In Java, characters are stored using char data types. This data type supports the Unicode

character set. The character data type occupies 2 bytes (16 bits) in the memory .

The following table summarises the size and range:

Variables Variables is an identifier that denotes a location in computers memory to store a data value. Declaring Variables

Variable declaration plays a key role in a Java program. A variable can be used in a Java program only if it has been declared. During program execution, when the computer executes a variable declaration, it performs the following two tasks:

Page 7: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

+ Sets aside memory for the variable.

+ Associates the variable's name with that memory.

A simple variable declaration has the following syntax: data-type variable-name;

Where the data-type can be any primitive data type and the variable-name is

the name of the variable. Since a variable is an identifier, all the rules of identifier

naming conventions also apply (see section 4.5.2). The following statement will declare an integer type variable studentAge. float percentage; char grade; You can assign values to multiple variables in one statement or choose to declare some and initialise the others. For example:

short hours= 10, minutes= 36, secs= 55;

int x, y = 6, z;

Page 8: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

Constants (Named or Symbolic Constants) Sometimes, you may not want to change the value of a variable once it has been initialised. For example, in a program that calculates simple interest, you may want to

keep the interest rate 0.05 throughout the entire program. In Java, the modifier final

can be used in a variable declaration to ensure that the value stored in the variable

cannot be changed after the variable has been initialised. The keyword final makes a

variable as a constan t, i.e. whose value cannot change

final double INTEREST_RATE = 0.05;

Comments in Java Comments are added in a program to give an overview of the code and provide additional information.

Single line Comment

This type of comments begins with (//)

Multiline Comment Sometimes, multiple lines are required to explain the logic of a program. In such cases, the programmer can use a multiline comment. This style of comment starts with the initiating slash-asterisk(/*) and ends with the term· (*/) ., the comment can extend over multiple lines. Note that there must be no space between sash the asterisk.

Documentation Comment Documentation comment is a special comment (called Javadoc) This type of comment is

generally used two asterisks (/** I and ends with an asterisk followed by a slash (*/), Just like

other comments. The compiler ignores this kind of comments

Page 9: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

Multiple Choice Questions: 1. ASCII stands for ........................................... .. A. American Standard Code for Information Interchange B. American Simulated Code for Information Interchange C. American Standard Code for Interchange of Information

D. American Standard Code for Interaction of Information

2. ASCII is ............................................. A. 6-bit set of codes C. 7 -bit set of codes

B. 8-bit set of codes D. 16-bit set of codes

3. Extended ASCII is ............................................. A. 6-bit set of codes C. 7-bit set of codes

B. 8-bit set of codes D. 16-bit set of codes

4. Which of the following is not a token? A. Keywords C. Operators

B. Identifiers D. Procedure

5. Which of the following is a keyword? A. character C. object

B. break D. attribute

6. Which of the following is not a legal identifier? A. _age C. 9thClass

B. $Amount D. nullValue

7. Which of the following is an invalid integer? A 1999 C. 199 998. 1999999 D. 199 B. Which of the following is not a character literal? A. '\t' C. 't'

B. "f' D. All of these 9. Which of the following punctuator is the statement terminator in Java?

A ; B , C. . D. All of these

10. Which of the following is not a primitive data type in Java? A. Boolean B. short C. float D. class 11. Which of the following is the size of a long data type in Java? A. 32 bits C. 48 bits B. 64 bits D. Long data type is not supported in Java 12. Which of the following is the size of a boolean data type in Java?

Page 10: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

A. 1 bit C. 8 bits B. 16 bits D. Boolean data type doesn't take any space in memory [ans: 1.A 2.( 3. B 4.D 5. B 6. C 7.C 8. B 9.A 10.D 11. B ] State whether given statements are True or False: 1. Java supports the use of the ASCII character set only. 2. The smallest unit in a Java program is known as a token. 3. The Unicode character set uses between 8 and 32 bits per character. 4. In an escape sequence, a character is a preceded by a forward slash (/). 5. In Java, an identifier cannot begin with a $ sign. 6. The boolean data types are used for storing logical values. 7. Java offers five types of tokens. 8. The char data type reserves 8 bits in memory. 9. If a literal constant contains a decimal point, then it is of the type double by default. 10. The modifier final can be applied to a variable declaration to ensure that the value stored in the variable cannot be changed after the variable has been initialised.

[1. False 2. True 3. True 4. False 5. False 6. True 7. True 8. False 9. True 10. False] Answer the following questions In short: 1. What is a character set? Ans. A character set is a defined list of characters recognised by the computer hardware and software. 2. What is an escape sequence in Java? Ans. An escape sequence is a set of characters that has a special meaning for the Java compiler. In an escape sequence, a character is a preceded by a backslash (\). Some

examples of escape sequences are \n, \' and \t. 3. int res = 'A';

What is the value of res?

Ans. 65

4. Write the output for the following:

System.out.println("lncredible" + "\ n" + "world"); Ans. Incredible world

5. What are tokens in Java? Name any t hree tokens. [ICSE-2018)[ICSE-201 8] Ans. All characters in a Java program are grouped into symbols called Tokens. As you know,

a computer program consists of a set of instructions called statements. A statement is composed of various components. Each individual component of a programming statement is referred to as a token. Keywords, identifiers, and literals are three tokens in Java.

6. What are keywords? Give an example. [ICSE-2016] Or What does the token 'keyword' refer to in the context of Java? Give an example for

keyword. [ICSE-2012) Ans. Keywords are reserved words because the Java compiler reserves these words for its own use and therefore are not available as names for variables or methods. These words have a special meaning to the Java compiler. For example, void and public.

Page 11: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

7. What are identifiers? [ICSE-2015] Ans. Identifiers are used to name different parts of a program such as variables, methods, classes, objects, etc.

8. What are literals in Java?

or

What is a literal? [ICSE-2013) Ans. Literals are a sequence of characters that represent values in a program and are

stored in variables. They consist of digits, letters and other characters.

9. Identify the literals listed below:

i. 0.5 ii ‘A’ iii. false iv “a”

ANS: i. real iii. Boolean ii. Char iv. String

10 What are prirnI11ve data t)'PE,? r arne any rnree pnm1hve dat<1 types. [ICSE-201

AnS.. Pnmit1ve data rypes are fundamental d aia types ~hat a1e an integral, part of

me Jara language. The names of three

pnmiuve data types are int, float and boolean.

11. Arrange the following primitive datatypes in ascending order. i. char ii. byte iii. Double iv. int

Ans. i. byte ii. char

iii. int iv. Double 12. State the number of bytes occupied by char and int data types. Ans. The char data type occupies 2 bytes and the int data type occupies 4 bytes. [ICSE-2017] 13. State the difference between a boolean literal and a character literal. [ICSE-2009] Ans. (i) A boolean literal is always of boolean type. It's either boolean value true or

false. A character literal is of char type. It is one character enclosed in single quotes

e.g. 'G'.

(ii) A boolean literal uses 1 byte of storage whereas, a char literal uses 2 bytes. 14. Write one difference between primitive data types and composite data types. [ICSE-201 6] Ans. Primitive data types are fundamental data types that are an integral part of the Java language (i.e. int, char). The data types that are derived from the primitive data types (i.e. class, array) are called non-primitive or composite data types. 15. What are the default values of the primitive data type int and float? Ans. Default value of int is 0 and float is 0.0f 16. Name the primitive data type in Java that is: (i) a 64-bit integer and is used when you need a range of values wider than those provided by int.

Page 12: CH-3 Values and Data Types · Identifiers are used to name different components of a program such as variables, methods, objects, etc. Java Identifiers naming conventions It is recommended

(ii) a single 16-bit Unicode character whose default value is '\u0O00' Ans. (i) long (ii) char 17. State one difference between floating point literal and double literal. Ans. Floating point literals require 4 bytes of storage whereas double literal require 8

bytes. 18. Classify the following as primitive or non-primitive data types: (i) char (ii) arrays (iii) int (iv) classes Ans. (i) char - primitive data type. (ii) arrays - non-primitive data type. (iii) int - primitive data type. (iv) classes - non-primitive data t ype.

9. Give one example each of a primitive data type and a composite data type

Ans. Primitive data type - int and long. Composite data type - class and array