Reputation: 399
What is the difference between data types and literals in Java?
Upvotes: 37
Views: 50840
Reputation: 31
A literal is a fixed value that is assigned to a variable (of a specific data type) directly without using a constructor
For eg:
String var1 = "Java"; -- here "Java" is a literal
String var2 = new String("Java"); -- here "Java" is not a literal
Upvotes: 2
Reputation: 1252
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.
Upvotes: 4
Reputation: 101
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
Upvotes: 2
Reputation: 68942
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";
Upvotes: 4
Reputation: 413
Values like 1.5, 2, 3.13, “hello” that appear directly in a program are known as literals.
Upvotes: 5
Reputation: 157
Data Type : it defines the memory assignment for different "types" available in java.
source http://javawebtutorial.blogspot.in/2013/10/data-types-in-java-as-you-all-know-that.html
Literals : Literals in java define the actual value we can using for variables, constants or to perform any operation.
source : http://javawebtutorial.blogspot.in/2013/10/literals-in-java-literals-in-java.html
Upvotes: 0
Reputation: 22116
Data types are just, well, different types of data, like String
s or float
s or ArrayList
s. In Java, most data types are classes. Edit although according to one of the other answers, I think maybe the term "data type" might be used more for primitives, i.e. types that are not classes.
A literal is a way of expressing a value without having to create it using a constructor. For example, if you just put 3
in your code, it means the int
3. You don't have to say new Integer(3)
or anything like that.
By the way, may I recommend the official Java Tutorials.
Upvotes: 1
Reputation:
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
Upvotes: 46
Reputation: 114767
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
]
Upvotes: 21
Reputation: 262474
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"
.
Upvotes: 5