Chapter 2: Introduction to Identifiers, Variables, Literals, Data types in Java Programming for Beginners
Data types in java programming like Identifiers, Variables, Literals explanation is provided below for more clarity.
A name in the java program is called an identifier, it can be class name (or) variable name or method name.
Rules to define identifiers:
- They allowed characters in java identifiers are (a to z, A to Z, 0 to 9, _ ,$)
If we use any other things we will get Compile time error
- Identifiers can’t start with a digit
- Java identifiers are case sensitive
- There is no length Limit for java identifiers. But it’s not recommended to take more than 15 length
- Reserved words are can’t be used as Identifiers
- All the Predefined Java class names & interface names we can use as identifiers. Even though it is legal. but, it is not recommended
Check the following Example identifiers:
- _______ $
- My Demo
Literals: A constant value which can be assign to the variable is called “Literal”, Ex: int x=10;
Literals can be divided into Integral literals, floating point literals, character literals, Boolean literals, String literals.
Integral literals: For the Integral datatypes (byte, short, int, long) the fallowing are various ways to specify the Literal value
1.1. Decimal literals: Allowed digits are from 0 to 9, and the first digit is from 1 to 9 and rest of digits are from 0 to 9.
Ex: int x=12;
1.2. Octal literals: Allowed digits are from 0 to 7, Literal value should be prefixed with “0” zero, and rest of digits are from 0 to 7.
Ex: int x=010;
1.3. Hexadecimal literals: Allowed digits are 0 to 9, a to f, A to F. we can use both upper and lowercase. Literal value should be prefixed with 0X or 0x.
Ex: int n=ox10; (or) int n=0X10;
Floating point literals: Every floating point literal is by default double type & hence we can’t assign directly to float variable. But we can specify explicitly floating point literal is the float type by suffixing with ‘f’ or ‘F’.
Ex: float x=1.23; (not possible)
float f1=12.345f; ( possible)
We can specify floating point literal explicitly as double type by suffixing with d or D
Ex: double d=123.345D;(possible)
float f=123.4567d; (not possible)
NOTE: We can specify floating point literal only in decimal form and we can’t specify in Octal and hexadecimal form
double c=0x123.456;(compile time error)
We can assign Integral literal directly to the floating point datatypes and that Integral literal can be specified either in decimal form or Octalform or Hexadecimalform.But we can’t assign floating point Literals directly to the Integral Literals
Ex: int i=123.23; (compile time error)
We can specify floating point even in scientific form also(exponential form)
Ex: double d=1.2e3;
Boolean literals: The only possible values for the boolean datatypes are true/false.
Ex: boolean s=true;
boolean a=”true”;( compile time error)
boolean c=”True”;(compile time error)
A char literal can be represented as single character with in single quotes
Ex: char ch=’a’;
char c=’ab’;( compile time error)
A char literal can be represented as integer literal which represents unicode of the character
We can specify Integral literal either in decimal form or octal form or Hexa decimal form.But allowed range 0 to 65535.
Ex: char q=97;
char g=65536; (compile time error)
A char literal can be represented in unicode representation,which is nothing but \U xxxx 4-digit hexadecimal number.
Ex: char ch=’\u0061′;
char ch=’\uabcd'(compile time error) i.e ; missing
Every escape character is a char literal
Ex: char ch=’\n’;
char ch=’\l’; (compile time error)
Any sequence of characters with in ” ” (double quotes) is called String Literal
Ex: String s=”Shakuntala”;
VARIABLES in Java
VARIABLES in java is an identifier whose value will be modified during execution of the java program.
Practices for writing variables:
- The First letter must be an alphabet.
- The length of the variable should not exceed more than 32 characters.
- No specific symbols are allowed except underscore.
- No keywords should use as variable names.
Types of variables in JAVA
Syntax of a Variable declaration:
Syntax of a Variable declaring and defining:
Syntax of a Variable declaring and definingWhile developing any JAVA program that will be developed with respect to class only. In a class, we can use ‘n’ number of data members and ‘n’ number of methods. Generally, in JAVA, we can use two types of data variables or members. They are Instance / Static and non-static variables.
That’s all about Introduction To Data Types In Java Programming Like Identifiers, Variables, Literals for Beginners. Share if you like our article.