Category Archives: Java

Reverse a byte array in Android

By | May 10, 2018

Here is a simple function to reverse a byte array in android. [java] public static void reverse(byte[] array) { if (null == array) { return; } int i = 0; int j = array.length – 1; byte tmp; while (j > i) { tmp = array[j]; array[j] = array[i]; array[i] = tmp; j–; i++; } } [/java]

What are static variables? Why it is used? What is it’s use? – A Common Interview Question.

By | November 7, 2014

The static keyword is used in java mainly for memory management. We may apply static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class. The static can be: variable (also known as class variable) method (also known as class method) block nested class Static variable If you… Read More »

How to find your Google Plus ID

By | September 27, 2012

This is so simple 1. Go to your Google + account ( 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: 4. The long numerical string in the URL is your Google+ ID. Here is CoderzHeaven’s from the URL above:… Read More »

Java Applet MouseEvents

By | April 22, 2012

A simple event based applet applications is described below First importing the necessary header files // Demonstrate the mouse event handlers. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code=”MouseEvents” width=300 height=100> </applet> */ Next step is to create a class which implements the “MouseListener” and “MouseMotionListener “ public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg =… Read More »

JComboBox in Java swing

By | April 20, 2012

This shows you how to add JcomboBox to JPanel import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import javax.imageio.ImageIO; import javax.swing.*; public class Example implements WindowListener { JFrame content_frame = new JFrame(); JPanel panel = new JPanel(); JComboBox Combonote; public Example() { content_frame.setTitle(“Contents”); content_frame.addWindowListener(this); content_frame.setSize(300, 300); content_frame.setVisible(true); content_frame.setLocationRelativeTo(null); content_frame.setResizable(false); ///cannot maximize content_frame.setVisible(true); showComboBox(); } private void showComboBox() { String test[]=… Read More »

Java check memory Allocation

By | April 7, 2012

[java] 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… Read More »

How to Convert a string to date in JAVA ?

By | April 3, 2012

Hi, For converting string to a date in JAVA use the following code. [java] import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) throws Exception { SimpleDateFormat myDateFormat = new SimpleDateFormat("dd/MM/yyyy"); Date dateToday = dateFormat.parse("25/06/2011"); System.out.println(myDateFormat.format(dateToday)); } } [/java] 🙂

How to find if a string starts with a particular word – JAVA ?

By | June 29, 2011

Hi, In order to find whether a given string starts with a particular string or not in JAVA, use the following code. [java] public class Main { public static void main(String[] args) { String urStr = "Coderz Heaven"; if (urStr.startsWith("Coderz")) { System.out.println("Yes! Your string starts with Coderz"); } else { System.out.println("No!"); } } } [/java]

Exception Handling – Divide by Zero – Java Example

By | June 25, 2011

Hi, Given below is a code snippet which will demonstrate an exception handling in Java with Divide by zero error. [java] public class MainClass { public static void main(String args[]) { int urAns, urDiv; try { urDiv = 0; urAns = 25 / urDiv; System.out.println("Do you really think this will print out? No! It won’t!"); } catch (ArithmeticException… Read More »

Simple String Concatenation Example in JAVA

By | June 25, 2011

Hi, Given below is a simple concatenation example in JAVA where two strings are joined together to form a single string. [java] public class MainClass { public static void main(String[] arg) { String yourString = "Coderz" + "Heaven"; System.out.println(yourString); } } [/java] And…the output will be.. “CoderzHeaven” 🙂

How to search for substring from a string in JAVA ?

By | June 25, 2011

Hi, For searching for a particular substring from a starting index of a string and get the starting index of it in JAVA, use the following sample of code. [java] public class MainClass{ public static void main(String[] arg){ String myStr = "coderzco"; int begIndex= 2; int myIndex= 0; myIndex = str.indexOf("co", begIndex); System.out.println(myIndex); } } [/java] It will… Read More »

How to search for characters from strings in JAVA ?

By | June 25, 2011

Hi, For searching for a particular character from a string and getting the index of that particular character, use the following code. [java] public class MainClass{ public static void main(String[] arg){ String myStr = "Coderz"; int strIndex = 0; strIndex = myStr.indexOf(‘o’); System.out.println(strIndex); } } [/java] It will print out the index of ‘o’, that’s 1. 🙂

Applet FlowLayout Example

By | May 16, 2011

The FlowLayout class puts components in a row, sized at their preferred size. Creates a new flow layout manager with the indicated alignment and the indicated horizontal and vertical gaps. The hgap and vgap arguments specify the number of pixels to put between components. [java] import java.applet.*; import java.awt.*; /* <applet code="FlowLayoutApplet" width=300 height=200> </applet> */ public class… Read More »

Swing JPanel with background image

By | March 7, 2011

This code add an image to Jpanel. JPanel panel1 = new JPanel(); JFrame recursion_frame = new JFrame(); BackgroundPanel stack_image1 = new BackgroundPanel(“Recursion.jpg”); panel1.setLayout(null); panel1.setLayout(new BorderLayout()); panel1.setBackground(Color.lightGray); panel1.setVisible(true); recursion_frame.setContentPane(panel1); ///adding to panel… recursion_frame.getContentPane().add(stack_image1); //adding image to panel…

Position swing window in center of the screen

By | March 7, 2011

In some case we need to position the window in the center of the screen . The same effect can be achieved by hard coding the center points .But in that case the window position change when the screen resolution changes This code will position the window in center irrespective of different screen resolutions public JFrame stack_frame =… Read More »

Creating a JMenubar in java swing

By | March 4, 2011

Creating a JMenubar is simple. JMenuBar menubar = new JMenuBar(); JMenu menu1 = new JMenu(“File”); JMenu menu2= new JMenu(“Help”); file = new JMenuItem(“Exit to menu”); edit = new JMenuItem(“Exit”); JMenuItem about = new JMenuItem(“About us”); f.setJMenuBar(menubar); menu1.add(file); menu1.add(edit); menu2.add(about); menubar.add(menu1); menubar.add(menu2); file.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //Action…… } }); edit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent… Read More »

Preventing Overriding in Java

By | February 25, 2011

Methods and variables can be ‘override’ in subclasses. (Yes, it’s a good feature too!). But what if we don’t want to ‘override’ our Methods and Variables in Java? It’s simple… Declare them using the keyword ‘final’ as modifier. That’s it. eg: [xml]final int myVariable = 79;[/xml] So the value of myVariable can never be changed any way. Also… Read More »

Creating Exceptions in JAVA

By | February 25, 2011

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… Read More »

Abstract Class in java

By | February 20, 2011

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… Read More »

How to create custom layout for your spinner in ANDROID? or Customizable spinner

By | February 20, 2011

This code helps you to customize the spinner in ANDROID. For that you have to create an XML file inside your layout folder as shown below and name it spinner.xml. You can give all properties that are available for TextView inside this which is going to be then applied for your spinner. [xml] <xml version="1.0" encoding="utf-8"?> <TextView xmlns:android=""… Read More »

Java Exception

By | February 19, 2011

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