Understanding Packages and classpath in Java

What is Package?

Package in Java is a way of organizing the code. In simple terms, its an directory or directory hierarchy which is used to store the code files. A statement like import java.util.Scanner means that on system java is a directory which in turns have sub directory name util, which have Scanner class. To create custom package the procedure is to create a directory and sub directories and declare it java file. Consider a class A, which belongs to a package org.demo, save it as A.java. Continue reading “Understanding Packages and classpath in Java”

Comparing user defined objects : Comparable and Comparator

Comparison plays important role in any programming language. Almost all the sorting techniques involves comparison. In Java, all the predefined classes and primitive data types supports comparison. User defined data types needs to provide the comparison logic, Comparable and Comparator comes in picture. For the primitive data type, how to compare is inbuilt and can be achieved using different comparison operators like <, >, <=, >=. Assume int a = 10 and int b = 20, then a<b is true. Continue reading “Comparing user defined objects : Comparable and Comparator”

Understanding Object class

Object class is a super class of all class in Java. All inheritance hierarchies starts from Object class followed by other class. Consider example class Shape as parent class of Rectangle and Circle class, in that case Object class is parent or super class of Shape class hence Rectangle and Circle class not only get methods and properties of Shape class but also Object class. This article shows the methods of Object class which is inherited by all the classes that are predefined or user defined. Continue reading “Understanding Object class”

Introduction to Java – What is Java?

Java is a general purpose programming language. The main feature that makes the Java language popular is “WORA” i.e. Write Once Run Anywhere. It means programs once written can run on any platform. Program written in Windows platform and compiled on Windows platform can run on Linux and Mac platform without recompiling the program or modifying the source code. This is achieved by generating the bytecode during the compiling process. Bytecode is an intermediate format which is than interpreted by JVM [ Java Virtual Machine ] at run time. As name says its a Virtual machine and not a physical machine its job is to translate bytecode in to executable code for the platform on which it is running. Following diagram shows the whole process. Continue reading “Introduction to Java – What is Java?”

Using equals and hashCode methods in Java

In Java, comparing two primitive data types can be achieved by using == operator, but to compare reference data type we need to provide the equals method.

By default every class in Java inherits from Object class which provides default implementation of equals method. It simply compares reference and return true and false accordingly. Lets see it through example.

Continue reading “Using equals and hashCode methods in Java”

Stack Trace in Java

Before understanding stack trace let use understands how Java applications are executed. Whenever a java application is executed, Heap is used to allocate memory at run time for various objects; apart from heap run time stack is created which is filled and removed for the methods and constructors calls. When the exception is raised the stack content is printed and hence is known as Stack trace. Looking at stack trace we can find the actual error and how our code reached their. The examples and diagrams are for understanding the concepts only. Continue reading “Stack Trace in Java”

How to install and add JRE7 in Eclipse?

If you want to install JRE7 in Eclipse then we have given step by step details in this article.

Download Java and Install it.

Download Eclipse and Open it.

In Eclipse window, Right Click on Java Project and Click Properties -> Click on Library Tab -> Select Java Build Path.

Continue reading “How to install and add JRE7 in Eclipse?”

Understanding String and String literals in Java

In any programming language String is an important data structure. In this post we will explore about String in Java.
String is a class which represents the characters. Being a class it provides many methods to work with a String. To create a String in Java we have two different ways and they are
  1. String x = “Hello”;
  2. String a = new String(“Hello”); Continue reading “Understanding String and String literals in Java”