In Java, a literal is an exact representation of a value. Literals differ from variables as the value of a variable can change, but what a literal represents cannot.
There are 4 types of integer literals in Java: binary (base 2) decimal (base 10) octal (base 8)
Literals are raw values or data that are stored in a variable or constant. Variables are mutable, i.e., their values can be changed and updated. Constants are immutable, i.e. their values cannot be changed or updated. Literals are both mutable or immutable depending on the type of literal used.
From Java Data types tutorial
Data types :
Primitive types are special data types built into the language; they are not objects created from a class
Literal :
A Literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation
boolean result = true;
boolean - is data type
true - is literal
String string = "Hello World";
< 1 > < 2 > < 3 >
1 is a data type, 2 a variable name, 3 a (String) literal
From the JLS:
A literal is the source code representation of a value of a primitive type [like
1
,true
,'t'
or1.2f
], the String type [like""
orSomething
], or the null type [null
]
I don't know that they have enough in common to be able to identify differences, but data types are things like int
, float[]
, Object
, and literals are something like 1
, { 1.0f, 2.0f}
, "abcdef"
.
Values like 1.5, 2, 3.13, “hello” that appear directly in a program are known as literals.
A literal is a constant value which is compatible to a datatype, a literal is used to assign a value to variable, to compare values or define constants. See JLS 3.10.
e.g:
int varOfDataTypeInt = 123;
String s = "string literal";
Int (Data type) x (Variable) = 100 (Literals) ;
Data type :- Data type means type of data ,it may be byte
, short
, int
, long
, float
, double
,char
boolean
and many other user defined type(Class) like Employee, Student etc...
Literals :- The value we assign to variable is called Literal .
e.g:- String str= "India";
Here "india" is string Literal .
Literals are fixed value for a variable until they are not assign by other variable.
true
, false
and null
are reserved word in java. Technically they are literals values and not keywords .However they can not be used as Identifier because they have specific meaning to the java Compiler.
Data Type: Are nothing but a reserved memory location to store values. meaning when you create a variable you reserve some space in memory.
Literal: Is the source code representation of a fixed value, a Given or Constant value. Ex: boolean result = true
, String s1 = "Hello World"
.
boolean - is data type, result - is variable, true - is literal
String - is Object data type, s1 - is variable, "Hello World" - is literal
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With