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

前端 未结 10 1681
旧时难觅i
旧时难觅i 2021-01-30 08:59

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

相关标签:
10条回答
  • 2021-01-30 09:29

    Values like 1.5, 2, 3.13, “hello” that appear directly in a program are known as literals.

    0 讨论(0)
  • 2021-01-30 09:30

    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.

    0 讨论(0)
  • 2021-01-30 09:34

    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

    0 讨论(0)
  • 2021-01-30 09:40

    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".

    0 讨论(0)
  • 2021-01-30 09:43
     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]

    0 讨论(0)
  • 2021-01-30 09:44

    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";
    
    0 讨论(0)
提交回复
热议问题