Data Type | Description | Size | Default Value |
boolean | true or false | 1-bit | false |
char | Unicode Character | 16-bit | \u0000 |
byte | Signed Integer | 8-bit | (byte) 0 |
short | Signed Integer | 16-bit | (short) 0 |
int | Signed Integer | 32-bit | 0 |
long | Signed Integer | 64-bit | 0L |
float | Real number | 32-bit | 0.0f |
double | Real number | 64-bit | 0.0d |
- Declaring an uninitialised variable at class level is valid and it takes a default value of 0 or null
- Declaring an uninitialised local variable and referencing/using it is allowed (compilation error - variable is not initialized)
Representing different number system : int decVal = 26; // The number 26, in decimal
int octVal = 032; // The number 26, in octal
int hexVal = 0x1a; // The number 26, in hexadecimal
int binVal = 0b11010; // The number 26, in binary
Representing float :
double d1 = 123.4;
double d2 = 1.234e2; // same value as d1, but in scientific notation
Underscore :
The following example shows other ways you can use the underscore in numeric literals:
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
You can place underscores only between digits; you cannot place underscores in the following places:
* At the beginning or end of a number
* Adjacent to a decimal point in a floating point literal
* Prior to an F or L suffix
* In positions where a string of digits is expected
The following examples demonstrate valid and invalid underscore placements in numeric literals:
float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point
long socialSecurityNumber1
= 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix
int x1 = _52; // This is an identifier, not a numeric literal
int x2 = 5_2; // OK (decimal literal)
int x3 = 52_; // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2; // OK (decimal literal)
int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2; // OK (hexadecimal literal)
int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number
int x9 = 0_52; // OK (octal literal)
int x10 = 05_2; // OK (octal literal)
int x11 = 052_; // Invalid; cannot put underscores at the end of a number
No comments:
Post a Comment