Skip to content

Commit 3c1f0de

Browse files
authored
first commit
first commit
1 parent 82a72de commit 3c1f0de

22 files changed

+2416
-2
lines changed

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,37 @@
1-
# Java-Data-Structures
2-
A repository for understanding Java data structures. Covers primitive data types (e.g., int, float, double, char) and high-level data structures like arrays, linked lists, stacks, queues, trees, and maps, with detailed explanations and usage examples.
1+
# Java-Data-Structures
2+
A repository for understanding Java data structures. Covers primitive data types (e.g., int, float, double, char) and high-level data structures like arrays, linked lists, stacks, queues, trees, and maps, with detailed explanations and usage examples.
3+
4+
---
5+
6+
**Java-Data-Structures-Guide/**<br>
7+
│<br>
8+
├── README.md<br>
9+
│<br>
10+
├── [*primitive-data-types/*](./primitive-data-types/)<br>
11+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [*primitive-types/*](./primitive-data-types/primitive-types/)<br>
12+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [**`boolean`**](./primitive-data-types/primitive-types/boolean/) [<sub>BooleanExample.java</sub>](./primitive-data-types/primitive-types/boolean/BooleanExample.java)<br>
13+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [**`char`**](./primitive-data-types/primitive-types/char/) [<sub>CharExample.java</sub>](./primitive-data-types/primitive-types/char/CharExample.java)<br>
14+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [**`int`**](./primitive-data-types/primitive-types/int/) [<sub>IntExample.java</sub>](./primitive-data-types/primitive-types/int/IntExample.java)<br>
15+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [**`float`**](./primitive-data-types/primitive-types/float/) [<sub>FloatExample.java</sub>](./primitive-data-types/primitive-types/float/FloatExample.java)<br>
16+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── [**`double`**](./primitive-data-types/primitive-types/double/) [<sub>DoubleExample.java</sub>](./primitive-data-types/primitive-types/double/DoubleExample.java)<br>
17+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│<br>
18+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── [*wrapper-classes/*](./primitive-data-types/wrapper-classes/)<br>
19+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [**`Boolean`**](./primitive-data-types/wrapper-classes/Boolean/) [<sub>BooleanWrapperExample.java</sub>](./primitive-data-types/wrapper-classes/Boolean/BooleanWrapperExample.java)<br>
20+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [**`Character`**](./primitive-data-types/wrapper-classes/Character/) [<sub>CharacterWrapperExample.java</sub>](./primitive-data-types/wrapper-classes/Character/CharacterWrapperExample.java)<br>
21+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [**`Integers`**](./primitive-data-types/wrapper-classes/Integers/) [<sub>IntegersWrappersExample.java</sub>](./primitive-data-types/wrapper-classes/Integers/IntegersWrappersExample.java)<br>
22+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── [**`Float`**](./primitive-data-types/wrapper-classes/Float/) [<sub>FloatWrapperExample.java</sub>](./primitive-data-types/wrapper-classes/Float/FloatWrapperExample.java)<br>
23+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── [**`Double`**](./primitive-data-types/wrapper-classes/Double/) [<sub>DoubleWrapperExample.java</sub>](./primitive-data-types/wrapper-classes/Double/DoubleWrapperExample.java)<br>
24+
│<br>
25+
├── non-primitive-data-types/<br>
26+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── basic/<br>
27+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── string.md<br>
28+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── array.md<br>
29+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;│<br>
30+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── collections/<br>
31+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── list.md<br>
32+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── set.md<br>
33+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── map.md<br>
34+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├── queue.md<br>
35+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── deque.md<br>
36+
│<br>
37+
└── CONTRIBUTING.md<br>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
public class IntExample {
2+
public static void main(String[] args) {
3+
// Print ranges of integer types (Byte, Short, Int, Long)
4+
System.out.println("Byte range: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
5+
System.out.println("Short range: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
6+
System.out.println("Int range: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
7+
System.out.println("Long range: " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
8+
System.out.println(); // Print an empty line for better readability
9+
10+
// Initialize integer variables for different data types
11+
byte byteValue = 10; // A byte value
12+
short shortValue = 32500; // A short value
13+
int intValue = 2147483640; // An int value
14+
long longValue = 1234567890123456789L; // A long value with 'L' at the end to indicate it's a long literal
15+
16+
// Demonstrate arithmetic operations
17+
demonstrateArithmeticOperations(intValue, byteValue, shortValue, longValue);
18+
19+
// Demonstrate relational operations
20+
demonstrateRelationalOperations(intValue, longValue);
21+
22+
// Demonstrate type casting
23+
demonstrateTypeCasting(byteValue, intValue);
24+
25+
// Demonstrate overflow and underflow in byte type
26+
demonstrateOverflowAndUnderflow();
27+
}
28+
29+
// Method to demonstrate arithmetic operations on integer types
30+
private static void demonstrateArithmeticOperations(int intValue, byte byteValue, short shortValue,
31+
long longValue) {
32+
System.out.println("Arithmetic Operations:");
33+
// Addition of intValue and byteValue
34+
System.out.println("Addition: " + (intValue + byteValue));
35+
36+
// Subtraction of shortValue and byteValue
37+
System.out.println("Subtraction: " + (shortValue - byteValue));
38+
39+
// Multiplication of intValue and shortValue
40+
System.out.println("Multiplication: " + (intValue * shortValue));
41+
42+
// Division of longValue by intValue
43+
System.out.println("Division: " + (longValue / intValue));
44+
45+
// Modulo operation (remainder of shortValue divided by byteValue)
46+
System.out.println("Modulo: " + (shortValue % byteValue));
47+
System.out.println(); // Empty line for separation
48+
}
49+
50+
// Method to demonstrate relational operations between int and long
51+
private static void demonstrateRelationalOperations(int intValue, long longValue) {
52+
System.out.println("Relational Operations:");
53+
// Check if intValue is equal to longValue
54+
System.out.println("Equal to: " + (intValue == longValue));
55+
56+
// Check if intValue is not equal to longValue
57+
System.out.println("Not equal to: " + (intValue != longValue));
58+
59+
// Check if intValue is greater than longValue
60+
System.out.println("Greater than: " + (intValue > longValue));
61+
62+
// Check if intValue is less than longValue
63+
System.out.println("Less than: " + (intValue < longValue));
64+
65+
// Check if intValue is greater than or equal to longValue
66+
System.out.println("Greater than or equal to: " + (intValue >= longValue));
67+
68+
// Check if intValue is less than or equal to longValue
69+
System.out.println("Less than or equal to: " + (intValue <= longValue));
70+
System.out.println(); // Empty line for separation
71+
}
72+
73+
// Method to demonstrate type casting between byte and int
74+
private static void demonstrateTypeCasting(byte byteValue, int intValue) {
75+
System.out.println("Type Casting:");
76+
// Implicit casting (byte to int) is done automatically by Java
77+
int implicitCast = byteValue;
78+
System.out.println("Implicit cast from byte to int: " + implicitCast);
79+
80+
// Explicit casting (int to byte) requires type casting because it may result in
81+
// data loss
82+
byte explicitCast = (byte) intValue;
83+
System.out.println("Explicit cast from int to byte: " + explicitCast);
84+
System.out.println(); // Empty line for separation
85+
}
86+
87+
// Method to demonstrate overflow and underflow behavior in byte type
88+
private static void demonstrateOverflowAndUnderflow() {
89+
System.out.println("Overflow and Underflow:");
90+
// Overflow: Byte.MAX_VALUE + 1 causes an overflow, wrapping around to
91+
// Byte.MIN_VALUE
92+
byte overflowExample = (byte) (Byte.MAX_VALUE + 1);
93+
System.out.println("Overflow in byte (Byte.MAX_VALUE + 1): " + overflowExample);
94+
95+
// Underflow: Byte.MIN_VALUE - 1 causes an underflow, wrapping around to
96+
// Byte.MAX_VALUE
97+
byte underflowExample = (byte) (Byte.MIN_VALUE - 1);
98+
System.out.println("Underflow in byte (Byte.MIN_VALUE - 1): " + underflowExample);
99+
System.out.println(); // Empty line for separation
100+
}
101+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Integer Types in Java
2+
3+
## Overview
4+
In Java, integer types are used to store whole numbers of varying sizes and ranges. These types are efficient for different needs, from small byte-sized values to large long-sized values.
5+
6+
### Integer Types in Java:
7+
1. **`byte`**: 8-bit signed integer
8+
2. **`short`**: 16-bit signed integer
9+
3. **`int`**: 32-bit signed integer (default type for integers)
10+
4. **`long`**: 64-bit signed integer
11+
12+
---
13+
14+
## Comparison Table
15+
16+
| **Type** | **Size (bits)** | **Range** | **Default Value** |
17+
|------------|-----------------|-----------------------------------------|-------------------|
18+
| `byte` | 8 | -128 to 127 | 0 |
19+
| `short` | 16 | -32,768 to 32,767 | 0 |
20+
| `int` | 32 | -2,147,483,648 to 2,147,483,647 | 0 |
21+
| `long` | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
22+
23+
---
24+
25+
## Operations on Integer Types
26+
27+
### **Arithmetic Operations**
28+
| **Operation** | **Symbol** | **Example** |
29+
|-----------------|------------|-------------------------|
30+
| Addition | `+` | `intValue + byteValue` |
31+
| Subtraction | `-` | `shortValue - byteValue` |
32+
| Multiplication | `*` | `intValue * shortValue` |
33+
| Division | `/` | `longValue / intValue` |
34+
| Modulo | `%` | `shortValue % byteValue` |
35+
36+
### **Relational Operations**
37+
| **Operation** | **Symbol** | **Example** |
38+
|----------------------|------------|---------------------------|
39+
| Equal to | `==` | `intValue == longValue` |
40+
| Not equal to | `!=` | `intValue != longValue` |
41+
| Greater than | `>` | `intValue > longValue` |
42+
| Less than | `<` | `intValue < longValue` |
43+
| Greater than or equal to | `>=` | `intValue >= longValue` |
44+
| Less than or equal to | `<=` | `intValue <= longValue` |
45+
46+
---
47+
48+
## Type Casting
49+
50+
### **Implicit Casting** (Widening)
51+
- Automatically converts smaller types to larger types.
52+
- Example:
53+
```java
54+
byte byteValue = 10;
55+
int intValue = byteValue; // No explicit cast needed
56+
```
57+
### **Explicit Casting** (Narrowing)
58+
- Converts larger types to smaller types explicitly.
59+
- Example:
60+
```java
61+
int intValue = 1000;
62+
byte byteValue = (byte) intValue; // May lead to data loss
63+
```
64+
65+
## Overflow and Underflow
66+
When calculations exceed the range of the data type:
67+
68+
- **Overflow**: Value wraps around to the minimum value.
69+
- **Underflow**: Value wraps around to the maximum value.
70+
71+
- Example:
72+
```java
73+
byte overflowExample = (byte) (Byte.MAX_VALUE + 1);
74+
System.out.println("Overflow: " + overflowExample); // Output: -128
75+
76+
byte underflowExample = (byte) (Byte.MIN_VALUE - 1);
77+
System.out.println("Underflow: " + underflowExample); // Output: 127
78+
```
79+
80+
81+
## Example Code
82+
```java
83+
public class IntExamples {
84+
public static void main(String[] args) {
85+
// Initialize variables
86+
int a = 100;
87+
long b = 100000L;
88+
89+
// Arithmetic Operations
90+
long sum = a + b;
91+
System.out.println("Sum: " + sum);
92+
93+
// Relational Operations
94+
boolean isEqual = (a == b);
95+
System.out.println("Is Equal: " + isEqual);
96+
97+
// Type Casting
98+
// Implicit casting (int to long)
99+
long implicitCast = a;
100+
System.out.println("Implicit cast from int to long: " + implicitCast);
101+
102+
// Explicit casting (long to int)
103+
int explicitCast = (int) b;
104+
System.out.println("Explicit cast from long to int: " + explicitCast);
105+
106+
// Overflow Example
107+
byte maxByte = Byte.MAX_VALUE; // 127
108+
byte overflow = (byte) (maxByte + 1); // Overflow to -128
109+
System.out.println("Overflow Example (127 + 1): " + overflow);
110+
}
111+
}
112+
```
113+
114+
> **For more:** [`IntExamples.java`](./IntExamples.java)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Boolean Type in Java
2+
3+
## Overview
4+
In Java, the **boolean** type is used to represent one of two values: **true** or **false**. It is commonly used in decision-making statements and control flow, such as `if`, `while`, and `for` loops.
5+
6+
---
7+
8+
## Characteristics of Boolean Type
9+
10+
| **Type** | **Size** | **Values** | **Default Value** |
11+
|------------|----------|---------------------|-------------------|
12+
| `boolean` | 1-bit | `true` or `false` | `false` |
13+
14+
---
15+
16+
## Boolean Operations
17+
18+
```java
19+
boolean value1 = true; // primitive boolean
20+
boolean value2 = false; // primitive boolean
21+
```
22+
23+
### **Logical Operators**
24+
| **Operation** | **Symbol** | **Description** | **Example** |
25+
|-----------------|------------|---------------------------------------------|--------------------------------|
26+
| AND | `&&` | Returns `true` if both operands are `true`. | `value1 && value2` |
27+
| OR | `\|\|` | Returns `true` if at least one operand is `true`. | `value1 \|\| value2` |
28+
| NOT | `!` | Reverses the value of the operand. | `!value1` |
29+
30+
### **Relational Operators**
31+
| **Operation** | **Symbol** | **Description** | **Example** |
32+
|----------------------|------------|-------------------------------|---------------------|
33+
| Equal to | `==` | Returns `true` if both values are equal. | `value1 == value2` |
34+
| Not equal to | `!=` | Returns `true` if values are not equal. | `value1 != value2` |
35+
36+
---
37+
38+
## Example Code
39+
```java
40+
public class BooleanExamples {
41+
public static void main(String[] args) {
42+
// Initialize boolean variables
43+
boolean isJavaFun = true;
44+
boolean isFishTasty = false;
45+
46+
// Print boolean values
47+
System.out.println("Is Java fun? " + isJavaFun);
48+
System.out.println("Is fish tasty? " + isFishTasty);
49+
50+
// Logical AND
51+
boolean resultAnd = isJavaFun && isFishTasty;
52+
System.out.println("Logical AND: " + resultAnd);
53+
54+
// Logical OR
55+
boolean resultOr = isJavaFun || isFishTasty;
56+
System.out.println("Logical OR: " + resultOr);
57+
58+
// Logical NOT
59+
System.out.println("Logical NOT (isJavaFun): " + !isJavaFun);
60+
61+
// Relational Operations
62+
System.out.println("Is isJavaFun equal to isFishTasty? " + (isJavaFun == isFishTasty));
63+
System.out.println("Is isJavaFun not equal to isFishTasty? " + (isJavaFun != isFishTasty));
64+
}
65+
}
66+
```
67+
68+
> **For more:** [`BooleanExample.java`](./BooleanExample.java)

0 commit comments

Comments
 (0)