Category Archives: Core Java

How to find your Google Plus ID

This is so simple

1. Go to your Google + account (https://plus.google.com/).

2. Click on the Profile icon on the Left.

3. If you look at the URL in the address bar, it should look something like this:

https://plus.google.com/104653270154306099169/posts

4. The long numerical string in the URL is your Google+ ID. Here is CoderzHeaven’s from the URL above:

104653270154306099169/

Google + CoderzHeaven

Java check memory Allocation

class test
{
	public static void main(String args[])
	{
			Runtime r = Runtime.getRuntime();
			long mem1, mem2;
			Integer someints[] = new Integer[1000];
			System.out.println("Total memory is: " +r.totalMemory());
			mem1 = r.freeMemory();
			System.out.println("Initial free memory: " + mem1);
			r.gc();
			mem1 = r.freeMemory();
			System.out.println("Free memory after garbage collection: "+ mem1);

			for(int i=0; i<1000; i++)
				someints[i] = new Integer(i); // allocate integers
			mem2 = r.freeMemory();

			System.out.println("Free memory after allocation: "	+ mem2);
			System.out.println("Memory used by allocation: "+ (mem1-mem2));

			// 	discard Integers
			for(int i=0; i<1000; i++) someints[i] = null;
				r.gc(); // request garbage collection

			mem2 = r.freeMemory();
			System.out.println("Free memory after collecting" +" discarded Integers: " + mem2);
	}
}

When run the code the output will be similar to this

Total memory is: 5177344
Initial free memory: 4986744
Free memory after garbage collection: 5063784
Free memory after allocation: 5045296
Memory used by allocation: 18488
Free memory after collecting discarded Integers: 5063784

Creating Exceptions in JAVA

This is a simple custom exception in java. That is, we can create an exception by extending the exception class.

The “throw new MyExcep” will throw an exception and the error message we specified will be displayed

import java.lang.Exception;
@SuppressWarnings("serial")
class MyExcep extends Exception
{
	MyExcep(String Errormsg)
	{
		super(Errormsg);	// call Exception class error message
	}
}
public class Example
{
	public static void main(String[] args)
	{
		int x = 5 , y = 1000;
		try
		{
			float z = (float) x / (float) y;
			if(z < .01)
			{
				throw new MyExcep("Number is too small");
			}
		}
		catch(MyExcep e)
		{
			System.out.println("Caught My Exception");
			System.out.println(e.getMessage());
		}
		finally
		{
			System.out.println("I am Always Here");
		}
	}
}

The output will be like this

Caught My Exception
Number is too small
I am Always Here

Abstract Class in java

This example shows how a simple java abstract class works
Here “Shape” is the abstract class
abstract class Shape
{
abstract void initial(); // methods only defined…..
abstract void display();
}
class cube extends Shape
{
void initial() // Abstract Methods defined….
{
System.out.println(“Hello !! “);
}
void display()
{
System.out.println(“How are you?”);
}
}
public class Abstract
{
public static void main(String[] args)
{
// Shape S = new Shape(); // error cannot create objects for abstrac classes…..
cube C = new cube();
C.initial();
C.display();
}
}
Here we cannot create “Shape” class object because it is abstract
The output is
Hello !!
How are you?

Java Exception

Simple java program to show how exception works

public class exception
{
public static void main(String[] args)
{
try
{
int a = 10;
int b = 10/0;
}
catch(ArithmeticException e)
{
System.out.println(“Exception Caught ” + e);
}
}
}
The output will be like you expect
Exception Caught java.lang.ArithmeticException: / by zero