> For the complete documentation index, see [llms.txt](https://sandeepamaranath.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sandeepamaranath.gitbook.io/notes/languages-and-frameworks/java.md).

# Java

## Language Fundamentals

> **Content**
>
> 1. Identifiers
> 2. Reserved Keywords
> 3. Datatypes
> 4. Literals
> 5. Arrays
> 6. Types of variables
> 7. Var-arg methods
> 8. Main-methods
> 9. Command-Line arguments
> 10. Java coding standards

### 1. Identifiers

A name in Java program is known as an identifier which can be used for identification purpose. It can be a method name, class name, variable name, or label name.

```java
class Test { //1

 public static void main(String[] args){ // 2 3 4
 
    int x = 10; // 5
    
 }
  
}

// Identifiers

Test -> name of class
main -> name of method
String -> name of class
args -> name of array
x -> name of variable
```

#### Rules for defining Java identifiers

1. Allowed Java identifiers are

* A-Z
* a-z
* 0-9
* $
* \_

If we are using any other character we get a compile-time error.                                                                    Example: total\_value is **Valid,** total# is **Invalid**&#x20;

&#x20;2\. Identifiers can't start with a digit. Example: total123 is **valid**, 123total is **Invalid**

&#x20;3\. Java identifiers are case-sensitive. Java language itself is treated as a case-sensitive language. Example: int Number, int number, int Number ->  **All are valid**

&#x20;4\. The identifiers can be of any length. There is no limit. But it's not recommended to take too lengthy identifiers.

&#x20;5\. We can't use reserved words as identifiers. Example: int x = 10 is **valid**, int if = 10 is **invalid**

&#x20;6\. All predefined Java class names and interface names can be used as identifiers. int String = 10; int  Runnable = 20; **Both are valid.** Even though it is valid, it's not a good practice as it creates confusion.

{% hint style="info" %}
**Which of the following are valid Java identifiers?**

total\_value    -> valid

total#  -> **invalid**

123total  -> **invalid**

ca$h  -> valid

\_$ -> valid &#x20;

Java2Share -> valid

Integer -> valid

Int -> valid

int -> **invalid**
{% endhint %}

### 2. Reserved words

In Java, some words are reserved to represent some meaning or functionality. Such types of words are called reserved words.

![Reserved words categorization](/files/-MVgrRVDtTZ7sG4PIJeV)

#### Keywords for datatypes

`byte, short, int, long, float, double, boolean, char` -> **Total 8**

#### keywords for flow-control

`if, else, switch, case, default, while, do, for, break, continue, return` - **Total 11**

**Keywords for modifiers**

`public, private, protected, static, final, abstract, synchronized, native, strictfp, transient, volatile` - **Total 11** &#x20;

#### Keywords for exception-handling

`try, catch, finally, throw, throws, assert` - **Total 6**

#### Keywords for class-related

`class, extends, implements, interface, package, import` - **Total 6**

#### **keywords for Object-related**&#x20;

`new, instanceof, super, this` - **Total 4**

#### **Keyword for grouped constants**

`enum` - **Total 1**

We can use an enum to define grouped constants.&#x20;

```java
enum Month{
  JAN, FEB, MARCH,...
}
```

#### Keywords for return type

`void` - **Total 1**

{% hint style="info" %}
In Java, the `return` type is mandatory. If a method won't return anything then we need to declare with `void` return type.
{% endhint %}

#### &#x20;Banned keywords

1. **GOTO:** Usage of goto caused several problems so SUN banned this keyword.
2. **CONST:** There is `final` `keyword` that can be used instead of `const`&#x20;

{% hint style="danger" %}
We get a compile-time error using these keywords
{% endhint %}

#### Reserved Literals

1. `true, false` - for boolean datatypes
2. `null` - the default value for object reference

{% hint style="info" %}
Conclusions:

1. All 53 reserved words in Java contain only lower case alphabet symbols.&#x20;
2. In Java, we have the `new` keyword and not `delete` keyword because the destruction of useless objects is the responsibility of the Garbage collector&#x20;
   {% endhint %}

## Basics

![](/files/-MdMe0YSKvqW5MXguw-b)

&#x20;&#x20;

![](/files/-MdMjOkoJj2dLhWCQR_J)

&#x20;

![](/files/-MdMjwJqK6SSoe2dFERJ)

![](/files/-MdMksd4JUY-QJKU4o7e)

![](/files/-MdMlpeY1_rEio7rLylX)

![](/files/-MdMmloytwK9hKSmwp2K)

![To check all the methods of a class](/files/-MdMoACjyS7zK6LhYxdM)

### Datatypes

![](/files/-MdPynjBIKKkiz1MgG18)

![](/files/-MdgZKwqCXqWizWo25-e)

![](/files/-MdQ1E9pIL-aneayFZTm)

![](/files/-MdQ1xuBHEeqifVPV6Pg)

![](/files/-Mdgb56JlRjGHJJ0sHQ8)

![](/files/-MdgcDYX5Le3QcdFCym1)

{% hint style="info" %}
Negative numbers in java are represented by 2's complement form

2's complement means, first negate the bits and then add 1 to the number. Result is the 2s complement.&#x20;

Then to convert the number back to it's original, take 2s complement on the result again

See below example of converting -10
{% endhint %}

![](/files/-MdgeEqlA1WDbmWTXXPj)

### Performing right shift on negative numbers (Left shift and unsigned right is covered below)

If the number is a negative number then, after taking 2's complement and shifted the number to right by any number of positions, the signed bit is always untouched.

![](/files/-Mdgf28-4NgVRFEI9Lyq)

![](/files/-MdgfQbzDBnnEWn1H8xU)

The default decimal value in java is double and default integral is integer

### Java Architecture

![](/files/-MdSm6dXNMEUvbruRbxY)

![](/files/-MdSmZn4NmSAoYHoCOz8)

### Operators

#### JVM architecture

![](/files/-Md_SIIAFq4ZhAoZT42s)

![JVM architecture](/files/-MdV56QjUbHS6eXM_1e2)

![](/files/-MdeKl47iaNekhGHgldn)

![](/files/-MdeM1EQwFGw-C7Q5Gwa)

When we left shift a number 10 (1010 in binary) by 1, then we get 20. The number got multiplied by 2 on shifting 1 left. On shifting 2 positions left, it multiplies by 4 (2\*\*2). So to multiply by 2\* \*k then we shift by k to the left.

If we need to divide instead of multiplying we shift it by right. But we should not touch the first digit (signed bit or MSB) which represents the sign of the number. For unsigned right shift operator we can touch MSB and treat it as a normal bit and perform the right shift.&#x20;

So if we do right shift just like left shift then that is unsigned right shift.

### Swap two numbers without a temp or 3rrd variable

Can be done using XOR operation

![](/files/-MdgynyJ98BjrXQ04gOB)

In normal way it can be done like this

![swapping](/files/-MdgzukTJmjJuAhbk7Yb)

## Printing

![](/files/-MdhKrWwdSfNc1XauN-5)

![](/files/-MdhOEM5ulKyoQ8TwT0V)

![](/files/-Me2jp6ylMZwZ1j9TqnZ)

![](/files/-Me2kYKNn9b6-2gvt3L3)

![](/files/-Me2lcSzuF80_WF0kwV-)

![](/files/-Me3JbaZ01EvMgPTai8a)

![](/files/-Me3KbSDhzTrZPdgbQJO)

## Strings

Methods of defining a string

![](/files/-Me3Msy6o1KseQNcQxrq)

![](/files/-Me3N2V6DUwm3QyitRZb)

![](/files/-Me3NQumpyASxliiX7SM)

![](/files/-Me3NohIup0lfjZ10-Et)

Creating a string from string literal

![](/files/-Me3Pm7L1Y0G8c727XD-)

![](/files/-Me3QOqI71JRddW80rIG)

![](/files/-Me3RvtBv0pxAqVNzAN-)
