UNIT -3
Values and Data Types
Computer applications-Lorven public school, chanadapura.
Introduction
Each statement in java is formed by using different words called “Tokens”.
Each token is formed by valid characters.
Hence characters are the fundamental units of JAVA.
Computer applications-Lorven public school, chanadapura.
Character sets in JAVA
Character Sets
Letters Digits Delimiters Operators
Computer applications-Lorven public school, chanadapura.
Letters: A-Z AND a-z
Digits : 0-9
Delimiters: these are the special characters .
Braces- (),[],{}
Dot- .
Comma- ,
Semicolon: ;
Operators: special symbols used to perform operations .
Arithmetic operators: +, -, *,/,% etc..
Logical operators: &&,||,!
Relational operators: <,>,<=,>= etc..
Computer applications-Lorven public school, chanadapura.
Encoding of characters
Characters that we enter are converted to machine language, i.e. it is stored
in binary form.
It is converted using encoding techniques.
There are 2 types of encoding techniques, they are
ASCII-American standard code for information exchange.
Unicode
Each character has a code I any encoding technique.
Computer applications-Lorven public school, chanadapura.
ASCII
Computer applications-Lorven public school, chanadapura.
Unicode
Unicode provides a unique number for every character,
no matter what the platform,
no matter what the program,
no matter what the language.
The code contains hexadecimal digits ranging from 0x0000 to FxFFFF
Computer applications-Lorven public school, chanadapura.
Escape sequences
Computer applications-Lorven public school, chanadapura.
Tokens
Keywords
Identifiers
Literals
Operators
Punctuators
Separators
Assignment
Computer applications-Lorven public school, chanadapura.
Keywords
Keywords in Java are predefined or reserved words that have special meaning
to the Java compiler.
Computer applications-Lorven public school, chanadapura.
Identifiers
Computer applications-Lorven public school, chanadapura.
Literals
Computer applications-Lorven public school, chanadapura.
Assignment
Computer applications-Lorven public school, chanadapura.
Operators
An operator in Java is a special symbol that signifies the compiler to perform
some specific mathematical or non-mathematical operations on one or more
operands.
Computer applications-Lorven public school, chanadapura.
Punctuators and separators
Computer applications-Lorven public school, chanadapura.
Computer applications-Lorven public school, chanadapura.
Primitive data types
Declaration vs initialization
int x;
x=2;
Default initialization
Computer applications-Lorven public school, chanadapura.
Static vs dynamic initialization
Computer applications-Lorven public school, chanadapura.
Non-primitive data-types
These are derived by primitive data types.
They are also called derived types reference types and composite types.
Here instead of storing data these data types store reference to memory
location, where actually the data is stored.
Computer applications-Lorven public school, chanadapura.
Type conversion
Implicit type conversion
conversion takes place when two data types are automatically converted.
This happens when
The two data types are compatible.
When we assign value of a smaller data type to a bigger data type.
Byte char short int long float double
Example: int a; long b, c;
c= a + b;
Here the answer is automatically converted to long.
Computer applications-Lorven public school, chanadapura.
Type conversion
Explicit type conversion
If we want to assign a value of larger data type to a smaller data type we
perform explicit type casting or narrowing.
This is useful for incompatible data types where automatic conversion cannot
be done.
Here, target-type specifies the desired type to convert the specified value to.
Computer applications-Lorven public school, chanadapura.
example
public class MyClass
{
public static void main(String[] args)
{
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
}
}
Computer applications-Lorven public school, chanadapura.