Values and Types
1
Character set
▰ It is a set of alphabets, letters and some special characters that are
valid in Java Language
▰ Character Encoding: It tells computer how to interpret 0's and 1's into
actual character by pairing numbers with character.
▰ The commonly used character encoding is ASCII and UNICODE
2
ASCII Character set
▰ ASCII – American Standard Code for Information Interchange.
▰ In this each character in computer is represented using integer
value
▰ E.g.
Character ASCII
A 65
Blankspace 32
" (double quotes) 34
3
Unicode Character set
▰ It is 16 bit character set which can represent almost all human
alphabets and writing system.
▰ You can refer to a particular Unicode character by using \u
followed by four digit hexadecimal number
▰ E.g.
Character Unicode
A \u0041
Blankspace \u0020
" (double quotes) \u0022
4
ASCII Character set
▰ Java uses both ASCII and Unicode for character set.
Character ASCII values
A-Z 65 – 90
a–z 97 – 122
0 – 9 (digits) 48 – 57
Blank Space 32
5
Escape Sequence
▰ It is use to represent certain non graphical characters and other
characters which have some special meaning to the compiler.
▰ Any character preceded by a backslash ( \ ) is an escape sequence
▰ Escape sequence are written inside the double quotes of
[Link]()
Escape sequence Meaning
\n Inserts new line in the text
\t Inserts Tab space in the text
\' Inserts single quote in the text
\" Inserts double quote in the text
\\ Inserts backslash in the text 6
Example of java Program
class Program1
{
public static void main()
{
// storing values in variable
int a=5;
int b=3 ;
// calculating the sum
int c=a+b;
// printing the output
[Link]("The value of a= "+a);
[Link]("The value of b= "+b);
[Link]("Addition= "+c);
} 7
}
Token
▰Token is the smallest unit of Java program that ends with a space.
Literal / Constant
Keyword
Tokens
Identifier
Operator
Separator / Punctuator
Comment
8
Example of java Program
class Program1
Keyword
{
Identifier
public static void main()
{
Operators
// storing values in variable Literals / Constant
int a=5; Separators / Punctuators
int b=3 ; Comment
// calculating the sum
int c=a+b;
// printing the output
[Link]("The value of a= "+a);
[Link]("The value of b= "+b);
[Link]("Addition= "+c);
} 9
}
Keyword
▰ Keyword are the reserved words which has special meaning to the
compiler.
▰ Keyword cannot be used as identifier
▰ E.g. class, public, static, void, int etc…
10
Literal / Constant
▰Literals are the values which do not change during the
compilation/execution of java program
Character '@'
Byte, Short,
-1 , 5, 45989
Integer, Long
Literals /
Float, Double 3.14, 10.124
Constants
String "Mumbai"
Boolean true / false
11
GIVE REASON
Following are invalid, real/floating literals
1) 172.E5 2) 1.7E 3)0.17E2.3 4)17,225E02 5).25E-7
1) At least a digit must follow the decimal point
2) No digit specified for exponent
3) Exponent cannot have fractional part
4) No comma allowed
5) No preceding digits before decimal part 12
Identifier
▰ Identifier are the names given to different part of the java program like
class name, variable name, function/method name, object name
▰ They help us to identify the different parts of the java program
13
Conventions/Rules in general
for all identifiers
▰ An identifier can be of any length.
▰ It may contain alphabets, digits and underscore ( _ )and dollar
sign ($) characters.
▰ They must not be a keyword or true, false or null literal
▰ It must not begin with a digit
▰ It can start with only 2 special characters $ and _ Ex: String
_s; or int $n1;
▰ Java is case sensitive i.e. uppercase and lowercase letters are
treated differently
14
Conventions/Rules for naming
a class
▰ The first Letter of a each and every word should be in upper
case (Capital Letter)
▰ If a class name consist of more than one word, then write all
the word in continuous form
▰ E.g System, Scanner, BufferedReader, InputStreamReader
etc…
15
Conventions/Rules for naming
a method/function
▰ Function name should always end with parenthesis ().
▰ If a function name consist of one word than the whole word
should be in small letters.
▰ If a function name consist of more than one word, then the first
word is in small letter and rest of the word , first letter of each
word is in capital letter.
▰ E.g pow(), print(), println(), next()
▰ nextInt(), nextLine(),
▰ isUpperCase(), equalsIgnoreCase() etc…
16
Data types
▰ The type of value that can be stored in a given variable is
known as datatype.
▰ Significance of giving data type to a variable
a) The type of value that can be stored in the variable
b) The type of operation that can be performed on the variable
17
Categories of Data types
These data types exist own their
Primitive own. They are also known as
Built-in data types
Datatype
These data types depend on
Composite / primitive data type and their
Non primitive/ values. They are also known as
Reference User Defined Types value (UDT)
or User Defined Data types.
18
Primitive Data type
▰ These are the data types that exist on their own and do not depends on
other data types.
▰ They are also known as built-in data type
▰ They have fix memory size.
▰ There are 8 Primitive data types (boolean, char, byte, short, int, long,
float, double)
19
Composite /
Non – Primitive Datatype
▰ Array, String, class, objects are composite datatypes.
▰ They do not have fix memory size
20
Data types
Data Type Default Value Memory size
boolean false 1 byte
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L or 0l 8 byte
float 0.0f or 0.0F 4 byte
double 0.0d or 0.0D or 0.0 8 byte
String null or "" No. of characters x 2
1 byte = 8 bits 21
Variable
▰ Variable is a temporary memory location which can
temporarily store any value.
▰ Two things to do to work with variable
a) Declaration
b) Initialisation
22
Variable Declaration
▰ Declaring a variable means creating a memory space to store
the value
▰ Syntax:
datatype variablename;
e.g. int a; int b; int c; or int a, b, c;
char ch;
23
Variable Initialisation
▰ Initialising a variable means assigning or storing a value in a
variable.
▰ Syntax:
variablename = value;
e.g. a = 15;
ch = '@';
24
Variable Declaration &
Initialisation in one line
▰ Syntax:
datatype variablename = value;
e.g. int a = 15;
char ch = '@';
25
Working with Variables
Data Type Declaration Initialisation
boolean boolean x; x = false;
char char ch; ch = '@';
byte byte a; a = 7;
short short a; a = 77;
int int a; a = 71456;
long long a; a = 532L;
float float a; a = 7.15f;
double double a; a = 7.15;
String String a; a="Mumbai“; 26
Expression in Java
▰An expression is combination of literals , operators ,
variables and parentheses used to calculate a value.
27
Types of Expression
1) Pure Expression: Similar types of operands are present.
E.g: int a = 5, b = 3;
int c = a + b; // Pure Expression
2) Mixed Expression: Different types of operands are present.
E.g: int a = 5; double b = 3.75;
double c = a * b; // Mixed Expression
28
Types of Expression
3) Complex Expression: More than one operator is present.
E.g: int a = 5, b = 3, d = 15;
int c = a + b * d; // Complex Expression
4) logical Expression: Expression resulting into true or false
E.g: int a = 5; int b = 3;
boolean c = a > b; // a > b is Logical Expression
29
Data Conversion
▰ In a mixed expression the result can be in any one from of its
data types.
▰ Hence, it is needed to convert the various data types into a
single type.
▰ This is termed as Type conversion.
▰ Two types
I. Implicit type conversion
II. Explicit type conversion
30
Implicit Conversion
▰ The data type of the result gets converted automatically by the
compiler into its higher type without intervention of the programmer.
▰ It is also called as Implicit Casting OR automatic conversion OR
widening conversion OR coercion
▰ The memory size of Source datatype is smaller than the memory
size of the Destination datatype.
▰ Eg.
int a = 4; // int needs 4 bytes
double b = a; // double needs 8 bytes
[Link](a); // 4
[Link](b); // 4.0 31
Explicit Conversion
▰ When the data gets converted to another type after programmer
intervention, it is termed as Explicit type conversion.
▰ It is also called as Forced Conversion OR Type Casting OR
Narrowing
▰ The memory size of Source datatype is bigger than the memory
size of the Destination datatype.
▰ Eg.
double a = 4.12; // double needs 8 bytes
int b = (int)a; // int needs 4 bytes
[Link](a); // 4.12
[Link](b); // 4 32
Comment
▰ Comments are the non executable statement of Java program which
are ignored by the compiler
▰ There are 3 types of comments
➢ Single Line Comment : //
➢ Multiline Comment: /* ….
…..
…..*/
➢ Documentation Comment: /* * ….
…..
…..*/ 33