Skip to main content

Posts

Showing posts with the label example

CHEAT SHEET for java List interface, LinkedList and ArrayList

Consider Below points before choosing List concrete implementations for your use case:  ArrayList is a linear data structure and  re-sizable/dynamic array   Initial size can be given. and if not given the default size is 10   It is always good practice to give initial size and advised not to give very large number as size   Good programmers prefer ArrayList to LinkedList if retrieval operations is done more frequently than insertion and deletions of elements.   Better choose LinkedList if insertion and deletion operations are done frequently compared to retrieval operations.   Lists are not sorted. if required use Collections.sort() to sort   Collections.sort() uses optimized merge sort [tim sort] on ArrayList   However legacy merge sort also can be used using a switch. But it is going to be deprecated  in near future  The major difference between ArrayList and LinkedList is RandomAccess behavior...

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: 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 ...

Simple program which demonstrates Swing/AWT Grid layout

Simple program which demonstrates Swing/AWT Grid layout: This post is intended to the beginners of swing/awt. This below program creates a JFrame with 2 labels, 2 textfields and 2 buttons. import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; /**  * @author Nagasharath Kopalle  *  * Simple program to demontrate Gridlayout  *  */ public class GridLayoutDemo extends JFrame { private JLabel labelName = null; private JTextField textFieldName = null; private JLabel labelPassword = null; private JPasswordField passwordFieldPassword = null; private JButton buttonSubmit = null; private JButton buttonCancel = null; /** * */ public GridLayoutDemo() { initComponents(); } /** * instantiates components and adds properties. */ void initComponents() { this.setTitle("Grid Layout Dem...

Eclipse Java Editor Shortcuts

Eclipse Oxygen Java editor Shortcuts helps in editing source code more comfortably. Frequently used default shortcuts are listed below.  Description Shortcut Delete Line CTRL + D Copy Line CTRL + ALT + Down Arrow Add method level, class level, member level Comments SHIFT + ALT + J Format Java Source code CTRL + SHIFT + F Make line commented CTRL + / Make multiple lines [block] commented   CTRL + SHIFT + / Search selected similar word within the source document CTRL + K Select line from beginning to end SHIFT + End key Select line from end to beginning SHIFT + Home key Select Word by word in same line CTRL + SHIFT + Right Arrow Move line above ALT + Up Arrow Move line below ALT + Down Arrow Shortcuts can ...