The smallest element of a program which is meaningful to compiler is called token
Keywords are the special words that are basically reserved keywords in any programming language. We cannot use them as variables , class, method ,Identifiers
abstract for new enum super
assert goto package extends switch
boolean if private final synchronized
break implements protected finally this
byte import public float throw
case instance of continue while throws
catch int default return transient
char interface do short try
class long double static void
const native else strictfp volatile
Java Identifier
- Tokens that represents names
- Assigned to variables, methods ,classes to uniquely identity them
Characteristics
- can consists of uppercase, lowercase, digits, $ sign and _ underscore character
- Begins with letter, $ or -
- It is case sensitive
- Cannot be a keyword
- Can be of any length
Java Constants:
Constants in java are fixed values those are not changed during the Execution of program, a constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants, but the variable modifiers static and final can be used to effectively create one. Java supports several types of Constants those are :
1.Integer Constants
2,Real Constants
3.Single Character Constants
4.String Constants
5.Backslash Character Constants
Integer Constants:
Integer Constants refers to a Sequence of digits which Includes only negative or positive Values and many other things those are as follows
Decimal Integer Constants
Decimal constant consists of a set of digits 0 through 9, preceded by an optional –or + sign. Ex : 1, 3, 7, 8, 65, 543676664
Octal Integer Constants
An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0.
Ex: 038, 320, 0456, 0552, 0432
Hexadecimal Integer Constants
A hexadecimal integer constant consists of any combimation of digits from the set 0 through F, with an leading 0x or 0X.
Ex: 0x4, 0X456, 0x552, 0x32, 0x43
Real Constants
The quantities like distance, height, temperature, price, and so on need to be represented in decimals, these quantities may contain fractional parts or real parts like 56.890. Nmbers(constants) which are used to represent such quantities are called Real or Floating Point Constants.
Example
A Real Constant must have at Least one Digit
It must have a Decimal value
It could be either positive or Negative
If no sign is specified then it will be treated as Positive
Ex: 251.0, 234.890 etc are Real Constants
In The Exponential Form of Representation the Real Constant is Represented in the two Parts The part before appearing e is called mantissa whereas the part following e is called Exponent.
In Real Constant The Mantissa and Exponent Part should be Separated by letter e. The Mantissa Part have may have either positive or Negative Sign Default Sign is Positive.
Single Character Constants
A Character is Single Alphabet, a single digit or a Single Symbol that is enclosed within Single inverted commas.
Character Constant Can hold Single character at a time.
Contains Single Character Closed within a pair of Single Quote Marks
Integer Representation: Character Constant represent by Unicode
It is Possible to Perform Arithmetic Operations on Character Constants Ex: 'a', 'A', '1', '#', '-', '<', 'X' etc.
Declaring Single Variable
char variable_name;
Declaring Multiple Variables
char var1,var2,var3;
Declaring & Initializing
char var1 = 'A',var2,var3
String Constants
String is a Sequence of Characters Enclosed between double Quotes These Characters may be digits, Alphabets Like "Hello", "1234" etc.
The string is "Sequence of Characters".
String Constant is written in Pair of Double Quotes. The string is declared as Array of Characters.
Backslash Character Constants
Java Supports Backslash Constants those are used in output methods, for example \n is used for new line character, these are also called as "Escape Sequence" or Backslash Character Constants.
Although it consists of two characters, it represents a single character. Each escape sequence has Unicode value.
Each and Every combination starts with back slash They are non-printable characters.
Ex:
\b Backspace
\n New line
\r Carriage return
\t Horizontal tab
\" Double quote
\' Single quote
\\ Backslash
\v Vertical tab
\? Question mark
Java Literals
Literals
A literal is a fixed value that we assign to a variable. Literals in Java are similar to normal variables but their values cannot be changed once assigned. In other words, literals are constant variables with fixed values. These are defined by users and can belong to any data type. Java supports five types of literals which are as follows:
Integer
Floating Point
Character
String
Boolean
Ex:
public class GvpLiteral
{
public static void main(String[] args)
{
int gvp1 = 112; // Int literal
float gvp2 = 31.12f; //Float literal
char gvp3 = 'v'; // char literal
String gvp4 = "Gayatri"; // String literal
boolean gvp5 = true; // Boolean literal
System.out.println(gvp1); //112
System.out.println(gvp2); //31.40
System.out.println(gvp3); //vidya
System.out.println(gvp4); //Gayatri
System.out.println(gvp5); //true
}
}
Output:
112
31.12
v
Gayatri
true
Computer programs read data from input devices like keyboard, mouse etc. They process this input data and write it to an output device. Java stores the program data in variables.
Java program first declares the variables, reads data into these variables, executes operations on the variables, and then writes them somewhere again.
There are the following types of variables in Java basics:
- Local Variables
- Class Variables (Static Variables)
- Instance Variables (Non-static Variables)
1.Local Variable:
A variable declared inside the body of the method of a class is called local variable. You can use this variable only within that method and the other methods in the class aren't even aware that the variable exists. Their scope is limited to the method which means that You can’t change their values and access them outside of the method.
Note: A local variable cannot be defined with "static" keyword.
2.Instance Variable:
A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static. Each instance(objects) of class has its own copy of instance variable. It is called instance variable because its value is instance specific and is not shared among instances.
3.Static variable:
Static variables are also known as class variable because they are associated with the class and common for all the instances of class. A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable and share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the memory.
Ex:
class Diffvar
{
int a=10; //instance variable
static int c=30; //static variable
public static void main(String x[])
{
int b=20; //local variable
Diffvar iv = new Diffvar();
System.out.println("Instance Variable = " +iv.a);
System.out.println("Local Variable = " +b);
System.out.println("Static Variable = " +c);
}
} //end of class
Output:
Instance Variable = 10
Local Variable = 20
Static Variable = 30
No comments:
Post a Comment