How to calculate size of an Image

If ever you have a doubt on byte code of an image or size of an image, this is how it calculates of it.

bit and byte

Every image consists of blocks called pixels. As we all know data is always stored informs of bits. So even pixels will consume some bits of memory. For suppose we have only white and black 0 represents white and 1 represents black. So each of these pixels consume one bit of memory. For example: we have an image of size 100X200 pixels.

So total amount of memory in bits is 100*200*1=20,000 bits

A byte is equal to 8 bits.

So total amount of memory in bytes is 20,000/8=2500 bytes.


Now if you want to have shades of grey , definitely we must increase the bits of memory for a pixel. Say we have given 2 bits of memory for a pixel, we can store 4 shades of grey.

00 represents white

11 represent black

01 represents light grey

10 represent dark grey

For example: we have an image of size 100X200 pixels .

Total memory =(100*200*2)/8=5000 bytes


The Similar way if you allocate 3 bits per pixel, we can have 8 shades of color.

000 represent White

001 ,010,011,100,101,110 represent various shades of grey

111 represent black

Total memory =(100*200*3)/8=7500 bytes

From above 3 observations, we can easily derive a formula.

n bits can produce 2^n patterns (2 to the nth power)

Total no of colors= 2^bits  (2 to the power bits per pixel).

4 shades of colors=2*2 which is 2 bits per pixel

8 shades of  colors=2*2*2    which is 3 bits per pixel

16 shades of  colors=2*2*2*2 which is 4 bits per pixel

32 shades of  colors=2*2*2*2*2  which is 5 bits per pixel

64 shades of  colors=2*2*2*2*2*2  which is 6 bits per pixel

For a general image representation, we need 256 colors. So

256 shades of colors=2*2*2*2*2*2*2*2 which is 8 bits per pixel


256 colors






Encapsulation in Java with Example

Encapsulation is a basic principle of Java. Let me explain you encapsulation with a simple example.After reading the concept completely, I’m pretty sure you will never need to look back again to learn encapsulation concept.

Encapsulation ensures to hide the implementation details of a class to the other classes. Basic elements in a class are instance variables and member functions. Let us assume that we have written a class “Car”. The class “car” has some public variables like minSpeed, maxSpeed and noOfTyres instances variables some methods using them to calculate some logic.
Now say your friend has written some class called BenzCar and instantiated your Car class and have modified the instance variables of your Car class by mistake by initializing maxSpeed to 0 KMS and minSpeed to 100 KMS which are invalid by logic. So, now you are in serious trouble. Isn’t it?
So, to prevent this kind of blunders you must prevent your Car Class’s instance variables initialization restricted by making them private and adding new public methods which should be the only way to initialize the instance variables of your class.

Simple Example for understanding Encapsulation

Without Encapsulation example:

Class Car
Public int minSpeed;
Public int maxSpeed;
Public int noOfTyres;
Public int OptimimSpeed(maxSpeed)
Return (maxSpeed)/2;
package com.developprojects.withOutEncapsulation;

public class BenzCar {
	public static void main(String args[]) {
		Car car = new Car();
		car.minSpeed = 100;
		car.maxSpeed = 0;
		int optimumSpeed = car.optimimSpeed(car.maxSpeed);
				.println("OptimimSpeed of Benz Car with out encapsulation ::kms "
						+ optimumSpeed);

How to implement Encapsulation in Java:

Encapsulation is essential key feature of Object Oriented Programming. We should follow some simple rules to implement encapsulation in java. They are:

  1. Instance variable needs to safe guarded from other classes by making them private or protected access modifiers
  2. Make methods public and instance variables can only be accessed directly using these public methods. The naming conventions of these methods should be like set<VariableName> and get<VariableName>

With Encapsulation Example:

package com.developprojects.encapsulation;

public class Car {

	private int minSpeed;
	private int maxSpeed;
	private int noOfTyres;

	 * @return the minSpeed
	public int getMinSpeed() {
		return minSpeed;

	 * @param minSpeed
	 *            the minSpeed to set
	public void setMinSpeed(int minSpeed) {
		this.minSpeed = minSpeed;

	 * @return the maxSpeed
	public int getMaxSpeed() {
		return maxSpeed;

	 * @param maxSpeed
	 *            the maxSpeed to set
	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;

	 * @return the noOfTyres
	public int getNoOfTyres() {
		return noOfTyres;

	 * @param noOfTyres
	 *            the noOfTyres to set
	public void setNoOfTyres(int noOfTyres) {
		this.noOfTyres = noOfTyres;

	 * @return the OptimimSpeed of the vehicle
	public int optimimSpeed(int maxSpeed) {
		return (maxSpeed) / 2;

package com.developprojects.encapsulation;

public class BenzCar {

	public static void main(String args[]) {
		Car car = new Car();
		int optimumSpeed = car.optimimSpeed(car.getMaxSpeed());
		System.out.println("OptimimSpeed of Benz Car:: kms " + optimumSpeed);

From the above example, we have implemented encapsulation protecting the instance variables. Now even if we change the methods in actual class (Car class here). There is no change in the implementation class (BenzCar class).

Introduction: Basic understanding of XPath and its related concepts

Introduction to XPath:

Full form of XPath is XML Path. It is a query language designed to traverse through an xml document and select the required nodes using XPath Expressions and XPath functions, which I will discuss in the next chapters. XPath is a World Wide Web consortium (w3c) recommendation and the latest specification is Xpath 2.0.

This specification is designed to be referenced normatively from other specifications defining a host language for it. It is not intended to be implemented outside a host language. The implementation ability of this specification has been tested in the context of its normative inclusion in host languages defined by the XQuery and XSLT.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dev:Corporation xmlns:dev="">
		<dev:website category="technical">
			<dev:topic>XPath Tutorial</dev:topic>
			<dev:author>Viswa Tej Swarup Reddy</dev:author>


Regular XPath terminology:

The common XPath vocabularies you must know before proceeding further are:

  1. Nodes
  2. Atomic Values
  3. Items

Every XML document is a tree of nodes. Various types of nodes are

  1. Tag nodes
  2. Element nodes
  3. Attribute nodes
  4. Text nodes

In the above XML example,
< Corporation> is the root node.
<Organization> tag is a node.
<topic> XPath Tutorial</topic> is an element node.
category=”technical” is an attribute node.
XPath Tutorial is a text node.

And in turn these nodes can be categorized internally based on their relationships in between.

Some of them are namely

  1. Ancestor:  Ancestor nodes are parent nodes, if the parent nodes have parents, then we have to include their names too and it follows till the root node. In the above example the ancestor nodes of <author> node are <website>,<Organization>, <Corporation>.
  2.  Descendent: Descendent nodes are Child nodes, if the child nodes have children, then we have to include their names too and if follows till the lowest node is reached. In the above example the descendent nodes of <Organization> node are <website>, <name>, <topic>, <author>, <price>.
  3. Parent: Parent nodes are immediate parent of the selected node or attribute. In the above example <website> is the parent node for <name>, <topic>, <author>, <price>.
  4. Child:  Child nodes are the immediate child nodes of the selected nodes. In the above example, child nodes for <website> are <name>, <topic>, <author>, <price>.
  5. Siblings: The nodes which share a common parent are called siblings. In the above example, <name>, <topic>, <author>, <price> are siblings who share a common parent ,<website>.
  6. Text: Text nodes are just the values of the individual nodes.  In the above example,, XPath Tutorial, Viswa Tej Swarup Reddy, FREE are all text nodes.

It may sound redundant but we can also categorize node types based on the node’s functionality and purpose. They are:

  1. Document Node or Root Node
  2. Elements
  3. Attributes
  5. Namespace
  6. Text
  7. Processing Instruction


  1. Document Node or Root Node: The top most element of an XML document is document node. All the other elements remain within the document node.In the Above example <Corporation> is the document node.
  2. Elements:  Elements are contained within the document node. Anything in an XML document with Opening and closing tag is called an Element.
  3. Attributes: Attributes describe the element and usually lies in the opening tag. Example: category=”technical” describes that the website element belong to the technical category. In case if we have two elements with same name, we can differentiate them using attribute node.
  1. Comments: Comments are text defined in XML document for describing things to other users. They are contained in between the <! — and  à tags.
  2. Namespace:  Different systems use different tag names for their nodes. There is always a possibility of name conflict. So, a better option is to use prefix before every tag name. This prefix will have to be defined using xmlns attribute. The syntax would be like <element xmlns:prefix=”URI”>.
  3. Processing Instruction: Processing Instruction is the most import element of an XML document in real time scenarios. It instructs the renderer which may be a browser or another application which uses this xml document, the encoding format and whether it is standalone or not. In our example, we have used standard web encoding=”UTF-8″ tells us that the encoding is a 8 bit Unicode format and standalone=”yes” tell us that the xml document is self-contained . The standalone declaration means that the document is self-contained. That in turn means one of three things:
    1. There is no DOCTYPE declaration in it.
    2. The DOCTYPE declaration is inline only.
    3. The DOCTYPE declaration is external or combined, but the external part contains no data that changes the infoset      representation of the document.
  4. Text: Text nodes are text defined in Element or Attribute or Processing instruction.Examples of text nodes are XPath Tutorial, technical.

Properties of Nodes:
The following are the various types of Properties that a node can have:

  1. Name: Name Property can be applied to Elements, attributes, namespaces, processing instructions and document node. A Name may contain a combination local name and prefix. We can extract those values using the standard functions. Namespace-uri()  function for accessing the prefix and local-name() function for local name.  Example: dev is prefix and Corporation is local name.
  2. String values: It refers to the values of the nodes. String() function helps us for getting the value of the node.
  3. Base URI
  4. Attributes
  5. Namespace
  6. Parent
  7. Children
  8. Type Annotation

Atomic values: Atomic values are nodes with no parent or child nodes. They are indeed similar to text nodes., XPath Tutorial, Viswa Tej Swarup Reddy, FREE are all atomic values.

Items:Everything in XPath is indeed a sequence of items.  Items can be either nodes or atomic values. It is just similar to a node which may or may not contain parent or child nodes.