0% found this document useful (0 votes)
146 views38 pages

Java Data Types and Values Explained

The document provides an overview of values and data types in programming, focusing on character encoding, tokens, literals, identifiers, assignment, operators, punctuators, separators, and keywords. It explains primitive and non-primitive data types, including numeric and non-numeric classifications, as well as type conversion methods such as implicit and explicit conversions. Additionally, it includes examples and important questions related to the concepts discussed.

Uploaded by

sachianandladdha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views38 pages

Java Data Types and Values Explained

The document provides an overview of values and data types in programming, focusing on character encoding, tokens, literals, identifiers, assignment, operators, punctuators, separators, and keywords. It explains primitive and non-primitive data types, including numeric and non-numeric classifications, as well as type conversion methods such as implicit and explicit conversions. Additionally, it includes examples and important questions related to the concepts discussed.

Uploaded by

sachianandladdha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT III

VALUES AND DATA TYPES

By
Jyoti Suryawanshi
INTRODUCTION
• Program is a set of statements.
• Each statement is formed using different words called as
tokens.
• Each token is formed by using valid characters.
• Characters consist of letters, digits, operators and delimiters.
ENCODING OF CHARACTERS
• Characters entered from keyboard are not directly stored in
the computer memory.
• Because memory stores a character in numeric form.
• So characters entered are converted into numeric form
before actually getting stored in the memory.
• Two types of encoding schemes:
[Link] code
[Link]
ENCODING SCHEMES

1. ASCII code
• ASCII stands for American Standard Code for
Information Interchange.
• It uses 7 bits code for representing each
character.
• It includes 256 characters but only 128 are used.
• ASCII code represents these characters as
numbers, with each letter assigned a number
from 0 to 127.
ESCAPE SEQUENCES
Escape Meaning • These are some characters that are used to
sequence control the cursor while displaying the values
\t Horizontal on the screen.
tab
\\ Backslash • Each escape sequence starts with backslash
\’ Single quote
(\) followed by a specific character.
\” Double
• The complete set is enclosed within double
quote quotes.
\n New line
• Some escape sequences are shown in the
table.
ESCPAE SEQUENCES
• Ex: [Link](“pawar\n public school”) ;
Output: pawar
public school
• Ex: [Link](“pawar\t public school”) ;
Output: pawar public school
• Ex: [Link](“\“pawar public school\””) ;
Output: “pawar public school”
ESCPAE SEQUENCES
• Ex: [Link](“pawar\\public school”) ;
Output: pawar\public school
• Ex: [Link](“pawar \’public\’ school”) ;
Output: pawar ‘public’ school
• Ex: [Link](““pawar public school””) ;
Output: Syntax error
TOKENS
• Token is a set of valid characters used for writing a
statement in java program.
• These are smallest elements of the program identified by the
compiler.
• Token is defined as each individual component of Java
program that carries meaning and takes active part in
program execution.
• Types of tokens used are: Literals, Identifiers, Assignment,
Operators, Punctuators, Seperators and Keywords.
LITERALS
• Literals are the constants that remain unchanged during the
entire execution of the program.
• The various types of literals are:
• Integer Literals: The whole numbers are known as Integer
Literals. Ex: 12,-34, 456 etc
• Real Literals: They are fractional numbers. Ex:3.45, 5.67 etc
• Character Literals: A single letter or a digit or any special
symbol enclosed within single quotes is known as character
litetal. It does not take part in arithmetical calculations.
• Ex: ‘A’, ‘1’ etc.
LITERALS

• String Literals: A set or group of characters enclosed within


double quotes is known as a String litetal. Ex: “Pawar Public
School”
• Boolean literal: Boolean Literals are true or false. A boolean
literal can be either true or false at a time.
• Null literal: Null literal denotes the absence of a value. It is
used to initialize object or array.
Ex: Integer n = null;
IDENTIFIERS
• Identifiers are are names given to a classic, function or
variables, an object or an interface.

ASSIGNMENT
• The term assignment refers to storing a value in a
variable.
• The variable and value to be stored must be of
compatible data types.
Ex: int a= 12;
OPERATORS
• Operators are the symbols or signs used to specify the
operations to be performed in a Java expression.
• There are three types of operators in Java.
• Arithmetical operators: +, -, *, / etc
• Relational operators: <, >, <=, >=etc
• Logical operators: &&, ||etc
PUNCTUATORS
• Punctuators are the punctuation signs used as special
characters in Java. Ex: comma(,),semicolon(;) etc

SEPERATORS
• They are the special characters in Java, which are used to
seperate the variables or the characters.
• Ex: brackets(), curly brackets {} etc.
KEYWORDS
• Keywords are the reserved words which are preserved by
the system and carry special meaning for the system
compiler.
• We can not use them as names for variables.
• Ex: class, public, for, void etc.
DATA TYPES
• Data type basically
refers to type of data,
a memory location
can hold.
• Whenever we need to
store a value, the
respective data type
must be mentioned so
that system may
structure memory
location for its proper
usage.
PRIMITIVE DATA TYPES
• Primitive data types are the basic or fundamental data
types used to declare a variable.
• They are further classified as Numeric data types and Non
numeric data types.
• Numeric data types
• Non-Numeric data types
PRIMITIVE DATA TYPES
• Numeric data types:
• The numeric data types are used to declare variables that can store
numbers.
• Based on type of values that can be stored in a variable, numeric
data types are classified as Integer type and real type.
• Non- Numeric data types:
• Non numeric data types do not deal with numbers rather they are
used to declare a character or a set of characters.
• They are classified as character type and boolean type.
NUMERIC DATA TYPES
• Integer type:
• A variable declared under this type contains an integer literal.
• They are classified as:
• byte: It is used to store integer data upto 8 bits(1 byte).
Ex: byte x= 7;
• short: It is used to store a small range of integers upto 2 bytes.
Ex: short x= 25;
• int: It is used to store integers upto 4 bytes.
Ex: int n=342;
• long: It is used to store large integers upto 8 byte.
Ex: long c= 1234;
NUMERIC DATA TYPES
• Real Type:
• A variable declared under this type contains floating point data.
• These are categorised as:
• float: It represents a fractional number with a small range upto 4
bytes.
A float data type can store numbers upto 7 significant digits only.
Ex: float x= 3.45f;
• double: It represents a fractional number with wide range values
upto 8 bytes.
A double data type has more precision. It will store upto 15
significant digits.
Ex: double x= 0.1234567;
RANGE OF NUMERIC DATA TYPES.
NON-NUMERIC DATA TYPES
• Character type:
• char:
• A variable is declared as char when it holds a character
literal.
• It is enclosed in single quotes.
Ex: char div= ‘A’;
• String:
• A set of characters are stored in String data type.
• It is enclosed in double quotes.
Ex: String name= “Pawar Public School”;
NON-NUMERIC DATA TYPES
• Boolean type:
• boolean:
• Boolean Literals are true or false.
• To store a boolean literal, the variable must be declared
boolean.
• Ex: boolean x= true;
NON-PRIMITIVE DATA TYPES
• A non primitive data type is one that is derived from
primitive data types.
• A number of primitive data types are used together to
represent a non primitive data type.
• Hence a non primitive data type is also called as composite
data type.
• Ex: array, class etc.
DECLARATION VS INITIALIZATION
• Declaration is the term used to declare the tpe of a variable
whereas initialization refers to assigning or storing value to
the variable.
• Ex: int x; //variable declaration
x=5; // variable initialization
• Declaration and initialization can be done in one line also.
• Ex: int x=5;
DEFAULT INITIALIZATION
• If a variable is not
initialized but is used
in the program, then
the system initializes it
implicitly with a
specific value called
default initial value.
STATIC INITIALIZATION

• Static
initialization is a
way to store a
value directly
to a variable
by using
assignment (=)
operator.
DYNAMIC INITIALIZATION
• Dynamic initialization is used to initialize a variable at run
time.
• In this system variable is initialized with value which is the
outcome of some expression.
INITIALIZATION
• The variables which are declared within class are initialized
with default initial values.
• The variables declared under main () need to be initialized
by the user.
TYPE CONVERSION
• Arithmetical expression is defined as set of variables,
constants and arithmetical operators.
• Ex: a+b*c-4;
• Here a, b, c are variables, 4 is constant and +, -, * are
operators.
• If an arithmetical expression contains all the datavaluea of
same type is known as pure expression.
• Ex: int a, b, c;
a+b*c-4;
TYPE CONVERSION
• When data items in the expression have different data types
then the expression called Impure Arithmetical Expression.
• Ex: int a;
float b, c;
a+b*c-4;
• In impure expression what will be the data type of result
after execution?
• System converts result into specific data type known as type
conversion.
TYPE CONVERSION

• Types of type conversion:


• Impliclit type conversion
• Explicit type conversion
IMPLICIT TYPE CONVERSION
• In impure expression,
when data type of
the result gets
converted into
highermost data type
available in the
expression without
any intervention of
the user, is known as
implicit type
conversion.
• It is also known as
Coercion.
EXPLICIT TYPE CONVERSION
• It is type of data conversion in which data of result of
impureexpression gets converted into specific type as per
user’s choice.
• The choice of data type must be written within braces( ).
• The explicit type conversion is also known as Type Casting.
• Ex: int a; float b;
c = (int)(a+b) ;
• In the example a, b are different types of data. Result should
implicitly be in higher most float type. But user demands to
convert it into int type which is lower type.
• Hence System is forced to provide a int type result using type
casting.
IMPORTANT QUESTIONS

• State the values of n and ch.


char c= ‘A’;
int n= c+1;
char ch = (char) n;
------ n= 66 ch = B
• Predict the return data type of r.
int p; float m;
r= p+ m;
IMPORTANT QUESTIONS

• What is meant by type conversion? How is an implicit conversion different


from explicit conversion?
• What are the types of casting shown in the following examples?
double x = 15.2;
int a= 12;
I) int y= (int) x;
II) long b = a;
IMPORTANT QUESTIONS
• Give one example each of primitive data type and non-
Primitive data type.
• What is literal?
• Name a primitive data type that is single 16 bit whose default
value is ‘\u0000’.
• State difference between floating point literal and double type
literal.
• What are the default values of primitive data type int and
float?
• Identify Literals below: 0.5, ‘A’, false
IMPORTANT QUESTIONS
• State difference between primitive data type and
composite data type.
• Distinguish between token and identifier.
• Distinguish between Character and Boolean literal.
• Arrange primitive data types in ascending order of their
size:char, byte, double, int

You might also like