Home / Core Java / Introduction To Data Types In Java Programming Like Identifiers, Variables, Literals for Beginners

Introduction To Data Types In Java Programming Like Identifiers, Variables, Literals for Beginners

Share this Post with your Friends:

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.

Identifiers:
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:

  1. 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
  2. Identifiers can’t start with a digit
  3. Java identifiers are case sensitive
  4. There is no length Limit for java identifiers. But it’s not recommended to take more than 15 length
  5. Reserved words are can’t be used as Identifiers
  6. 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:

  1. Datatype123
  2. 4shared
  3. all@u
  4. Total_no_Values
  5.  _$_
  6. total#
  7. int
  8. _______ $
  9. ———–$
  10. 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)
float g=9.87F;

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 d=123.456;
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;

float f=1.3e2f;

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)
boolean b=002;

Character literals:

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 w=65535;
char g=65536; (compile time error)
char f=0xFACE;
char ch=0642;

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
char ch=’\uface’;

Every escape character is a char literal


Ex:
char ch=’\n’;

char ch=’\t’;
char ch=’\l’; (compile time error)

String Literals:

Any sequence of characters with in ” ” (double quotes) is called String Literal
Ex: String s=”Shakuntala”;

Data types:

Datatypes
Datatypes

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 declaration
Syntax of a Variable declaration

Examples:

Syntax of a Variable declaring and defining:

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.

 

Structure of a Class

Difference between static and non static variables
Difference between static and non static variables

Data types

Primitive Data Type
Primitive Data Type

Reference Data Type

Float Category data types

That’s all about Introduction To Data Types In Java Programming Like Identifiers, Variables, Literals for Beginners. Share if you like our article.

About Sohail

I am a professional blogger & digital nomad by choice. from Hyderabad, India. I write about Blogging, Regular job updates, Administrator @ Social media platforms, WordPress, SEO, and Web designing. I believe everyone should know about regular job updates, especially for unemployed and freshers. This is why I blog because it will make a difference to someone & that could be you.

Leave a Reply

Your email address will not be published.