Java — A Quick Tour

Arash Arora
Geek Culture
Published in
5 min readJan 7, 2022
Photo by Fotis Fotopoulos on Unsplash

Java is a popular programming language, which is primarily used for

  • Web Applications
  • Mobile Applications
  • Desktop Applications
  • Games and much more..

Java was developed by Sun Microsystems in 1995. James Gosling is known as the father of Java. It was earlier known as Oak.

Why Java?

Java is platform independent thus works on various platforms be it Windows, Mac, Linux etc. It is secure, open source, and supports OOPS concepts.

Java is both compiled and interpreted

Bytecode — machine language intended for the JVM

JVM — Java Virtual Machine, which is a software based interpreter. It basically converts compiled binary bytecode to specific machine language regardless of hardware & software components & configurations of the machine. Thus Java is a platform independent language

Hello World in Java

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

In java, every piece of code that runs must be inside of a class. Class name should always start with a capital letter according pascal case.

public — it is an access specifier. Public means that it is globally available. The main method needs to be public so as to be executed by java runtime

static — it basically means that it does not require an instance to call the main function

void — return type, doesn’t return anything.

main — entry point of every java program.

String[] args — also known as java command line arguments, java main method accepts a single argument of type String array. We can change the name args.

System — it is a final class

out — it is an instance of printstream type

println — print the statements and then move the cursor to the next line

How to compile java programs

javac a.java

a.java → a.class (.class file would be made for every class present in the a.java)

Command used for the output

java a

Comments in Java

Comments — any text or notes written between the double slashes will be ignored by Java thus wouldn’t be executed

Single line comments with //

Multi line comments with /* ... */

Variables

They are defined as the containers which contains data

Variables can be of various datatypes that is it would hold data specific to a datatype say integer

eg. int a = 5; //a would only hold integer values

Datatypes

It is of two types

1. Primitives — those which are predefined in java

  • byte → takes 1 byte (-128 to 127), default = 0
  • short → takes 2 bytes ((-2¹⁶)/2 to 2¹⁶/2–1)), default = 0
  • int → takes 4 bytes ((-2³²)/2 to 2³²/2–1)), default = 0
  • long → takes 8 bytes, default = 0
  • float → takes 4 bytes, default = 0.0f
  • double → takes 8 bytes, default = 0.0d
  • char → takes 2 bytes, default = ‘\u0000’
  • boolean → takes 1 bit (true or false), default = false

2. Non Primitives — those which are user defined

  • String
  • Array
  • Class
  • Interface

Declaring a variable

class Main {
public static void main(String[] args) {
int a = 5;
float b = 3.23f;
char c = 'A';
boolean d = true;
double e = 93.232;
byte f = 12;
short g = 23;
long h = 27398l;
String name = "Arash Arora";
}
}

Type Casting — conversion of one primitive datatype to another

  • Widening Casting — which is done automatically (smaller type to larger type size)

byte →short → char → int → long → float → double

  • Narrowing Casting — done manually (large datatype to smaller)

double →float → long → int → char → short → byte

Widening Casting Example

int a = 9;double b = a;

Narrowing Casting Example

double a = 9.423;int b = (int) a;

Operators — which are used to perform operations on operands

x = 100 + 50;

100, 50 → operand

+ → operator

Types of operators

  • Arithmetic Operators
  1. +
  2. -
  3. *
  4. /
  5. %
  6. ++
  7. - -
  • Assignment Operators
  1. =
  2. +=
  3. -=
  4. *=
  5. /=
  • Comparison Operators
  1. ==
  2. !=
  3. >
  4. <
  • Logical Operators
  1. &&
  2. ||
  3. !
  • Bitwise Operators
  1. Left Shift → << → 10<<2 → 10*2² = 40
  2. Right Shift → >> → 10>>2 → 10/2² = 2
  3. >>> → for negative number, it changes the parity bit (MSB) to 0
  4. &, | → checks both the conditions no matter if first is false
  • Ternary Operators

a>b ? a : b; //it basically means if a>b then return a else return b

Keywords — those words which are reserved (predefined)

ex. boolean, int, break, byte etc.

Objects — instance of a class

Through objects we can access class variables and methods. It consists of a state (stored in fields)& behavior (methods) of a class.

Creating an object

class Student {
...
}
class Main {
public static void main(String[] args) {
Student s = new Student(); //object for class Student
}
}

References —

It points out to the object created, holds the location of the object in the memory. It is created alongside with the creation of object.

Read Inputs from users

We’ll use Scanner module of java.util package to take inputs from the users

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); /*System.in refers to readm
what we type from keyboard*/
int a = sc.nextInt(); //take input from user of type integer
float b = sc.nextFloat();
double c = sc.nextDouble();
// as nextDouble() won't read new line characters thus if use
// nextLine() to read a string, it would result into an empty
// string as it will read that new line character, so we would use // nextLine() first and then read the string
nextLine();
String s = nextLine();
boolean d = hasNextInt(); //returns true if the input is integer

Control Statements

if-else

if(condition) {
statements
} else if(condition2) {
statements
} else {
statements
}

switch-case

switch(expression) {
case value1 : ...
break;
case value2 : ...
break;
case value3 : ...
break;
default : ...
}

for loop

for (initialization;condition;increment/decrement) {
...
}

while loop

while(condition) {
...
}

do while loop — executes for atleast 1 time

do {
...
} while(condition);

for each — direct access to the elements of the array

for(type var: array) {
...
}

Array

It is a collection of homogeneous elements that has contiguous memory location. It is a data structure where we store same data of similar datatype.

It has a fixed size defined by the user.

We can store any primitive values or objects in an array.

Types of Arrays

  1. Single Dimensional
  2. Multidimensional

Declaration of array (single dimensional)

int[] arr; // declaration
arr = new int[5]; //instantiation
int[] arr = new int[5]; // declaration and instantiation
int a[] = {33,3,4,5} // intialization

Declaration of array (multi dimensional)

int[][] arr = new int[3][3]; //3 rows & 3 columns
arr[0][0]=1;
arr[0][1]=2;
...

Jagged Array — array of arrays with different number of columns

int values[][] = new int[3][];values[0] = new int[1];values[1] = new int[3];values[2] = new int[2];
for(int i=0;i<values.length;i++) { for(int j=0; j<values[i].length;j++) { values[i][j]=sc.nextInt(); }}

--

--

Arash Arora
Geek Culture

Hey! I'm Arash Arora, currently pursuing my Btech in CSE with cybersecurity as a specialization.