Literals in JAVA
Literals in JAVA
First to know literals we got to look at the Primitive Data Types
- PRIMITIVE DATA TYPES:
JAVA is statically typed (Variables must be declared before use). There are 8 Primitive Data Types supported by JAVA:
- Integral (int)
- byte ---
- Value ranges from -128 to 127.
- Takes 1 byte.
- Default value is 0.
- short ---
- Value ranges from [ -(2^16)/2 to (2^16)/2 -1 ]
- Takes 2 bytes.
- Default value is 0.
- int ---
- Value ranges from [ -(2^32)/2 to (2^32)/2 -1 ]
- Takes 4 bytes.
- Default value is 0.
- long(L) ---
- Value ranges from [ -(2^64)/2 to (2^64)/2 -1 ]
- Takes 8 bytes.
- Default value is 0.
- Decimal
- char (' ') ---
- Value ranges from [ 0 to 65535(2^10-1) ]
- Takes 2 bytes. (because it supports unicode)
- Default value is
'\u0000'(or 0)
- boolean ---
- Value can be true or false.
- Size depends on JVM.
- Default value is false.
In order to choose datatypes we first need to find the type of data that we want to store. After that we have to analyse the min-max value we might use.
Literals:
A constant value which can be assigned to the variable is called a s a literal.
101 - Integer Literal
10.1f - Float Literal
10.1 - Double Literal (default type for decimal values)
'A' - Character Literal
True - Boolean Literal
"Coder" - String Literal
Code:
package com.company;
public class Tut3 {
public static void main(String[] args) {
byte bt1 = 30;
int int1 = 45;
short st1 = 100;
long l1 = 65656434735L;
char c1 = 'a';
float f1 = 85.45f;
double d1 = 586.46556d;
boolean b1 = true;
String str = "Programmer";
//Use print function to run the codes above
}
}
Keyword:
Words which are reserved and used by the JAVA compiler.They cannot be used as an identifier.
Go and check official website for reserved keyword --- CLICK HERE
Comments
Post a Comment