Java Literal: Boolean, whole Numbers, Floating point numbers

Java Literal:

Java Literal:- If you have a statement in a Java program such as int i=27; then the Java compiler must correctly use the number 27 as an integer number. Such expressions – be it for numbers, signs or Boolean Values – are called java Literal. Any elementary data type of Java knows its own Literal. In simple cases, as in the above example i=27, the use of Java Literal so obvious that a long description is unnecessary. But you’ll see right away that there are several special cases.

Note

Java Literal for string and char: There are even more special cases than numbers in character strings. Literal for the elementary data type char and for the String class.



Boolean java Literal

The topic”boolean Literal”is quickly settled: There are only two Java Literal, true, and false.

Whole Numbers Literal:

In the simplest case, simply specify the number in decimal. As long as you have a number range, this works for all integral Data types except long. long numbers that exceed the int range, you must explicitly set by a trailing l or L as long-literal indicate. (Hint: Use L! The lowercase letter l is very easy with the number 1.)

int i1 = 2000000000 , i2 = -198765432;

short s1 = 32000 , s2 = -32000;

byte b1 = -128, b2 = 127;

long l1 = 12345678901234 L;

Basically, Java considers any number without a decimal point as an int-Number. As the above listing has shown, it is sometimes necessary to explicitly set the data type in the java Literal. This possibility exists not only for long, but also for other number types (see below Table).

Java Literal Data type
123 Int
123L, 123l Long
123F, 123f Float
123D, 123d double


The decimal number system uses 10 as the base. In the IT you have but often also to do with hexadecimal, octal and binary numbers that as base 16, 8 or 2. In the Java code, you set hexadecimal Numbers 0x ahead, octal numbers simply 0 (i.e. a zero) and binary numbers 0b:

int i = 23; // decimal

i = 0xA0; // hexadecimal , value 160

i = 023; // octal, value 19

i = 0b1001; // binary, value 9

In order to increase the readability of long numbers, you may, since Java 8, Insert underscores into the numbers:

int i = 2 _000_000_000 ; // 2 billion

long l1 = 0 x1234_5678_9abc_def0L ; // 1311768467463790320

All integral data types in Java have signs. In hardware seams However, you may need to include programs in a byte variable store a value greater than 127, or store a value in an int variable greater than 231. This is possible if you place the casting operator in front:

byte b = (byte )240;

short s = (short)0xFF00;

int i = (int)0xFFFF_FFFF_FFFF_FFFFL ;

System.out. println(b); // Output: -16

System.out. println(s); // Output: -256

System.out. println(i); // Output: -1



floating-point numbers java Literal:   

When a number is a decimal point or the letter e or E with, Java regards the number as a double Literal. The equivalent letters e and E are used to select very small or large Write numbers in exponential notation. 1.23e4 or 1.23E4 means 1,23 ? 103 = 1230.If you explicitly set a float number with reduced For accuracy, you must specify the letter f or F Place behind:

double d1 = 1.23;

double d2 = 1.23 E3;

float f = 1.23 f;

System.out. println(d1); // Output: 1,23

System.out. println(d2); // Output: 1230,0

System.out. println(f); // Output: 1,23



Related Article:

Java internal byet code, jvm, library and Hello World program explanation how it works

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: