[Canonical page](<https://docs.dhtmlx.com/planning-engine/java/api/data/id/>)
[Full documentation index](<https://docs.dhtmlx.com/planning-engine/llms.txt>)


# Id

The id of a task, link, resource, assignment or calendar. An `Id` holds either a number or a string. Create one with [`Id.num`](<https://docs.dhtmlx.com/planning-engine/java/api/data/id.md#num>) or [`Id.str`](<https://docs.dhtmlx.com/planning-engine/java/api/data/id.md#str>).

Two ids are equal when their [`key()`](<https://docs.dhtmlx.com/planning-engine/java/api/data/id.md#key>) values are equal, so `Id.num(1)` and `Id.str("1")` are the same id.

## Declaration

```java
public final class Id
```

`Id` is immutable and has no public constructor.

## Methods

### isNum

```java
public boolean isNum()
```

Returns `true` for a numeric id.

### isStr

```java
public boolean isStr()
```

Returns `true` for a string id.

### numValue

```java
public Double numValue()
```

Returns the number of a numeric id, or `null` for a string id.

### strValue

```java
public String strValue()
```

Returns the text of a string id, or `null` for a numeric id.

### key

```java
public String key()
```

Returns the id as text. A string id is returned as is. A number is printed the way JavaScript's `String(n)` prints it:

| Number | Key |
|---|---|
| `1` | `"1"` |
| `1.5` | `"1.5"` |
| `0` and `-0` | `"0"` |
| `1e21` | `"1e+21"` |
| `1e-7` | `"1e-7"` |
| `Double.NaN` | `"NaN"` |

`equals`, `hashCode` and `toString` all use this key.

**Errors:** throws `IllegalStateException` when the id is an infinite number.

### norm

```java
public String norm()
```

Same as [`key()`](<https://docs.dhtmlx.com/planning-engine/java/api/data/id.md#key>).

### equals

```java
public boolean equals(Object other)
```

Returns `true` when `other` is an `Id` with the same key: `Id.num(1).equals(Id.str("1"))` is `true`. String keys compare case-sensitively. Every NaN id equals every other NaN id. `equals(null)` and a non-`Id` argument return `false`.

A number beyond `double` precision is stored rounded, so `Id.num(9007199254740993L)` equals `Id.str("9007199254740992")`.

**Errors:** throws `IllegalStateException` when the key of either id is needed and that id is an infinite number.

### hashCode

```java
public int hashCode()
```

Hash of the key, so equal ids hash the same and `Id` works as a `HashMap` or `HashSet` key.

**Errors:** throws `IllegalStateException` for an infinite number.

### toString

```java
public String toString()
```

Returns the key.

## Static methods

### num

```java
public static Id num(double n)
```

Creates a numeric id.

### str

```java
public static Id str(String s)
```

Creates a string id. The empty string is a valid id.

**Errors:** throws `NullPointerException` when `s` is `null`.

## Validation

An id in project data must be a finite number or a string. A NaN or infinite numeric id throws [`InputException`](<https://docs.dhtmlx.com/planning-engine/java/api/errors/input-exception.md>) with path `"id"` when the data is loaded.

## Example

```java
import org.dhtmlx.plen.*;
import java.util.HashMap;

public class IdExample {
  public static void main(String[] args) throws Exception {
    Id a = Id.num(1);
    Id b = Id.str("1");
    Id c = Id.num(1.5);

    System.out.println(a.equals(b));    // true
    System.out.println(a.isNum());      // true
    System.out.println(b.strValue());   // 1
    System.out.println(c.key());        // 1.5

    var names = new HashMap<Id, String>();
    names.put(a, "Design");
    System.out.println(names.get(Id.str("1")));  // Design
  }
}
```

## Related

- [`TaskData`](<https://docs.dhtmlx.com/planning-engine/java/api/data/task-data.md>) - task ids and parents
- [`SchedulingConfig.rootId`](<https://docs.dhtmlx.com/planning-engine/java/api/data/scheduling-config.md#rootid>) - the id that means no parent
- [Ids](<https://docs.dhtmlx.com/planning-engine/java/records-ids-and-dates.md#ids>) - creating ids and key semantics
- [Ids and dates](<https://docs.dhtmlx.com/planning-engine/java/scheduling/project-data.md#ids-and-dates>) - ids in project data
