Java packages and compiling them
Neat explanation here. Or check the (Local copy)
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)
{
...
}
fun(1);
fun(1,2,3);
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(bob.score);
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
}
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
System.out.println(age);
System.out.println(freq);
System.out.println(sh);
System.out.println(by);
System.out.println(bp_f);
System.out.println(bp);
System.out.println(pow);
System.out.println(red);
//Arrays (with primitive types)
int[] primeNumbers = {2,3,5,7,11,13};
System.out.println(primeNumbers); //prints address
System.out.println(primeNumbers[0]);
System.out.println(primeNumbers.length); //Note, length is not a method but an attribute
}
}
Non primitive data types
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
//Non primitive datatypes start with captial letter
//String
//should always be in Double quotes and not single!
String s = "abc";
// They are classes which have convenience methods
System.out.println(s.length());
// all primitive datatypes can by concatenated with a string without having to cast
int age = 26;
String str = "Hi " + age;
System.out.println(str);
//Lists : Can be used as arrays and has many convinence methods
List list = new ArrayList();
list.add(2);
list.add(3);
list.add(4.0); //can mix types
list.add('c');
System.out.println(list);
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
System.out.println(list1);
List<Integer> intlist = new ArrayList<Integer>(Collections.nCopies(10, 5)); //Templating with non primitive types
//intlist.add(5.0); //breaks
System.out.println(intlist);
//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.add(i);
castList.remove(2); //remove element
System.out.println(castList);
//casting to string
castList.toString();
//Map
// has key and value. like python dict
Map map = new HashMap();
map.put("Father","Rob");
map.put("Mother","Loy");
System.out.println(map);
System.out.println(map.get("Father"));
System.out.println(map.size());
}
}
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("18");
else
System.out.println("Not allowed");
System.out.println(check);
//while loop
int x = 1;
while ( x <= 10){
//print even numbers
if(x%2 == 0)
System.out.println(x);
x++;
}
//for loop
//cant use x as counter variable because it was already defined in global scope
for (int y = 1; y<=10; y++)
System.out.println(y*100);
//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;
System.out.println(x);
}
//lopping through array
String[] family = {"aravind","usha","sankaran"};
// String is the type of object in the list
for(String name : family)
System.out.println(name);
List familyList = new ArrayList();
familyList.add("a");
familyList.add("b");
familyList.add("c");
//cant use String, as we have not templated List. IF we had templated to String, then we can use String
for(Object name : familyList)
System.out.println(name);
}
}
Mainly used for web scrapping from html codes
String river = "mississippi";
//SPLIT
System.out.println(river.split("s"));
//[mi,'',i,'',ippi]
//SUBSTRING
System.out.println(river.substring(2,5));
//ssi
//REGEX
Pattern p = Pattern.compile("Mi(.*?)i"); //finds until first i
Matcher m = p.matcher(river);
while(m.find()){
System.out.println(m.group(1));
}
//ss
Pattern p = Pattern.compile("Mi(.*)i"); //finds until last i
//ssissipp