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 all characters in almost all languages and writing systems around the world.
In Java program, all characters are grouped into symbols called tokens. OR the smallest individual unit in a program is known as a token. Tokens are- identifier, keyword, separator, operator, literal and comment.
The first category of token is an Identifier. Identifiers are used by programmers to name things in Java such as variables, methods, fields, classes, interfaces, exceptions, packages, etc. All characters in an identifier are significant, including the case (upper/lower) of the alphabetic characters. Eg: The identifier Count and count denote different names in Java
Identifiers are the names given to different part of the program viz. variables, objects etc. Forming Rules Identifiers can have alphabets, digits and underscore and doller sign characters. They must not be a keyword or boolean literal or null literal. They must not begin with digit. They can be of any length. Java is case sensitive i.e. Upper case and lower case letters are treated differently.
Keywords are the second category of tokens in Java. Keyword sometimes also called as reserved word. Keywords have inbuilt meaning and the programmers cannot use for other things. Below are the list of all keywords in Java.
The third category of token is a Separator. ;,. ( ) { } [ ] Eg: Math.max(count,limit); In the above segment the identifiers are Math, max, count, limit. And the separators are. (, ) ;
The fourth category of token is operator. The keywords instanceof and new are the part of operators. Below is the list of the operators in Java.
The fifth category of tokens is literals. The values written in Java are called literals. Example: 1, 3.14, “Hello”, true, ‘a’, null. int Literals: Literal of the primitive type int represent countable, discrete quantities. 0 is a non zero digit. 1,2,3,4……
double Literals: Literal of the primitive type double represent measureable quantities. Like real numbers in mathematics, they can represent fractions and numbers with decimal places.
boolean literals: boolean literal gives one of the two results- true or false. char literal: This is the first literal of text type. char literal is enclosed within the single quotes. Eg: ‘X’, ‘a’, ‘ ‘
String literals: The second type of text literal is a String. Example: “Enter your name”. This is the only literal that is not of primitive type. Note that the string is different from char. String- “X” char – ‘x’
Sometimes you will see an escape- sequence inside the single-quotes for a character-literal or one or more inside double- quotes for a string-literal (see above); each escape sequence is translated into a character that prints in some "special" way. Some commonly used escape sequences are:
\n – new line \t – horizontal tab \v – vertical tab \b – backspace Eg: If we output "Pack\nage", Java would print on the console Pack age
null literal: special kind of literal that is used to represent a special value. Comments : The sixth and final category of tokens is the Comment. Comments allow us to place any form of documentation inside our Java code. They can contain anything that we can type on the keyboard comment.
Comments: Comments are declared in two ways: Line-Oriented: begins with // and continues until the end of the line. Block-Oriented: begins with /* and continues (possibly over many lines) until */ is reached.
There are two types of data types: 1. Primitive data types 2. Non-primitive data types Primitive data types: There are eight primitive data types supported by Java.
Size: 1 byte. Range: -128 to 127. Default value: 0. Example: byte a = 100;
Size: 2 bytes Range: -32,768 to 32,767 Default value: 0 Example: short s = 1000;
Size: 4 bytes Range: -2,147,483,648 to 2,147,483,647 Default value: 0 Example: int a = 10000;
Size: 8 bytes Range: --9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Default value: 0 Example: long a=100000L;
Size: 4 bytes Default value: 0.0f Example: float f1 = 3.14f;
Size: 8 bytes Default value: 0.0d Example: double d1= 3.14d;
boolean data type represents one bit of information. There are only two possible values: true and false. This data type is used for simple flags that track true/false conditions. Default value is false. Example: boolean one = true
Size: 2 bytes Range: 0 to 65,535 Example: char a=‘A’;
Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. Example: Animal animal = new Animal(“dog");
A variable is a named memory location, which holds a data value of a particular data type.
int age; float principalAmount; int a; double b;
int age = 50; float principalAmount = 5000; int a = 10; double b = 20.0;
int age = 50; int b = age * 10; int c = age;