Skip to content

Latest commit

 

History

History
330 lines (228 loc) · 11 KB

File metadata and controls

330 lines (228 loc) · 11 KB

Java Essentials

Java JDK, JRE and JVM

  • JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java program.

  • JRE (Java Runtime Environment) is a software package that provides Java class libraries, Java Virtual Machine (JVM), and other components that are required to run Java applications.
  • JDK (Java Development Kit) is a software development kit required to develop applications in Java. When you download JDK, JRE is also downloaded with it.

What is JDK?

JDK (Java Development Kit) is a software development kit required to develop applications in Java. When you download JDK, JRE is also downloaded with it.

In addition to JRE, JDK also contains a number of development tools (compilers, JavaDoc, Java Debugger, etc).

JDK contains JRE and other tools to develop Java applications.

Java Development Kit

If you want to develop Java applications, download JDK.


Relationship between JVM, JRE, and JDK.

JRE contains JVM and class libraries and JDK contains JRE, compilers, debuggers, and JavaDoc

Relationship between JVM, JRE, and JDK

Java Operators

Operators in Java can be classified into 5 types:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Logical Operators
  5. Unary Operators
  6. Bitwise Operators

Arithmetic Operator

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)

Logical Operators

Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR

Java Unary Operators

Operator Meaning
+ Unary plus: not necessary to use since numbers are positive without using it
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- Decrement operator: decrements value by 1
! Logical complement operator: inverts the value of a boolean

Java Assignment Operators

Operator Example Equivalent to
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

Java Relational Operators

Operator Example Equivalent to
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

Java Logical Operators

Operator Example Meaning
&& (Logical AND) expression1 && expression2 true only if both expression1 and expression2 are true
|| (Logical OR) expression1 || expression2 true if either expression1 or expression2 is true
! (Logical NOT) !expression true if expression is false and vice versa

Java Bitwise Operators

Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR

Java Basic Input and Output

Java Output

System.out.println(); or
System.out.print(); or
System.out.printf();

Java Input

import java.util.Scanner;
// create an object of Scanner
Scanner input = new Scanner(System.in);
// take input from the user
int number = input.nextInt();

Java Flow Control

Java if...else Statement

Working of if Statement

if the number is greater than 0, code inside if block is executed, otherwise code inside if block is skipped

Working of Java if statement



How the if...else statement works?

If the condition is true, the code inside the if block is executed, otherwise, code inside the else block is executed

Working of Java if-else statements


How the if...else...if ladder works?

If the first test condition if true, code inside first if block is executed, if the second condition is true, block inside second if is executed, and if all conditions are false, the else block is executed

Working of if...else...if ladder


class Main {
  public static void main(String[] args) {

    int number = 0;

    // checks if number is greater than 0
    if (number > 0) {
      System.out.println("The number is positive.");
    }

    // checks if number is less than 0
    else if (number < 0) {
      System.out.println("The number is negative.");
    }
    
    // if both condition is false
    else {
      System.out.println("The number is 0.");
    }
  }
}



Output

The number is 0.

Java switch Statement

switch (expression) {

  case value1:
    // code
    break;
  
  case value2:
    // code
    break;
  
  ...
  ...
  
  default:
    // default statements
  }

Java for Loop

Working of for loop in Java with flowchart

Flowchart of Java for loop


for-each Loop Sytnax

// print array elements 
class Main {
  public static void main(String[] args) {
    // create an array
    int[] numbers = {3, 9, 5, -5};
    // for each loop 
    for (int number: numbers) {
      System.out.println(number);
    }
  }
}

/* Output
3
9
5
-5
*/

Java while loop

while (testExpression) {
    // body of loop
}

Flowchart of while loop in Java

Flowchart of Java while loop

Flowchart of do...while loop in Java

Flowchart of Java do while loop


do {
    // body of loop
} while(textExpression);

Break Statement

How break statement works?

How break statement works in Java programming?

Working of Java break Statement

if (number < 0.0) {
    break;
}

Java break and Nested Loop

The break statement terminates the innermost while loop in case of nested loops.

Working of break Statement with Nested Loops

Labeled break Statement


The labeled break statement is used to break the outermost loop.

Working of the labeled break statement in Java

Java continue Statement

Working of Java continue statement

The working of continue statement with Java while, do...while, and for loop.

Working of Java continue Statement

Java continue with Nested Loop.


Java continue with Nested Loop

Labeled continue Statement

Java Arrays

String[] array = new String[100];
// declare arrray 
dataType[] arrayName;
// declare an array
int[] age = new int[5];

// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
..

Elements are stored in the array

Java Arrays initialization

int[][] a = new int[3][4];

Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum of 12 elements,

2-dimensional array in Java

2-dimensional Array