开发者

What is the difference between data types and literals in Java?

What is the difference between data types and litera开发者_Python百科ls in Java?


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'or 1.2f], the String type [like "" or Something], 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


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


Data types are just, well, different types of data, like Strings or floats or ArrayLists. 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜