You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Core JAVA.md
+89Lines changed: 89 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -912,3 +912,92 @@ Example:
912
912
| Size | 4 bytes | 8 bytes |
913
913
| Range | -1.7E38 to 1.7E38 | -1.7E308 to 1.7E308
914
914
915
+
---
916
+
917
+
<imgsrc="img/JVM%2001.jpg">
918
+
919
+
In C/C++, this two are allow because of.
920
+
921
+
-`int` values can implicitly convert to `boolean` contexts (0 is false, non-zero is true). This allows `if(x)` (where `x` is an integer) and
922
+
-`while(1)` (an infinite loop) to compile.
923
+
924
+
Java's "powerful compiler" enforces **stronger type checking**. It does **not** allow implicit conversions from `int` to `boolean`. The `if` and `while` conditions **must** explicitly evaluate to a `boolean` type.
925
+
926
+
---
927
+
928
+
# char = 1 byte (in C/C++) 🆚 char = 2 byte (in JAVA) ?
929
+
930
+
> Java's `char` is 2 bytes because it uses **Unicode (specifically UTF-16)** to represent a vast range of characters from almost all writing systems worldwide. C and C++ typically use 1-byte `char` which is sufficient for **ASCII** (or extended ASCII), primarily designed for English and Western European languages (256 characters max). Java's design prioritizes global character support from its inception.
931
+
932
+
---
933
+
934
+
<imgsrc="img/integral%20data%20types.JPG">
935
+
936
+
Specify literal values for integral data types (byte, short, int, long) in Java:
937
+
938
+
| Basis/Form | Description | Digits/Characters Used | Example (int) | Valid Prefix |
|**Octal Form**| Base-8 representation | 0-7 |`int x = 010;`|`0`|
942
+
|**Hexadecimal Form**| Base-16 representation | 0-9 and a-f (or A-F) |`int x = 0x10;`|`0x` or `0X`|
943
+
|**Binary Form**| Base-2 representation (Java 7+ feature) | 0-1 |`int x = 0b10;`|`0b` or `0B`|
944
+
945
+
> While Java is generally case-sensitive, for hexadecimal literals (e.g., 0x or 0X followed by digits), the letters 'A' through 'F' (representing values 10-15) are case-insensitive. </br>
946
+
> This means you can use either uppercase or lowercase letters for the hexadecimal digits A through F. Both 0xDEADBEEF and 0xdeadbeef (or even mixed case like 0xDeAdBeEf) are valid and represent the same integer value.
|`int x = 10;`| ✅ Valid | — | Standard decimal literal. |
974
+
|`int x = 0786;`| ❌ Invalid |`integer number too large`| Octal literals (prefixed with `0`) can only contain digits 0-7. `8` and `6` are invalid in octal. |
|`int x = 0XBeer;`| ❌ Invalid |`';' expected` or `illegal character: 'r'`|`r` is not a valid hexadecimal digit (0-9, a-f). |
979
+
|`int x = 2147483647;`| ✅ Valid | — | Max value for `int`. |
980
+
|`int x = 2147483648;`| ❌ Invalid |`integer number too large`| Exceeds the max value for `int`. |
981
+
|`int x = 2147483648l;`| ❌ Invalid |`possible loss of precision, found: long, required: int`|`l` makes it a `long` literal, which cannot be implicitly assigned to an `int` if it's too large for `int`. |
982
+
|`int x = true;`| ❌ Invalid |`incompatible types: found: boolean, required: int`| Boolean cannot be implicitly converted to int. |
|`boolean b = 0;`| ❌ Invalid |`incompatible types: found: int, required: boolean`| Integer cannot be implicitly converted to boolean. |
985
+
|`boolean b = True;`| ❌ Invalid |`cannot find symbol, symbol: variable True`|`True` (with uppercase 'T') is not a keyword; boolean literals are `true` or `false` (lowercase). |
986
+
|`boolean b = "true";`| ❌ Invalid |`incompatible types: found: java.lang.String, required: boolean`| String literal cannot be assigned to a boolean. |
When you write integral literal values in your Java code (e.g., `10`, `010`, `0x10`, `0b10`), the **Java compiler and eventually the JVM (Java Virtual Machine) always convert these into their standard binary representation (which corresponds to their decimal value)** for internal storage and processing.
995
+
996
+
For instance:
997
+
*`int x = 10;` (Decimal 10)
998
+
*`int y = 010;` (Octal 10, which is Decimal 8)
999
+
*`int z = 0x10;` (Hexadecimal 10, which is Decimal 16)
1000
+
*`int a = 0b1010;` (Binary 1010, which is Decimal 10)
0 commit comments