Java Notes 10th ICSE Saravanan Ganesh 13
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 1
Java Token Java Tokens are basic building block of a Java 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 2
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 afjlkdjsalfjlsajflksjaflkjsalkfjdsljflsjalfkjalfwerqrewqrfdsgwssfdafdsafsawgsrewt Conventions: 1 A class name should be in TitleCase HelloWorld, NumberProgram, Student, CarPool 2 A variable name should be in camelcase marks, totalmarks, totalmarksofstudent 3 A function name should be in camelcase findsum, finddiff, findproduct, calculateaverage 4 Constants must be in upper case and separated by underscore 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 = 220/70; 3
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 234, 23432, -324, +342 a Decimal Integer Literal: An integer literal consisting of a sequence of digits is taken to be decimal integer literal 234, 23432, -324, +342 b Octal Integer Literal: A sequence of digits starting with 0 (digit zero) is taken to be an octal integer 012, 077 c Hexadecimal Integer Literal: A sequence of digits preceded by 0x or 0X is taken to be an hexadecimal integer 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 Valid: 3424, 32421323, +4343, -2253, 00 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 b Exponential Form: A real literal in exponent form consists of two parts: mantissa and exponent For instance 58 * 10 205 can be represented as 058E206 where 058 is mantissa and 206 is exponent 3 Character Literal: One ASCII character enclosed within a pair of single quotes is called a character literal Valid: A, B, Z, a, z,, #,!, @, $, %, ^, 3, 0 Invalid: AB, a@, 2,,,,? Escape Sequence or backslash constant: 4 \, \, \?, \\, \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 Computer, Applications,, Computer Application, tech253545@gmailcom, 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 ;, ( ) [ ] 5
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 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 +, -, *, /, %, >, >=, <, <=, ==,!=, &&,, &,, ^, >>, >>>, << c Ternary Operator: An operator which requires three operands to perform its operation 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 Operators 6
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 ( -2 31 to + 2 31 1) long 8 bytes Long integer ( -2 63 to + 2 63 1) 2 Two different kinds of data types to store floating literal 7 Data Type Size Description Range Remarks float 4 bytes Singleprecision floating point -34E38 to +34E38 Precision up to 6 digits Examples: currency, temperature, percentage, double 8 bytes Doubleprecision floating point -17E308 to 17E308 length Precision upt to 15 digits Large numbers or 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 Boolean Size 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 = 314f; 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; 8 2 Initialization: Storing the data in a variable a = 10; b = 5432f; 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 int a = 55; double b = 454; 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 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; 9 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 static int numofstudents; A static keyword is NOT used during declaration int numofstudents; 3 There will be one copy of a static variable per class 4 static variables can be accessed directly using a class name StudentnumOfStudents 5 public class Student String name; int age; double marks; static int numofstudents; In the above example numofstudents is a static variable There will be one copy of an instance variable for every object (or every instance) of the class non-static variables can be accessed only by an object of a class Student stu = new Student(); public class Student String name; int age; double marks; static int numofstudents; In the above example name, age, marks are nonstatic variables 10
Decision Making Statements Theory Syntax if ( <expr> ) st1; st2; stn; What if the body contains only one statement if ( <expr> ) st1; where, if -> is a keyword <expr> -> is a Boolean expression st1, st2 stn -> are executable statements if ( <expr> ) st1; st2; else st3; st4; if ( <expr> ) st1; else st2; where, if, else -> are keywords <expr> -> is a Boolean expression st1, st2, st3, st4 -> are executable statements 11
if ( <expr1> ) st1; st2; else if ( <expr2 ) st3; st4; else if ( <expr3 ) st5; st6; else st7; st8; if ( <expr1> ) st1; else if ( <expr2 ) st2; else if ( <expr3 ) st3; else st4; where, if, else -> are keywords <expr> -> is a Boolean expression st1, st2, st3, st4 st8 -> are executable statements switch ( <expr> ) case const1 : case const2 : case constn : Can t write without curly braces default: 12
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 4 Syntax: while( <condition> ) st1; st2; st n; will be executed at least once Syntax: do st1; st2; st n; while( <condition> ); 13
ASCII Table 14
Range for unsigned numbers 15 Number of bits(n) Possible values (Binary) 1 0 1 2 00 01 10 11 3 000 001 010 011 100 101 110 111 4 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 5 00000 11111 6 000000 111111 7 0000000 1111111 Possible values (Decimal) 0 1 0 1 2 3 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 31 0 63 0 127 Range General Formulae 0 to 1 0 to 2 n -1 0 to 3 0 to 2 n -1 0 to 7 0 to 2 n -1 0 to 15 0 to 2 n -1 0 to 31 0 to 2 5-1 0 to 63 0 to 2 6-1 0 to 127 0 to 2 7-1
8 00000000 11111111 16 0000000000000000 1111111111111111 0 255 0 65535 0 to 255 0 to 2 8-1 0 to 65535 0 to 2 16-1 Range for Signed Number byte 8-128 to 127-2 n-1 to +2 n-1-1 -2 8-1 to +2 8-1 -1-2 7 to +2 7-1 short 16-32768 to +32767-2 16-1 to +2 16-1 -1-2 15 to +2 15-1 int 32 (around) -2 billion to + 2 billion -2 32-1 to +2 32-1 -1-2 31 to +2 31-1 long 64-10E18 to +10E18-2 64-1 to +2 64-1 -1-2 63 to +2 63-1 16
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, ie, 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 17