Skip to main content

Posts

Java and OOPS concepts

Java is an object oriented programming language which can be downloaded and installed in your local Machine from java.com To explain java binaries and tools requires block diagram which gives clear picture. For now I brief few important tools. JDK - java development toolkit JVM - Java virtual machine JRE - Java Run time environment From oracle site we can download JDK which is shipped along with JVM and JRM. Mostly java programmers need the JDK. For end users JRE alone is enough. Basic OOPS characteristics are Inheritance Encapsulation Abstraction and Polymorphism The above concepts collectively makes application software extendable and changeable with less effort. Also helps in Developing design patterns. Design patterns gives solutions for common challenges usually a programmer faces while development applications. Factory design pattern, Abstract design pattern, Builder pattern etc. I explain these Design patterns in another article. Inheritance : Inherita...

Core Java: Immutable objects and mutable objects with an example

Immutable objects and mutable objects with an example  In object oriented programming language, in short OOPS, every problem, precisely every requirement is viewed as an object. Understanding the problem in this point of view brings many good things possible in programming like encapsulation, abstraction, polymorphism and inheritance. Anyways this post is not intended for describing benefits of OOPS. Object could be any thing. Let us take Car  for an instance. Basically car is an object and it has name, wheels, metal body, engine, maximum speed,  gear box etc as properties. Let us consider the car name is Ferrari, maximum speed is 200 km per hr with 6 gears. While driving the car driver can change the speed of the car and change the gear etc. So while car is running it's current speed, current gear are considered to be state of the object. The current speed can be changed using accelerator and gear can be changed using gear box. These can be considered as ...

Swing JLabel demo with example

This article is continuation of previous post JFrame usage demo . Please read it before continuing. This post uses the same example code shown in  JFrame usage demo  with JLabel changes and introduces layout manager BorderLayout. Layout Manager: Layout manager is responsible to arrange components on containers. Usually every swing container is created with a default layout manager but can be changed with setLayout() api. For example JFrame uses BorderLayout as default layout amanger. Java program for JLabel 1  2   import java.awt.BorderLayout; 3   import java.awt.Color; 4   import java.awt.Font; 5  6   import javax.swing.JFrame; 7   import javax.swing.JLabel; 8   import javax.swing.SwingUtilities; 9  10  /** 11   * 12   * Simple class to demonstrate JLabel of swing toolkit 13   * 14   * @author Nagasharath 15   * 16  ...

Swing: JFrame usage demo

This post helps in understanding usage of JFrame class with an example Swing toolkit categorizes components into 2 types. They are: Containers Controls Containers allows Controls to be arranged on them. JPanel, JFrame and JDialog are few which are frequently used. Controls are components like Buttons, labels, tables etc. These controls are arranged on containers using different layout managers. All Swing class names starts with J stands for J ava which symbolizes swing is pure java!   Layout Managers: Layout manager is responsible for laying out controls on containers according to the business requirement. Example layout managers are BorderLayout, GridLayout, GridBagLayout and FlowLayout. Few points about JFrame: JFrame is considered as a main window of the application.for almost all swing based applications All other controls and containers are created as child components of JFrame JFrama holds special components like  Menu bar, Tool bar etc...

Java Multi threaded programming basics with Reentrant lock

Java Multi threaded programming basics with Reentrant locks As we have seen in earlier post that implicit locking mechanism achieved using synchronized keyword slows down the application performance. Hence java concurrency api gives explicit locking mechanism to achieve mutual exclusion among shared object. How using explicit locks are better than implicit lock? Explicit lock acquires lock per Thread basis and not per method invoke basis. It is inverse in the case of implicit lock.   If a method is synchronized then every invocation of that method involves acquiring and releasing of lock. This process really slows down the application performance.  Hence it is always good idea to prefer reentrent lock or explicit lock to implicit lock. ReentrantLock is a class available in java.util.concurrency package lock() and unlock() methods are used used to acquire and release the lock. Condition class is used in place of Object class wait(), notifyAll() and notify(). ...

Java desktop GUI.

Graphical user interface in short GUI makes computer users easy to understand and use the application software comfortably. Almost all modern programming languages allows application developers to develop GUIs. Java has AWT/Swing as default API as part of Java Foundation classes in short JFC which are shipped with JDK. Hence these toolkits can be used directly in our applications with out adding external libraries like jars to our application's class path. But there are some other toolkits which found useful and industry endorsed in developing GUI.      SWT       JFaces [Framework for SWT]      Java FX      Swing      AWT AWT: Abstract window toolkit is available since java first version. AWT uses native operating system resources like Fonts, colors and components like table, tree, dialogues, windows etc. Few notable points about AWT: AWT is heavy weight. It uses OS resources There are l...

Java Multi threaded programming basics

All modern computer hardware architectures and operating systems are capable and optimized for multi processor system and multi threaded system.  Process Vrs Thread: Process is heavy weight where as Thread is light weight. Thread is light weight because process uses it's own address space and thread shares the Process's address space. So Threads are considered to be subset of a process. more over threads consumes less OS resources in terms of physical memory and uses CPU time at most. Junior programmers think concurrency is hard.  Experienced programmers think concurrency is easy.  Senior programmers think concurrency is hard.                                                                                    _ Java Concurrency Essentials by Martin ...