Java refresher

Java packages and compiling them

Neat explanation here. Or check the (Local copy)

A) Java classes

1) Access specifiers

1) Default access specifier of a java class is pacakge private. This is, this class can be instantiated from other classes contained in same package

2) Each .java file should contain atleast one public class

3) If the class is public, it can be instantiated by all packages just by importing import package.class

4) The attributes, methods and inner classes are by default package private. If they are private, they can be accessed only inside that class. private classes should not contain static variables

//Specify package
        package com.packagename

        public class A {

            B b = new B();//can access B in this package
            b.a; // a is accessible, only because it is in same package. package private in B
            b.D //D not accessible, since it is private in B

        class B { //default package private

            int a; //default package private

            class C{ //default package private

            private class D{ //private.

2) static and final keyword

1) static variables or methods in a class can accessd without having to instantiate that class

2) private classes cannot have static variables because the class itself it assumed to be a part of the object

3) final keyword is used for constant varibles whose values are not intended to be changed.

public class A{

               User.stat; //accessible because its static
               //User.score; //non static, so not accessible

               User rob = new User();
               rob.score; //accessible
               // rob.constant = 10; //cant modify         


        class User{
            static int stat = 101;
            int score = 10;
            final int constant = 1092; //constant variable

3) Anonymous class

The following syntax

Object abc = new Object() { ... }

creates an anonynmous class which extends the class object and creates an instance of that class

Variable length arguments

When you do not know the number of arguments that ll be passed to a function, use the following syntax

void fun(int... a)


B) Hello world java

Simple class

public class HelloWorld {
  // starting method
  public static void main(String[] args) {
    //prints statement
    System.out.println("Hello World");

Illustation with classes

public class HelloWorld {

  public static void main(String[] args) {

    User bob = new User();
    bob.score = 10;
    //bob.male = false; //breaks, since male is constant

    System.out.println(User.stat); // can access static variables without having to instantiate
    //System.out.println(User.male); //breaks, since male is not static

//default access is package private. ie it can be instantiated only from files in this package. Ie, in top of file we specify package com.aravind
// If private, then it can be instantiated only in this file
// if public, then this is availble to all packages. we just have to import. import package.*
class User { 

        //all vars and methods are package private by default
        int score; 
        final boolean male = true; //constant 
        final static int stat = 101; //we can access static variables without having to instantiate

C) Primitive data types and Arrays

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

  // ALL PRIMITIVE types : data type and its max values have been listed

    int age = 2147483647; //2^31-1 //4 bytes = 32 bits : 1 sign bit and 31 other bits 
    long freq = 9223372036854775807L;//2^63-1 //8 bytes = 64 bits // Note the L at the end of number
    short sh = 32767; // 2 bytes : 2^15-1
    byte by = 127; // 1 byte : 2^7 -1

    float bp_f = 3.40282347E38f; // IEEE 754 format : (2-2^-23)*2^(2**8 - 1) : 8 bits exponent, 23 bits fraction, up to 6 decimal digit precision
    double bp = 1.7976931348623157E308; // upto 14 decimal digit precision

    char c = 'a'; // 2 bytes
    boolean b = true; //or false : 1 byte

    int pow = (int) Math.pow(2,332); //2147483647 is the max number. anything greater than that is redundant
    float red = 23f*1E38f; // same redunduncy occurs when we multiply by large numbers. Outputs the same value for increasing factors




    //Arrays (with primitive types)

    int[] primeNumbers = {2,3,5,7,11,13};

    System.out.println(primeNumbers); //prints address
    System.out.println(primeNumbers.length); //Note, length is not a method but an attribute 

D) String, List, Map

Non primitive data types

import java.util.*;

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

    //Non primitive datatypes start with captial letter

    //should always be in Double quotes and not single!
    String s = "abc";
    // They are classes which have convenience methods

    // all primitive datatypes can by concatenated with a string without having to cast
    int age = 26;
    String str = "Hi " + age;

    //Lists : Can be used as arrays and has many convinence methods 

    List list = new ArrayList(); 
    list.add(4.0); //can mix types

    System.out.println(list.get(0)); //indexing not allowed : list[0]
    System.out.println(list.size()); //length of array

    List list1 = Arrays.asList(8,9,'c'); //creating without add

    List<Integer> intlist = new ArrayList<Integer>(Collections.nCopies(10, 5)); //Templating with non primitive types
    //intlist.add(5.0); //breaks

    //casting primitive arrays to List
    // No direct way without importing external libraries
    int a[] = {5,6,7};
    List<Integer> castList = new ArrayList<Integer>(a.length); //Fixes the length of array
    for(int i:a)
    castList.remove(2); //remove element

    //casting to string

    // has key and value. like python dict
    Map map = new HashMap();



E) if and loop

import java.util.*;

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

    boolean check = false;
    int age = 18;

    //if else
    if (age > 18)
        check = true;
    else if (age == 18)
        System.out.println("Not allowed");


    //while loop  
    int x = 1;
    while ( x <= 10){
        //print even numbers
        if(x%2 == 0)

    //for loop
    //cant use x as counter variable because it was already defined in global scope
    for (int y = 1; y<=10; y++)

    //but we can use y as it was a previous loop counter in local scope
    x = 0;
    //print first 10 triangular numbers
    for (int y = 1; y<=10; y++){
        x = x+y;

    //lopping through array
    String[] family = {"aravind","usha","sankaran"}; 

    // String is the type of object in the list
    for(String name : family)

    List familyList = new ArrayList();

    //cant use String, as we have not templated List. IF we had templated to String, then we can use String
    for(Object name : familyList)

F) String manipulations

Mainly used for web scrapping from html codes

String river = "mississippi";



Pattern p = Pattern.compile("Mi(.*?)i"); //finds until first i
Matcher m = p.matcher(river);

Pattern p = Pattern.compile("Mi(.*)i"); //finds until last i