Geek Culture
Published in

Geek Culture

Java — A Quick Tour

Photo by Fotis Fotopoulos on Unsplash
  • Web Applications
  • Mobile Applications
  • Desktop Applications
  • Games and much more..

Why Java?

Java is both compiled and interpreted

Hello World in Java

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

How to compile java programs

Command used for the output

Comments in Java

Variables

Datatypes

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)
  • Narrowing Casting — done manually (large datatype to smaller)

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

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

Keywords — those words which are reserved (predefined)

Objects — instance 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 —

Read Inputs from 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

  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(); }}

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arash Arora

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