Check Validate User

class UserDAO {
     
    public static String[]  getUsers() {
        String [] usernames = new String[] { "sridhar", "senthil", "siva" , "kamal" };
        return usernames;
    }
     
    public static boolean isValidUser(String username) {
         
        String[] users = getUsers();
        boolean isValid = false;
        for (String name : users) {
             
            if ( name.equals(username )) {
                isValid = true;
                break;
            }
        }
         
        return isValid;
         
    }
}
 
 
public class TestLogin {
 
    public static void main(String[] args) {
         
        //success case
        String username1 = "sridhar";
        System.out.println(  username1 + " is a Valid User ? " +  UserDAO.isValidUser(username1));
         
        //failure case
        String username2 = "abc";
        System.out.println(  username2 + " is a Valid User ? " +  UserDAO.isValidUser(username2));
         
    }
}

immutable object

  • The state of an immutable object cannot be changed after construction.
  • Immutable objects can play an important roll in threading.
  • A good example of an immutable objeect is String.
  • Any modification to an immutable object will result in  the creation of a new object.

Appending Strings

public class TestStringAppend {

    public static void main(String[] args) {

        String contentStr = "Core" + " " + "Java";
        System.out.println("Using String:" + contentStr);

        StringBuffer content = new StringBuffer();
        content.append("Core").append(" ").append("Java");
        System.out.println("Using StringBuffer:" + content);

        StringBuilder content2 = new StringBuilder();
        content2.append("Core");
        content2.append(" ");
        content2.append("Java");
        System.out.println("Using StringBuilder:"+ content2);

    }
}

String Utilities

StringUtils.java


package com.sridhar.string;

public class StringUtils{

	//equals() -  Determines the equality of two strings, ignoring case
	public static boolean isEqual(String name1, String name2) {
		return name1.equals(name2);
	}

	//equalsIgnoreCase() -  Determines the equality of two strings, ignoring case
	public static boolean isEqualsIgnoreCase(String name1, String name2) {
		return name1.equalsIgnoreCase(name2);
	}

	/*toUpperCase() - Returns a string, with lowercase characters converted to
	uppercase*/
	public static String convertToUpperCase( final String name) {
		return name.toUpperCase();
	}

	/*toLowerCase() - Returns a string, with uppercase characters converted to
	lowercase*/
	public static String convertToLowerCase( final String name) {
		return name.toLowerCase();
	}
	//trim() - Removes whitespace from both ends of a string
	public static String trimSpaces( final String name) {
		return name.trim();
	}

	//length() - Returns the number of characters in a string
	public static int calculateLength( final String name) {
		return name.length();
	}

	//isEmpty() - it returns true, if the string object is empty otherwise returns false
	public static boolean isEmpty( final String name) {
		return name.isEmpty();
	}

	//charAt() - Returns the character located at the specified index
	public static char getCharAt(final String name, int indexPos)
	{
		return name.charAt(indexPos);
	}

	/*concat() - returns a string with the value of
	the String passed in to the method appended to the end of the String*/
	public static String concat(String string, String string2) {

		return string.concat(string2);
	}

	//replace() - Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
	public static String replaceString(String something, String oldStr, String newStr) {
		return something.replace(oldStr,newStr);
	}

}

TestStringUtils.java


package com.sridhar.string;

public class TestStringUtils {

	public static void main(String[] args) {

		String content = "Chennai";

		//charAt()
		char character=StringUtils.getCharAt(content,4);
		System.out.println("Character in the position of 4 is: "+character);

		//concat()
		String concatenatedString = StringUtils.concat("call", " taxi");
		System.out.println("Concatenated String is: "+concatenatedString);

		//equals()
		boolean equalsResult = StringUtils.isEqual("java", "JAVA");
		System.out.println("Java is equal to JAVA: "+ equalsResult ); // is "false"

		//equalsIgnoreCase()
		boolean ignoreCaseresult = StringUtils.isEqualsIgnoreCase("java", "JAVA");
		System.out.println("Java is equal to JAVA: "+ ignoreCaseresult ); // is "true"

		//length()
		int stringLength = StringUtils.calculateLength("123456");
		System.out.println("Length of the string 0123456 is: "+stringLength);

		//replace()
		String replacedString = StringUtils.replaceString("Hardware","Hard","Soft");
		System.out.println("The String Hardware is replaced with: "+replacedString);

		//toUpperCase()
		String upperCaseString = StringUtils.convertToUpperCase("Core Java");
		System.out.println("Converted uppercase String is: "+upperCaseString);

		//toLowerCase()
		String lowerCaseString = StringUtils.convertToLowerCase("Core Java");
		System.out.println("Converted uppercase String is: "+lowerCaseString);

		//trim()
		String trimmedString = StringUtils.trimSpaces(" Some Content ");
		System.out.println("Trimmed String is : "+ trimmedString);

		//isEmpty()
		boolean isEmptyResult = StringUtils.isEmpty("Some Content");
		System.out.println("is that the string is empty :"+ isEmptyResult);
	}

}

Difference between String, StringBuffer, StringBuilder

Description String StringBuffer StringBuilder
Storage Area Constant String Pool Heap Heap
Modifiable No (immutable) Yes (mutable) Yes (mutable)
Thread Safe Yes Yes No
Performance Fast Very Slow Fast
package com.sridhar.string;

public class TestDifferentStringObjects {

	public static void main(String[] args) {
		String immutableObject1 ="abc";
		System.out.println("Immutable String object Creted and stored in String Pool:"+immutableObject1);
		String immutableObject2 = new String("def");
		System.out.println("Immutable String object Creted and stored in String Pool:"+immutableObject2);
		StringBuffer mutableObject1 = new StringBuffer("ghi");
		System.out.println("Thread safe Mutable String object Creted and stored in heap:"+mutableObject1);
		StringBuilder mutableObject2 = new StringBuilder("jkl");
		System.out.println("Non Thread safe Mutable String object Creted and stored in heap:"+mutableObject2);

	}

}