Skip to content

Commit 64eed00

Browse files
committed
update note
1 parent 1e77dcc commit 64eed00

6 files changed

Lines changed: 89 additions & 0 deletions

File tree

Core JAVA.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,92 @@ Example:
912912
| Size | 4 bytes | 8 bytes |
913913
| Range | -1.7E38 to 1.7E38 | -1.7E308 to 1.7E308
914914

915+
---
916+
917+
<img src="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+
<img src="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 |
939+
| :------------------ | :--------------------------------------------- | :-------------------------- | :------------ | :----------- |
940+
| **Decimal Form** | Base-10 representation | 0-9 | `int x = 10;` | None |
941+
| **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.
947+
948+
---
949+
950+
## Literal
951+
952+
<img src="img/literal.JPG">
953+
954+
```
955+
+-----------+ +---------+ +-----------------------+ +---------------------+
956+
| data type | | keyword | | name of variable / | | constant value / |
957+
| |<--| |<--| identifier |<--| literal |
958+
+-----------+ +---------+ +-----------------------+ +---------------------+
959+
| | | |
960+
| | | |
961+
v v v v
962+
int x = 10 ;
963+
```
964+
965+
## This is the only possible way to specify lateral values for integral data types.
966+
967+
<img src="img/specify%20lateral%20values.JPG">
968+
969+
Here's a table representing valid and invalid syntax for integral literals, along with common errors,
970+
971+
| Syntax | Validity | Error (if invalid) | Explanation |
972+
| :--------------------- | :------- | :----------------------------------------------- | :--------------------------------------------------------------- |
973+
| `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. |
975+
| `int x = 0777;` | ✅ Valid || Valid octal literal. |
976+
| `int x = 0xFace;` | ✅ Valid || Valid hexadecimal literal (case-insensitive for a-f). |
977+
| `int x = 0XBeef;` | ✅ Valid || Valid hexadecimal literal. |
978+
| `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. |
983+
| `boolean b = true;` | ✅ Valid || Valid boolean literal. |
984+
| `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. |
987+
| `if (2)` | ❌ Invalid | `incompatible types: found: int, required: boolean` | `if` condition requires a boolean expression. |
988+
| `while (1)` | ❌ Invalid | `incompatible types: found: int, required: boolean` | `while` condition requires a boolean expression. |
989+
990+
---
991+
992+
<img src="img/JVM%2002.JPG">
993+
994+
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)
1001+
1002+
---
1003+

img/JVM 01.jpg

18.6 KB
Loading

img/JVM 02.JPG

20.3 KB
Loading

img/integral data types.JPG

33.7 KB
Loading

img/literal.JPG

17.4 KB
Loading

img/specify lateral values.JPG

12 KB
Loading

0 commit comments

Comments
 (0)