# 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](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MVgqaZm16GDE1yfEZgI%2F-MVgrRVDtTZ7sG4PIJeV%2Fimage.png?alt=media\&token=367fee1b-e758-409b-a802-2525dc9ee459)

#### 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

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdMdqtD6oSf-NKbbEGE%2F-MdMe0YSKvqW5MXguw-b%2Fimage.png?alt=media\&token=963caf8e-3331-4861-92e3-9081caff9567)

&#x20;&#x20;

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdMeHkoobXGNDyTNt_s%2F-MdMjOkoJj2dLhWCQR_J%2FScreen%20Shot%202021-06-29%20at%208.26.32%20AM.png?alt=media\&token=7aae09c8-c8cf-49e5-b6fe-dd3efa7060f3)

&#x20;

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdMjQR1Ac-WMgFmyh-R%2F-MdMjwJqK6SSoe2dFERJ%2FScreen%20Shot%202021-06-29%20at%208.29.04%20AM.png?alt=media\&token=215b9026-cef5-46f0-80c0-143ef6a81d56)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdMjy3sn9FlirkItUAG%2F-MdMksd4JUY-QJKU4o7e%2Fimage.png?alt=media\&token=70cec22a-8556-4c75-8e63-3cd09acd2d47)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdMkuhsNrGBypipkD6B%2F-MdMlpeY1_rEio7rLylX%2Fimage.png?alt=media\&token=861e69d6-7557-4e30-86c6-e6e90ae5b17f)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdMlsQS1tRTGeioDGUB%2F-MdMmloytwK9hKSmwp2K%2Fimage.png?alt=media\&token=78875e3b-1b21-4a17-967a-be4916a1e6c5)

![To check all the methods of a class](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdMmo2ev65ZK1fx9krA%2F-MdMoACjyS7zK6LhYxdM%2Fimage.png?alt=media\&token=f1dd49a1-aa7c-4600-bc7c-7a3a5a53ebe8)

### Datatypes

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdPyh10mAV2oQJRVsaC%2F-MdPynjBIKKkiz1MgG18%2Fimage.png?alt=media\&token=0e6deac6-8a2a-48e3-95f2-953a1d372242)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdeP6y5Qh7GKawzFRK2%2F-MdgZKwqCXqWizWo25-e%2Fimage.png?alt=media\&token=df329b65-e6cc-4dcf-9e51-1c18388a0784)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdPyqPHhoe-umsxm-I6%2F-MdQ1E9pIL-aneayFZTm%2Fimage.png?alt=media\&token=3a43f56c-6bd3-4ba8-bfca-398e415f9708)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdQ1GH4B-kYhEu-ju9k%2F-MdQ1xuBHEeqifVPV6Pg%2Fimage.png?alt=media\&token=2c8a6060-a54f-43c4-acdc-aae8b8c718b4)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Mdg_8I-kQZ4ueINpjQ_%2F-Mdgb56JlRjGHJJ0sHQ8%2Fimage.png?alt=media\&token=ac81df42-d1c5-4e0b-bdf0-b22ad3202d22)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Mdgb7DgRZVEDfUpHZ5I%2F-MdgcDYX5Le3QcdFCym1%2Fimage.png?alt=media\&token=42d1603f-800c-46f4-948b-1cba72894edb)

{% 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 %}

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Mdge1hblDTpI_3z7NRV%2F-MdgeEqlA1WDbmWTXXPj%2Fimage.png?alt=media\&token=5dceab0f-c0f1-49c2-92f4-36d6b5310b1f)

### 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.

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Mdgesd1yI1lpO52nNkr%2F-Mdgf28-4NgVRFEI9Lyq%2Fimage.png?alt=media\&token=86f98611-dc75-4462-a226-0ff6007d4fe9)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Mdgesd1yI1lpO52nNkr%2F-MdgfQbzDBnnEWn1H8xU%2Fimage.png?alt=media\&token=c704c8a5-deff-4f72-9bb3-b0a55d042b5c)

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

### Java Architecture

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdSlxkh4NyHpJN3TiXR%2F-MdSm6dXNMEUvbruRbxY%2FScreen%20Shot%202021-06-30%20at%2012.36.11%20PM.png?alt=media\&token=5a73715a-f8ab-4a6a-a830-1374d850945a)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdSm8Q2JqqT3EES-Tzg%2F-MdSmZn4NmSAoYHoCOz8%2Fimage.png?alt=media\&token=7be03910-083f-468a-81db-f77856e8a8bc)

### Operators

#### JVM architecture

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Md_SA-mpHxjzt3aF5eb%2F-Md_SIIAFq4ZhAoZT42s%2Fimage.png?alt=media\&token=718adc2c-da21-4a3d-a192-e47d2002ba04)

![JVM architecture](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdV4sOXfo6kP94Dt-Ig%2F-MdV56QjUbHS6eXM_1e2%2Fimage.png?alt=media\&token=1f67010e-9da5-4fac-9e5a-d7bc58d59b77)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MddtPpvHW7rDHJYWBnc%2F-MdeKl47iaNekhGHgldn%2Fimage.png?alt=media\&token=97c5be56-7f94-4346-bbbc-9cb65bb9cf33)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdeKn-_a3cCmCUlV-w8%2F-MdeM1EQwFGw-C7Q5Gwa%2Fimage.png?alt=media\&token=b7698c76-17c8-4c1c-9587-95bce7ff1c47)

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

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdgfSqSSWHZ9jG0pqlK%2F-MdgynyJ98BjrXQ04gOB%2Fimage.png?alt=media\&token=9f9ac7ad-88bc-436b-90ca-e2d1063d36b9)

In normal way it can be done like this

![swapping](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Mdgz2zx-bcEq89n4ACD%2F-MdgzukTJmjJuAhbk7Yb%2Fimage.png?alt=media\&token=5af3e337-a4f9-40f4-9b33-9efd6273c747)

## Printing

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdgzyY5n4eIqeB_jcP1%2F-MdhKrWwdSfNc1XauN-5%2FScreen%20Shot%202021-07-03%20at%201.03.53%20PM.png?alt=media\&token=7f78ce8e-164d-4675-8562-6c0866c325e3)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MdhKstVxlgABuz23H8C%2F-MdhOEM5ulKyoQ8TwT0V%2Fimage.png?alt=media\&token=c8668490-e4e2-440e-a2f2-9a1d1c99c8c5)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me2j7b6DeI8f8LK19lB%2F-Me2jp6ylMZwZ1j9TqnZ%2Fimage.png?alt=media\&token=c7cfd2b6-1216-4ffb-a368-ab66d9060dd9)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me2jqMjDdHGQlAl-YzD%2F-Me2kYKNn9b6-2gvt3L3%2Fimage.png?alt=media\&token=8b672ce1-a01a-49de-be38-a0acaa25eb36)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me2kZids1Xlb1Yald9O%2F-Me2lcSzuF80_WF0kwV-%2FScreen%20Shot%202021-07-07%20at%209.36.42%20PM.png?alt=media\&token=c3e9f4c1-f6db-4d2b-947b-5317341ee038)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me2ldt5XQEUkclMvxrQ%2F-Me3JbaZ01EvMgPTai8a%2Fimage.png?alt=media\&token=8f3a90e2-2e76-4d6a-a866-2350fdde59dc)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me3Jd6LqEIlAGHDPB7L%2F-Me3KbSDhzTrZPdgbQJO%2Fimage.png?alt=media\&token=06ca4d08-0efe-490f-b30e-37ba7d2ac084)

## Strings

Methods of defining a string

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me3Kcl-PJv-D4gmyMYo%2F-Me3Msy6o1KseQNcQxrq%2Fimage.png?alt=media\&token=159c97d5-ff37-4d83-b35c-66f61e2cefc0)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me3Kcl-PJv-D4gmyMYo%2F-Me3N2V6DUwm3QyitRZb%2Fimage.png?alt=media\&token=f274b95a-08b7-4139-a9d2-f54ba9d02cda)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me3N4UPOfKqQIBySndz%2F-Me3NQumpyASxliiX7SM%2Fimage.png?alt=media\&token=f6ceaa41-6533-40d5-a460-ac77a55b1bb6)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me3NSbBNYahNFVbIFxr%2F-Me3NohIup0lfjZ10-Et%2Fimage.png?alt=media\&token=7a6d96ee-1068-445d-9054-036ce4c5cdee)

Creating a string from string literal

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me3NpkloQhkvfnTwr5w%2F-Me3Pm7L1Y0G8c727XD-%2Fimage.png?alt=media\&token=bf5f66cc-b7ac-4834-9206-2fddcd16bcfe)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me3NpkloQhkvfnTwr5w%2F-Me3QOqI71JRddW80rIG%2Fimage.png?alt=media\&token=bf81db73-db03-4fbe-8462-2bf9c2d50251)

![](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-Me3QQ78rCQjQCgz0yzl%2F-Me3RvtBv0pxAqVNzAN-%2Fimage.png?alt=media\&token=ab2306e5-cab3-4acb-a1be-2db51076b242)
