Java Tutorial: Introduction for beginners (Basic)

Java Tutorial

Java is a general-purpose, open-source and free programming language. It originally developed by Sun Microsystems in 1995 and more than 3 billion devices run Java. It is used to creating Desktop applications, Mobile applications, Web applications, Web servers, Games, and more. Java works on a variety of platforms. such as Windows, Mac, Linux and more. Java is easy to learn and simple to use. In this tutorial. You will learn some basic Java programming concepts.

Java “Hello World!”

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}

Java Tutorial – Installation

Download latest Java platform and install it. then setup the path for windows.

Setting Up the Path : https://www.java.com/en/download/help/path.xml

Java Tutorial – Start

Create Java file, called HelloWorld.java, you can use any java editor software for this (Notepad, Eclipse, Netbeans, Visual Studio Code). then enter the following code and save it.

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

Open Command Prompt and navigate to the directory where you saved “HelloWorld.java” then enter the
following command and press enter to compile your code

javac HelloWorld.java

If there are no errors, enter following command and press enter to run your code

java HelloWorld

Output

Java Tutorial

Java Tutorial – The main Method

All the code inside the “main()” method will be executed.

public static void main(String []args) {
  //code   
}

Java Tutorial – Comments 

public class ProgramCmnt{
  /* This is a single-line comment */

   public static void main(String []args) {
      // This is a single-line comment
      
      /* This is a
       * multi-line 
       * comment
       */
   }
}

Java Tutorial – Variables

Data Types

Strings– Stores text (“Hello World”, “Text”)
int – Stores Integers (1234, -242)
char – Stores single character (‘a’, ‘c’)
float – Stores floating point numbers (11.25, -54.21)
boolean – Stores boolean values (true or false)

data_type variable_name = variable_value;
public class ProgramVar{
   public static void main(String []args) {
      String website = "ctechf";
      int no = 100;
      int a = 10, b = 20, c = 30;

      System.out.println(website); //output: ctechf
      System.out.println(no); //output: 100
      System.out.println(a+b+c); //output: 60
   }
}

Data Types – Numbers

byte– Stores numbers (-128 to 127)
short– Stores numbers (-32,768 to 32,768)
int– Stores numbers (-2,147,483,648 to 2,147,483,647)
long– Stores numbers (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

float – Stores floating-point numbers (storing 6 to 7 decimal digits)
double– Stores floating-point numbers (storing 15 decimal digits)

Data Types – Text

Strings– Stores text
char – Stores single character

Data Types – Boolean

boolean – Stores boolean values (true or false)

Java Tutorial – Variables (Type Casting)

Change variable type (automatically)

public class ProgramVarc1{
   public static void main(String []args) {
      int noInt = 10;
      double noDouble = noInt;

      System.out.println(noInt);      // Outputs 10
      System.out.println(noDouble);   // Outputs 10.0
   }
}

Change variable type (manually)

public class ProgramVarc2{
   public static void main(String []args) {
      int noInt = 10;
      double noDouble = (double) noInt;

      System.out.println(noInt);      // Outputs 10
      System.out.println(noDouble);   // Outputs 10.0
   }
}

Change variable type (int to String)

public class ProgramVarc3{
   public static void main(String []args) {
      int noInt = -147;
      String noString = String.valueOf(noInt);
      
      System.out.println(noString);      // Outputs "-147"
   }
}

Change variable type (String to int)

public class ProgramVarc4{
   public static void main(String []args) {
      String noString = "478";
      int noInt = Integer.valueOf(noString);
      
      System.out.println(noInt);      // Outputs 478
   }
}

Java Tutorial – Arrays

Array_type[] Array_name = {"Value1", "Value2", "Value3" ...};
public class ProgramArray{
   public static void main(String []args) {
      String[] names = {"George", "Michel", "Elija", "Demon"};
   }
}

Array Length

public class ProgramArray1{
   public static void main(String []args) {
      String[] names = {"George", "Michel", "Elija", "Demon"};
      System.out.println(names.length); //Output: 4
   }
}

Access an Array

public class ProgramArray2{
   public static void main(String []args) {
      String[] names = {"George", "Michel", "Elija", "Demon"};

      System.out.println(names[1]); //Output: "Michel"
      System.out.println(names[3]); //Output: "Demon"
   }
}

For-Each

public class ProgramArray3{
   public static void main(String []args) {
      String[] names = {"George", "Michel", "Elija", "Demon"};

      for (String name : names) {
         System.out.println(name);
      }
   }
}

/* Output:
*  George
*  Michel
*  Elija
*  Demon
*/

Update an Array

public class ProgramArray4{
   public static void main(String []args) {
      String[] names = {"George", "Michel", "Elija", "Demon"};

      System.out.println("names[0] = " + names[0]); //Output: "George"
      names[0] = "Updated";
      System.out.println("names[0] = " + names[0]); //Output: "Updated"
   }
}

Java Tutorial – Multidimensional Arrays

Array_type[][] Array_name = { {Values of first Array}, {Values of second Array} };
public class ProgramMultiArray{
   public static void main(String []args) {
      int[][] numbers = { {1, 2, 3, 4}, {5, 6, 7, 8} };

      System.out.println(numbers[1][2]); //Output: "7"
      System.out.println(numbers[0][0]); //Output: "1"
   }
}
Java Tutorial

Java Tutorial – Operators

Arithmetic operators

Logical operators

Comparison operators

Java Tutorial – Conditions

if statement

if (condition) {
  // code
}
public class IfCon{
  public static void main(String[] args) {
    if (180 > 120) {
       System.out.println("180 is greater than 120");
    }
  }
}

if .. else statement

if (condition) {
  // code
} else { 
  // code
} 

or

String variable_name = (condition) ? "value" : "value";
public class IfElseCon{
  public static void main(String[] args) {
    int result = 78;
    if (result > 75) {
      System.out.println("Pass");
    } else {
      System.out.println("Fail");
    }
  }
}
// Outputs "Pass"

Simple one

public class IfSimpleCon{
  public static void main(String[] args) {
    int result = 78;
    String output = (result > 75) ? "Pass" : "Fail";
    System.out.println(output);
  }
}

if .. else if .. else statement

if (condition) {
  // code
} else if (condition2) {
  // code
} else { 
  // code
} 
public class IfElseIfCon{
  public static void main(String[] args) {
    int mark = 68;
    String result;
    if (mark > 75) {
      result = "A";
    } else if (mark > 65) {
      result = "B";
    } else if (mark > 55) {
      result = "C";
    } else if (mark > 35) {
      result = "S";
    } else {
      result = "F";
    }
    System.out.println(result); // Outputs "B"
  }
}

Java Tutorial – Switch Statements

switch(expression) {
  case 1:
    // code
    break;
  case 2:
    // code
    break;
  case 3:
    // code
    break;
  case 4:
    // code
    break;

  default:
    // code
}
public class SwitchSta{
  public static void main(String[] args) {
    int month = 6;
    switch (month) {
      case 1:
        System.out.println("January");
        break;
      case 2:
        System.out.println("February");
        break;
      case 3:
        System.out.println("March");
        break;
      case 4:
        System.out.println("April");
        break;
      case 5:
        System.out.println("May");
        break;
      case 6:
        System.out.println("June");
        break;
      case 7:
        System.out.println("July");
        break;
      case 8:
        System.out.println("August");
        break;
      case 9:
        System.out.println("September");
        break;
      case 10:
        System.out.println("October");
        break;
      case 11:
        System.out.println("November");
        break;
      case 12:
        System.out.println("December");
        break;
      default:
        System.out.println("Invalid month");
    }
  }
}
// Outputs "June"

Java Tutorial – Loop

While Loop

while (condition) {
  // code
}
public class WhileLoop{
  public static void main(String[] args) {
    int i = 0;
    while (i < 10) {
      System.out.println("i = " + i);
      i++;
    }
  }
}

Do/While Loop

do {
  // code
} while (condition);
public class DowhileLoop{
  public static void main(String[] args) {
    int i = 0;
    do {
      System.out.println("i = " + i);
      i++;
    } while (i < 10);
  }
}

for Loop

for (initialization; condition; Increment/Decrement) {
  // code
}
public class ForLoop{
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
      System.out.println("i = " + i);
    }
  }
}

infinitive Loop

public class InfinitiveWhileLoop{
  public static void main(String[] args) {
    while(true){  
      System.out.println("a");
    }
  }
}
public class InfinitiveDoloop{
  public static void main(String[] args) {
    do{  
      System.out.println("a");
    } while(true);
  }
}
public class InfinitiveForloop{
  public static void main(String[] args) {
    for(;;){  
      System.out.println("a");
    }
  }
}

Press “CTRL + C” for the break infinitive  loop

Java Tutorial – String Methods

String – Length

public class StringLength{
  public static void main(String[] args) {
    String txt = "Java is a general-purpose, open-source and free programming language.";
    System.out.println("Text Length:  " + txt.length()); //Output: "Text Length:  69"
  }
}

String – UpperCase and LowerCase

public class StringUpLow{
  public static void main(String[] args) {
    String txt = "Java programming language";
    System.out.println(txt.toUpperCase());   // Outputs "JAVA PROGRAMMING LANGUAGE"
    System.out.println(txt.toLowerCase());   // Outputs "java programming language"
  }
}

String – Special Characters

\’ – Single quote (‘)
\” – Double quote (“)
\\ – Backslash (\)
\n – New line
\t – Tab

public class StringSpecial{
  public static void main(String[] args) {
    System.out.println("Hello, \"Nik\"!");   // Outputs " Hello "Nik!" "
    System.out.println("Hello, \'Nik\'!");   // Outputs " Hello 'Nik!' "
  }
}

Sources : 
https://pixabay.com/ (images) 

Hello World,

Hope this post (Java Tutorial: Introduction for beginners (Basic)) will help you to learn something, If you enjoy my work then please share my posts with your friends and anyone who might be interested in programming and don’t forget to subscribe my mailing list.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Best Free eBooks to Learn Python Programming

Free eBooks to Learn Python

Free eBooks to Learn Python, Here are 13 of the best Python eBooks for python lovers.
Download Now

What is Python?

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux.

Python Tutorial: Introduction for Beginners (Part 1)
Python Tutorial: Introduction for Beginners (Part 2)
Python Tutorial: How to Connect MySQL Database in Python
Python Tutorial: Python GUI Programming (Tkinter)
Python Tutorial: Send Emails Using Python
Python Tutorial: Django Tutorial for Beginners

How to be a Programmer

Leave a Reply

69 Shares
Share
Tweet
Share
Pin
Share