Tutorialspoint

Java String startsWith()

import java.lang.*;

public class StringDemo {
   public static void main(String[] args) {
      String str = "www.tutorialspoint.com";
      System.out.println(str);

      //The start string to be checked
      String startstr1 = "tutorialspoint";
      String startstr2 = "tutorialspoint";

      //Checks that string starts with given substring and starting index
      boolean retval1 = str.startsWith(startstr1);
      boolean retval2 = str.startsWith(startstr2, 4);

      //Prints true if the string starts with given substring
      System.out.println("starts with " + startstr1 + " ? " + retval1);
      System.out.println("string " + startstr2 + " starting from index 4 ? " + retval2);
   }
}

Java Initialize and Compare Strings

public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials";
      String str2 = "point";
      // comparing str1 and str2
      int retval = str1.compareTo(str2);
      
      // prints the return value of the comparison
      if (retval < 0) {
         System.out.println("str1 is greater than str2");
      }
      else if (retval == 0) {
         System.out.println("str1 is equal to str2");
      } else {
         System.out.println("str1 is less than str2");
      }
   }
}

Java static Keyword

public class InstanceCounter {

   private static int numInstances = 0;

   protected static int getCount() {
      return numInstances;
   }

   private static void addInstance() {
      numInstances++;
   }

   InstanceCounter() {
      InstanceCounter.addInstance();
   }

   public static void main(String[] arguments) {
      System.out.println("Starting with " + InstanceCounter.getCount() + " instances");

      for (int i = 0; i < 500; ++i) {
         new InstanceCounter();
      }
      System.out.println("Created " + InstanceCounter.getCount() + " instances");
   }
}

Java program to check string as palindrome with library method

public class Palindrome {
   public static void main (String[] args) throws java.lang.Exception { 
      String str = "NITIN";
      String reverse = new StringBuffer(str).reverse().toString(); 
      if (str.equals(reverse)) 
         System.out.println("String is palindrome"); 
      else
         System.out.println("String is not palindrome"); 
   }
}

Java check string as palindrome Without library method

public class Palindrome {
   public static void main(String[] args) {
      String str = "SATYA";
      StringBuffer newStr =new StringBuffer();
         for(int i = str.length()-1; i >= 0 ; i--) {
            newStr = newStr.append(str.charAt(i));
         }
         if(str.equalsIgnoreCase(newStr.toString())) {
            System.out.println("String is palindrome");
         } else {
            System.out.println("String is not palindrome");	
         }
   }
}

Java ListIterator

package com.tutorialspoint;

import java.util.*;

public class LinkedListDemo {
   public static void main(String[] args) {

      // create a LinkedList
      LinkedList list = new LinkedList();

      // add some elements
      list.add("Hello");
      list.add(2);
      list.add("Chocolate");
      list.add("10");

      // print the list
      System.out.println("LinkedList:" + list);

      // set Iterator at specified index
      Iterator x = list.listIterator(1);

      // print list with the iterator
      while (x.hasNext()) {
         System.out.println(x.next());
      }
   }
}

Java 8 Optional Class

import java.util.Optional;

public class Java8Tester {

   public static void main(String args[]) {
      Java8Tester java8Tester = new Java8Tester();
      Integer value1 = null;
      Integer value2 = new Integer(10);
		
      //Optional.ofNullable - allows passed parameter to be null.
      Optional<Integer> a = Optional.ofNullable(value1);
		
      //Optional.of - throws NullPointerException if passed parameter is null
      Optional<Integer> b = Optional.of(value2);
      System.out.println(java8Tester.sum(a,b));
   }
	
   public Integer sum(Optional<Integer> a, Optional<Integer> b) {
      //Optional.isPresent - checks the value is present or not
		
      System.out.println("First parameter is present: " + a.isPresent());
      System.out.println("Second parameter is present: " + b.isPresent());
		
      //Optional.orElse - returns the value if present otherwise returns
      //the default value passed.
      Integer value1 = a.orElse(new Integer(0));
		
      //Optional.get - gets the value, value should be present
      Integer value2 = b.get();
      return value1 + value2;
   }
}

Java instanceof and its applications

class SuperClass {
   int value = 10;
}

class SubClass extends SuperClass {
   int value = 12;
}

public class Tester{
   
   public static void main(String[] args){
  
      SuperClass obj = new SubClass();
   
      //instanceof returns true for the complete Object Hierarchy
      if(obj instanceof SubClass){
         System.out.println("obj is instanceof SubClass");
      }
      if(obj instanceof SuperClass){
         System.out.println("obj is instanceof SuperClass");
      }
      if(obj instanceof Object){
         System.out.println("obj is instanceof Object");
      }
   
      SuperClass obj1 = null;
   
      //instanceof returns false for null
      if(obj1 instanceof SuperClass){
         System.out.println("null is instanceof SuperClass");
      }   
   
      SuperClass obj2 = new SuperClass();
   
      //instanceof returns false for the subclass
      if(obj2 instanceof SubClass){
         System.out.println("obj2 is instanceof SubClass");
      }
      if(obj2 instanceof SuperClass){
         System.out.println("obj2 is instanceof SuperClass");
      }
   }   
}

Java Iterators

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

public class Tester{

   public static void main(String[] args) throws ClassNotFoundException { 
      Vector<Integer> vector = new Vector<>();
      vector.add(1);vector.add(2);vector.add(3);

      System.out.println("Vector: ");
      Enumeration<Integer> enumeration = vector.elements();
      while(enumeration.hasMoreElements()){
         System.out.print(enumeration.nextElement() + " ");
      }

      List<Integer> list = new ArrayList<Integer>();
      list.add(1);list.add(2);list.add(3);

      System.out.println("\nList: ");
      Iterator<Integer> iterator = list.iterator();
      while(iterator.hasNext()){
         System.out.print(iterator.next() + " ");
      }

      System.out.println("\nList using ListItertor (Forward): ");
      ListIterator<Integer> listIterator = list.listIterator();
      while(listIterator.hasNext()){
         System.out.print(listIterator.next() + " ");
      }
      System.out.println("\nList using ListItertor (Backward): ");
      while(listIterator.hasPrevious()){
         System.out.print(listIterator.previous() + " ");
      }   
   }   
}

Java and multiple inheritance

interface Event {
   public void start();
}
interface Sports {
   public void play();
}
interface Hockey extends Sports, Event{
   public void show();
}
public class Tester{
   public static void main(String[] args){
      Hockey hockey = new Hockey() {
         public void start() {
            System.out.println("Start Event");
         }
         public void play() {
            System.out.println("Play Sports.");
         }
         public void show() {
            System.out.println("Show Hockey.");
         }
      };

      hockey.start();
      hockey.play();
      hockey.show();
   }   
}

1 2 3 4 5 6 7 ... 109 Next
Advertisements
Loading...