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");
      }
   }
}

:active pseudo class program for button

<html>
   <head>
      <style>
         #thisButton:active { color: red}
      </style>
   </head>
   <body>
      <button id="thisButton">Click Me!!</button>
   </body>
</html>

:active pseudo class program for background color css

<html>
   <head>
      <style>
         body:active {
            background: seagreen;   
         }
      </style>
   </head>
   <body>
      <div>click anywhere in this window</div>
   </body>
</html>

:active pseudo class program for font-size css

<html>
   <head>
      <style>
         a:active {font-size:20px;}		
      </style>
   </head>
   <body>
      <a href="https://www.tutorialspoint.com">Click here to learn</a> 
   </body>
</html>

:active pseudo class program for color css

<html>
   <head>
      <style>
         a:active { color:green;}
      </style>
   </head>
   <body>
      <a href="https://www.tutorialspoint.com">Click here to learn</a> 
   </body>
</html>

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());
      }
   }
}

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