0% found this document useful (0 votes)
17 views10 pages

Class 10 Java Notes-Unit 2

Uploaded by

nishantgowda63
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)
17 views10 pages

Class 10 Java Notes-Unit 2

Uploaded by

nishantgowda63
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
You are on page 1/ 10

UNIT 2- VALUES & DATA TYPES

Java Character Set


Character set is a set of valid characters that a language can recognise. A character represents any
letter, digit or any other sign.

Java uses the Unicode character set.

Unicode is a two-byte character code set that has characters representing almost all characters in
almost all human alphabets and writing systems around the world including English, Arabic, Chinese
and many more.

‘A’ - ‘\u0041’

‘B’ - ‘\u0042’

‘C’ – ‘\u0043’

The first 128 characters in the Unicode character set are identical to the common ASCII character
set.

The second 128 characters are identical to the upper 128 characters of the ISO Latin-1 extended
ASCII character set.

It’s the next 65280 characters that supports characters from other languages.

Java Token
Java Tokens are basic building block of a Java Program. Tokens are the
smallest individual part of a program

There are five types of java tokens. They are:

1. Keyword
2. Identifier
3. Literal
4. Separator
5. Operator

Keyword
Keyword is a reserved word which conveys a special meaning to the
language compiler.

Examples:

byte if break public


short else continue private
int switch void protected
long case final
float default static
double for volatile
char while class
Boolean do package

Note 1: all the keywords are in lower case.


Note 2: true, false and null are not keywords although they are reserved words.

Identifier
An identifier is the name given to different parts of the program like
variable name, function name, class name, package name, array name
etc.

Rules to form a valid identifier:

1. An identifier should contain only alphabets, digits, underscore and dollar ($) sign.
Valid: abc, abc123, abc123_, abc123_$, total_marks
Invalid: abc*123, total marks,

2. An identifier should not begin with a digit


Valid: abc123abc
Invalid: 123abc

3. An identifier is case
sensitive abc, Abc, ABc,
ABC, aBC, abC

4. An identifier should not be a keyword, true, false or null literals.


Valid: number, abc, abc1234, forfor, for123, $$for, PUBLIC
Invalid: for, if, while,

5. An identifier can be of any length


afjlkdjsalfjlsajflksjaflkjsalkfjdsljflsjalfkjalfwerqrewqrfdsgwsSFDAFDSAFSAwgs
rewt

Conventions:

1. A class name should be in TitleCase


Example: HelloWorld, NumberProgram, Student, CarPool

2. A variable name should be in camelCase.


Example: marks, totalMarks, totalMarksOfStudent
3. A function name should be in camelCase.
Example: findSum, findDiff, findProduct, calculateAverage

4. Constants must be in upper case and separated by underscore


Example: PI_VALUE, TAX_PER, MAX_MARKS

Note: final keyword is used to declare a constant. A variable can be converted into a constant by
using the final keyword.
Ex:

final double PI_VALUE = 22.0/7.0;

Literals
A literal is a constant data item which does not change.
Different kinds of literals

1. Integer Literal: A number with a positive or negative sign without a decimal point is
called an integer Literal
Example: 234, 23432, -324, +342
a. Decimal Integer Literal: An integer literal consisting of a sequence of digits is
taken to be decimal integer literal. Example: 234, 23432, -324, +342

b. Octal Integer Literal: A sequence of digits starting with 0 (digit zero) is taken to be an
octal integer. Example: 012, 077

c. Hexadecimal Integer Literal: A sequence of digits preceded by 0x or 0X is taken to


be an hexadecimal integer. Example: 0x41, 0xAB, 0x123

2. Floating Literal: A number with a decimal point having at least one digit before and after
the decimal point with a positive or negative sign is called as a floating literal.
Example:
Valid: 34.24, 3242.1323, +43.43, -22.53, 0.0
Invalid: .2342, 2342. , 0., .0
a. Fractional Form: A real literal in fractional form must have at least one digit before a
decimal point and at least one digit after the decimal point. It may also have either +
or – sign preceding it. A real literal with no sign is assumed to be positive.
3. Character Literal: One ASCII character enclosed within a pair of single quotes is
called a character literal.
Example:
Valid: ‘A’, ‘B’, ‘Z’, ‘a’, ‘z’, ‘ ’, ‘#’, ‘!’, ‘@’, ‘$’, ‘%’,
‘^’, ‘3’, ‘0’ Invalid: ‘AB’, ‘a@’, 2, ‘ “ ’, ‘ ’ ’, ‘’, ‘?’
Escape Sequence or backslash constant:
‘\’’, ‘\”’, ‘\?’, ‘\\’,

‘\n’  It’s a new line character – cursor moves to the beginning of next line
‘\t’  It’s a tab space character – cursor moves one tab space
‘\f’  form feed – cursor moves to the beginning of next page
‘\r’  carriage return – cursor moves to the beginning of the same line

4. String Literal: A group characters enclosed within a pair of double quotes is called as
String Literal
Example:
“Computer”, “Applications”, “”, “Computer
Application”, “[email protected]”, “Comp\tApp”
5. Boolean Literal: These are true or false
6. null literal: null is a literal which is used as default value of reference variable.

Separator
Separators are java tokens used to separate various tokens.

; , { } ( )[ ]

Operators
Operators are symbols used to perform various operations with the help of operands.
There two ways Operators can be categorized.
1. Based on number of operands
2. Based on functionality
1. Based on number of Operands:
There are 3 different categories of operators based on the number of Operands. They are:-
a. Unary Operator: An operator which requires only one operand to perform its operation.
Example:
i. Unary Plus (+)
ii. Unary Minus (-)
iii. Increment Operator (++)
iv. Decrement Operator (--)
v. Logical Not operator ( ! )
vi. Bitwise Not operator ( ~ )
b. Binary Operator: An operator which requires two operands to perform its operation.
i. The operand before the operator is called as Left operand
ii. The operand after the operator is called as Right operand
Example: +, -, *, /, %, >, >=, <, <=, ==, !=, &&, ||, &, |, ^, >>, >>>,
<<
c. Ternary Operator: An operator which requires three operands to perform its operation.
Example: Conditional Operator ( ? : )
2. Based on the operation:
There are six categories of operators based on the operation. They are:-
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Conditional Operators
5. Assignment Operators
6. Increment & Decrement
Operators
7. Bitwise Operators
8. Shift Operator
Data Type
A data type indicates the nature of data that can be stored.
There are two kinds of Data Types:
1. Primitive Data Type
byte, short, int, long, float, double, char and boolean
2. Reference Data Type
classes, interfaces, arrays

1. Four different kinds of data types to store integer literal

Data Type Size Description Range


byte 1 byte Byte-length integer -128 to + 127
short 2 bytes Short integer -32768 to + 32767
int 4 bytes Integer around -2 billion to + 2 billion
( -231 to + 231 – 1)
long 8 bytes Long integer ( -263 to + 263 – 1)

2. Two different kinds of data types to store floating literal

Data Type Size Description Range Remarks


float 4 bytes Single- -3.4E38 to Precision up to 6 digits.
precision +3.4E38 Examples: currency,
floating point temperature, percentage,
length.
double 8 bytes Double- -1.7E308 to Precision upto 15 digits.
precision 1.7E308 Example: Large numbers or
floating point high precision, such as for
astronomy or subatomic
physics.

3. One data type to store a character literal

Data Type Size Description Range Remarks


char 2 bytes Single 0 to 65535 Unicode
characters characters

4. One data type to store a Boolean literal

Data Type Size


Boolean 1 byte but uses only 1 bit

Note1: By default, any integer is of int data type, but if you want to make it a long integer then
postfix the number with L or l.
long a = 3242L;

Note2: By default, any floating-point literal is of double data type, but if you want to make it float,
then postfix the number with F or f.

float b = 3.14f;

Variable
A variable is a named memory location in which data can be
stored.
1. Declaration statement
Syntax:
<data_type> <varialb_name> ;
Examples:
int a; float
b; char c;
boolean d;

2. Initialization: Storing the data in a variable


a = 10;
b=
5.432f; c =
‘A’;
d = true;

Type Casting
The process of converting from one data type to another data type is
called as type casting.

There are two types of type casting:


1. Implicit Type casting
2. Explicit Type casting

Implicit Type Casting:


This is the process of converting from one data type to another data type
by the compiler without programmer’s intervention.
Also called as COERCION

Example:
int a = 55;
double b = 45.4;
double sum = a + b;

Explicit Type Casting:


This is the process of converting from one data type to another data type
by explicitly specifying the data type in the parenthesis just before the
variable or expression
Example:
int numOfBoys = 5;
int numOfGirls = 2;
double ratio = (double)numOfBoys / (double)numOfGirls

Implicit and Explicit Type Casting in the same expression

Example of both implicit and explicit type

casting int numOfBoys = 5;


int numOfGirls = 2;
double ratio = (double)numOfBoys/numOfGirls;

int numOfBoys =
5; int numOfGirls
= 2;
double ratio = numOfBoys/(double)numOfGirls;

char ch = 'A';
ch = (char)(ch + 1);

Static
Differences between static variable and non-static variables.

Note: Both static and non-static variables are MEMBER variables, which are defined outside all the
functions but inside the class.

static variable Non-static variables


1 Are also called as class variables Are also called as instance variables
2 A static keyword is used during declaration A static keyword is NOT used during
Example: declaration. Example:
static int numOfStudents; int numOfStudents;

3 There will be one copy of a static variable There will be one copy of an instance variable for
per class. every object (or every instance) of the class.
4 static variables can be accessed directly non-static variables can be accessed only by an
using a class name. object of a class.
Example: Example:
Student.numOfStudents Student stu = new Student();

5 Example: Example:

public class Student public class Student


{ {
String name; String name;
int age; int age;
double marks; double
static int numOfStudents; marks;
} static int numOfStudents;
}
In the above example numOfStudents is a In the above example name, age, marks are non-
static variable static variables

Decision Making Statements


Syntax What if the body contains only
one statement

if ( <expr> ) if ( <expr> ) where,


{ st1; if -> is a keyword
st1; st2; <expr> -> is a Boolean
… stn; expression st1, st2 ... stn -> are
} executable statements

if ( <expr> ) if ( <expr> ) where,


{ st1; if, else -> are keywords
st1; else <expr> -> is a Boolean
st2; st2; expression st1, st2, st3, st4 -> are
} executable statements
else
{
st3;
st4;
}

if ( <expr1> ) if ( <expr1> where,


{ ) st1; if, else -> are keywords
st1; else if ( <expr2 <expr> -> is a Boolean expression
st2; ) st2; st1, st2, st3, st4 ... st8 -> are
} else if ( <expr3 executable statements
else if ( <expr2 ) ) st3;
{ else
st4;
st3;
st4;
}
else if ( <expr3 )
{
st5;
st6;
}
else
{
st7;
st8;
}

switch ( <expr> ) Can’t write without curly braces


{
case const1 :

case const2 :
.
.
.
case constn :

default:
}

Difference between while and do while


while do while
1 Entry controlled loop Exit controlled loop
2 There is no semi colon (;) at the end of while There is a semi-colon (;) at the
end of do while
3 The body of the while loop may or may not be The body of the do while loop
executed even once will be executed at least once
4 Syntax: Syntax:
while( <condition> ) do
{ {
st1; st1;
st2; st2;
. .
. .
st n; st n;
} } while( <condition> );

Expressions
An expression can be either a Pure Expression, mixed Expression or Boolean Expression.

1. Pure Expression: When all the operands and the constants in an expression are of same data
type, i.e., either integers or floating-point literals then that kind of expression is called as pure
Expression.
int a, b, c;
c = a + b;

2. Mixed expression: When an expression contains operands and constants of various data
types, then they are called mixed expression.
int a, b;
double area;
area = a * b;

3. Boolean Expression: The expressions that result into false or true are called Boolean
expressions. The Boolean expressions are combination of constants, variables, logical and
relational operators.
(a + b) > c && (c+ d) > a

You might also like