Posts

Multithreading in Java

Multi threading in Java A thread is a lightweight sub-process, the smallest unit of processing.  Multiprocessing and multi threading, both are used to achieve multitasking. Multi threading  in java  is a process of executing multiple threads simultaneously. Advantages of Java Multi threading : 1) It  doesn't block the user  because threads are independent and you can perform multiple operations at the same time. 2) You  can perform many operations together, so it saves time . 3) Threads are  independent , so it doesn't affect other threads if an exception occurs in a single thread.

StringBuffer class Example Program

Java StringBuffer class Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed. Example Program : class StringBufferDemo { public static void main(String args[]) { StringBuffer str=new StringBuffer("WELCOME"); System.out.println(str); System.out.println(str.append(" to JAVA")); System.out.println(str.insert(4,"HOW")); System.out.println(str.delete(4,7)); System.out.println(str.reverse()); System.out.println(str.indexOf("WELCOME")); System.out.println(str.charAt(6)); System.out.println(str.substring(8));  } }

String in java

What is String in java Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object. How to create a string object? There are two ways to create String object: By string literal By new keyword String Literal Java String literal is created by using double quotes.  For Example:  String s= "welcome" ;    By new keyword String s= new  String( "Welcome" ); //creates two objects and one reference variable Example Program: class StringDemo { public static void main(String args[]) { String str=new String("Hello"); System.out.println(str); System.out.println(str.length()); System.out.println(str.indexOf("H")); System.out.println("Character at="+str.charAt(4)); System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); Syste...

Vector Example program

Java Vector Class Java Vector class comes under the java.util package. The vector class implements a growable array of objects. Like an array, it contains the component that can be accessed using an integer index. Vector is very useful if we don't know the size of an array in advance or we need one that can change the size over the lifetime of a program. Vector implements a dynamic array that means it can grow or shrink as required. It is similar to the ArrayList, but with two differences - Vector is synchronized. The vector contains many legacy methods that are not the part of a collections framework Three ways to create vector class object: 1.Vector vec=new Vector() Method 2: Syntax:  Vector object= new Vector(int initialCapacity) Method 3: Vector object = new vector ( int initialcapacity , capacityIncrement ) Example program : import  java.util.*;   public   class  VectorExample1 {   ...

One Dimensional Array

One Dimensional Array – Using Standard Method class OnedimensionalStandard { public static void main ( String args [ ] ) {      int [ ] a = new int [ 3 ] ; //declaration   a [ 0 ] = 10 ; //initialization   a [ 1 ] = 20 ;    a [ 2 ] = 30 ;    //printing array   System . out . println ( "One dimensional array elements are" ) ;      System . out . println ( a [ 0 ] ) ;      System . out . println ( a [ 1 ] ) ;      System . out . println ( a [ 2 ] ) ;      } } Using Scanner Read the array length as sc.nextInt() and store it in the variable len and declare an array int[len]. 2)  To store elements in to the array for i=0 to i<length of an array read the element using sc.nextInt() and store the element at the index a[i]. 3)  Display the elements of an array f...