Skip to main content

Posts

What is singletone Design pattern in java ?

Singleton design pattern is a Creational design pattern.It means create a single object or instance and make it global ,In other words, a class must ensure that only single object should be created and single object can be used by all other classes. There are two forms of singleton design pattern Early Instantiation: creation of instance at load time. (private   static  A obj= new  A();) Lazy Instantiation: creation of instance when required. (private   static  A obj; ) Advantage of Singleton design pattern Saves memory because object is not created at each request. Only single instance is reused again and again. To create the singleton class, we need to have static member of class, private constructor and static factory method. Static member: It gets memory only once because of static, itcontains the instance of the Singleton class. Private constructor: It will prevent to instantiate the Singleton class from outside the class. ...
Recent posts

Find 2nd Largest Number in java ?

import java.util.Scanner; public class SocondLargest {     public static void main(String[] args) {         int secondlargest = Integer.MIN_VALUE;         int largest = Integer.MIN_VALUE;         System.out.println(largest);         Scanner input = new Scanner(System.in);         System.out.println("Enter array values: ");         int arr[] = new int[5];         for (int i = 0; i < arr.length; i++) {             arr[i] = input.nextInt();             if (largest < arr[i]) {                 secondlargest = largest;                 largest = arr[i];  ...

How to find MacAddress in java ?

import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class MacAddress {     /**      * @param args      */     public static void main(String[] args) {         InetAddress ip;         try {             ip = InetAddress.getLocalHost();             System.out.println("Current IP address : " + ip.getHostAddress());             NetworkInterface network = NetworkInterface.getByInetAddress(ip);             byte[] mac = network.getHardwareAddress();             System.out.print("Current MAC address : ");           ...

File operation in java (open,read and write)

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class OpenFile { public static void createFile() { try { File file = new File("c:\\newfile.txt"); if (file.createNewFile()) { System.out.println("File is created!"); } else { System.out.println("File already exists."); } } catch (IOException e) { e.printStackTrace(); } } public static void writeFile() { try { FileWriter fw = new FileWriter("c:\\newfile.txt", true); BufferedWriter out = new BufferedWriter(fw); out.write("/cdfc"); out.close(); } catch (Exception ex) { ex.printStackTrace(); } } public static void readFile() throws IOException{ BufferedReader br=new BufferedReader(new FileReader("c:\\newfile.txt")); String lin...

Custom exceptional Handling in java

class InvalidAgeException extends Exception { private static final long serialVersionUID = 1L; InvalidAgeException(String s) { super(s); } } public class Custom { public static void age(int age) throws InvalidAgeException { if (age < 18) throw new InvalidAgeException("not valid"); else System.out.println("welcom to vote"); } public static void main(String[] args) { try { age(19); } catch (Exception ex) { System.out.println("exception occured " + ex); } } }

Write a program for stack operation in java ?

package com.chinta.datastructure; import java.util.EmptyStackException; import java.util.Stack; public class StackOperation { public static void showpush(Stack st,int a){ st.push(new Integer(a)); System.out.println("push :"+a); System.out.println("stack "+st); } public static void showpop(Stack st){ System.out.println("pop->"); Integer a=(Integer) st.pop(); System.out.println(a); System.out.println(st); } public static void main(String[] args) { // TODO Auto-generated method stub    Stack st=new Stack();             showpush(st, 23);             showpush(st, 24);             showpush(st, 25);             showpush(st, 26);             showpop(st);             showpop(st);             showpop(st);     ...

Why String is Immutable in java ?

Generally string is a sequence of characters. But in java, string is an object. String class is used to create string object. There are two ways to create String object: By string literal By new keyword String literal is created by double quote.For Example: String s="Hello"; String s1="Hello sir"; String s2="Hello sir"; Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance returns.  If the string does not exist in the pool, a new String object instantiates, then is placed in the pool.  Note: String objects are stored in a special memory area known as string constant pool inside the Heap memory.  String s=new String("Welcome");//creates two objects and one reference variable In such case, JVM will create a new String object in normal(nonpool) Heap memory and the literal "Welcome" will be placed in the string c...