Values and Data Types

Character Sets used in Java

Java language uses letters, digits, operators and delimiters. It uses Unicode to represent characters.

Unicode is a standard encoding system created by Unicode consortium that is used to encode a character in any computer language.

Unicode uses 16 bits to represent a character.

Advantages of Unicode

  • It is the universal coding scheme.
  • More efficient than ISO or IEC.
  • Supports uniform coding width for all characters.
  • Character codes are unique for each character.

ASCII Character Codes

ASCII stands for American Standard Code for Information Interchange. It uses 7 bits to represent a character.

ASCII code of white space blank is 32.

Difference between Unicode and ASCII

Unicode ASCII
It is a generalized form of coding scheme for numerous characters of different scripts. It is a specific coding scheme used for limited number of characters.
It represents higher range of codes. It represents limited range of codes.

Escape Sequences

They are the non-graphic characters, used as commands to direct the cursor while printing. Escape sequences always begin with a backslash (\) character.

Following are some of the escape sequences:

  • \t for Horizontal tab
  • \v for Vertical tab
  • \n for New line
  • \’ for Single quote
  • \” for Double quote
  • \b for Backspace
  • \f for Form feed
  • \r for Carriage return
  • \0 for Null
  • \\ for Backslash

Tokens

Tokens are the individual components of a Java statement.

Following are the various types of tokens:

  • Literals or Constants: These are the fixed values that do not change during program execution.
  • Identifiers: These are the variables used in a Java program.
  • Assignments: The = sign is an assignment as it helps to store values in a variable.
  • Punctuators: Special characters like question mark (?), dot (.), semicolon (;) are punctuators.
  • Separators: Special characters used to separate various parts of a Java program are separators.
  • Operators: These are symbols that perform some calculation or operation with values.
  • Keywords: These are reserved words which have special meaning for the compiler.

Initializing a Variable

Initialization of a variable can be done in one of two ways:

  1. Static initialization: It uses direct assignment of a constant to a defined variable.
  2. Dynamic initialization: It occurs when a variable gets initialized at run time.

Data Types in Java

There are two types of data types in Java: Primitive and Composite.

Primitive Data Type: These are the predefined data types. Following are the primitive data types:

  • byte (1 byte)
  • short (2 bytes)
  • char (2 bytes)
  • int (4 bytes)
  • long (8 bytes)
  • float (4 bytes)
  • double (8 bytes)
  • boolean (1 byte)

Type Conversion

Converting one data type to another is called type conversion.

There are two types of type conversion:

  1. Implicit: Conversion that takes place automatically into higher type is implicit type conversion. It is also known as coercion.
  2. Explicit: Conversion that takes place by the user by force is explicit type conversion.

Multiple Choice Questions:

  1. A constant which gives the exact representation of data is called:
    a. Variable
    b. Literal
    c. Identifier
    d. Character
  2. A word used in a high-level language which has a special meaning attached to it is called:
    a. Class
    b. Identifier
    c. Keyword
    d. Literal
  3. A character literal is assigned to a:
    a. char variable
    b. char type literal
    c. String variable
    d. String literal
  4. A character literal is enclosed in:
    a. ‘ ‘
    b. ” ”
    c. ::
    d. {}
  5. A set of characters is assigned to:
    a. String variable
    b. static variable
    c. boolean variable
    d. None
  6. The ASCII codes of uppercase alphabets range from:
    a. 65 – 90
    b. 60 – 85
    c. 65 – 91
    d. 97 – 122
  7. Which of the following results in integer type?
    a. 11.4F / 32.D
    b. 13.8F / 4.6F
    c. 12 / 3
    d. None
  8. Which of the following is non-primitive data?
    a. char
    b. long
    c. object
    d. short
  9. Which of the following type is an exact representation of fractional values?
    a. char
    b. double
    c. byte
    d. String
  10. boolean data is used to test a particular condition i.e. true or false. Which of the following is a correct representation?
    a. boolean m = true
    b. boolean m = ‘true’
    c. boolean m = “true”
    d. None

Fill in the blanks:

  1. The character sets of Java is like alphabets of English language.
  2. A standard encoding system of representing characters is Unicode.
  3. ASCII code is the decimal representation to represent a character.
  4. Each individual component of a Java statement is known as tokens.
  5. In Java, the constants are also called literals.
  6. Assignment operator is used to store a value in the variable.
  7. The comma, exclamation, question mark, etc. are termed as punctuators in Java language.
  8. An element of Java program that is used to identify a class, function or value is called as identifier.
  9. Integer type value occupies 4 bytes in memory.
  10. A Java expression that contains all the elements of same data type is pure expression.

6 thoughts on “Values and Data Types”

Leave a Reply

%d bloggers like this: