Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

June 28, 2020

A Complete article on String class in Java

A Complete article on String class in Java




String var = new String("Google interview questions");

There is a high chance you came across the questions about String in most of the interviews.

What is a String?

We can say a string as a sequence of characters.

How a string implemented?

The string is implemented as an array of bytes that stores the character using a certain specific character encoding ( Say for example CP1144)

How this Character Encoding works?

Java uses UTF-16 character encoding internally to store a string.

Java supports a wide range of encoding.

Sample Java code :-

String crackInterview = new String("Facebook interview questions");

byte[]  elonmask=  crackInterview.getBytes();

byte[]  uber=  crackInterview.getBytes("CP1044");

will return different results.

How to create a String in Java?

There are two ways to create a string in java. One by String literal and other by new keyword.

String var = "Amazon interview questions"; ( using literals )

String var = new String("Microsoft interview questions"); ( using new operator)

There will be some differences in creating in these ways, right?
yes, while using new operator a string will be created in the string pool as well as in heap memory. 
on the other hand while using literals string will be created on spring pool

Then what is a string pool? why java using a String pool?

Simply a pool of string in heap memory.

String pool is an implementation of String interning concept, developed on flyweight design pattern. 

we can use the intern() method to put back the string created using a new operator to string pool.

Since the value of the string object does not change, we called String is immutable.

Why we kept the string in a pool?

Security and Immutable

What is the flyweight design pattern?

A creational software design pattern.

Minimizing memory usage by share similar data.

How String becomes mutable?

To become string a mutale one we need to implement either StringBuilder or StringBuffer class


@author : Konzernites

March 21, 2019

program to print sentence having maximum number of words in it

/**
 * sharing a program to print sentence having maximum number of words in it...
 *
 * As per my understanding, you may have one paragraph, which u can split() by passing "." . It will return String array of sentences.

Then u can iterate the sentence string array and split the sentence using " " (space), to get the array of words in the sentence. Then use the array.length to find the words in the sentence.

Using above approach u can find sentence with max words.

Plz share more details if there is any understanding gap.
 *
 */
package com.cfed.tutorials;

/**
 * @author Konzerntech
 *
 */
public class Paragraph {

private String paragraph = null;
private int maxWordsSize = 0;
private String maxWord = null;
private String maxSentanceWithWord = null;

public Paragraph(String paragraph) {
this.paragraph = paragraph;
}

public String getSentenceWithMaxWords() {
String [] sentenceList = paragraph.split("\\.");
for(String sentence : sentenceList) {
String[] wordList = sentence.split("\\s");

for(String word : wordList) {
if(word.length()>maxWordsSize) {
maxWordsSize = word.length();
maxWord = word;
maxSentanceWithWord = sentence;
}

}
}
return maxSentanceWithWord;
}


public String getSentenceWithBiggestWord() {
if(null!=paragraph) {
getSentenceWithMaxWords();
return maxSentanceWithWord;
}
return "";
}

public String gettheBiggestWord() {
if(null!=paragraph) {
getSentenceWithMaxWords();
return maxWord;
}
return "";
}

public static void main(String[] args) {

String s = "LONDON India’s onetime jeweler to the stars. Nirav Modi, has been arrested in London and faces extradition to India to face allegations that he defrauded lenders. Nirav Modi built global jewelry empire, according to authorities, on web of illegal loans guaranteed by state-owned Punjab National Bank.Celebrity Jeweler Suspected in $2 Billion Fraud Arrested in London, Faces Extradition to India.";
Paragraph p  = new Paragraph(s);
System.out.println(p.getSentenceWithMaxWords());
}
}




November 24, 2018

Comparing String with its permutation in java

/**
 * Consumerfed
 */
package com.cfed.logicz;

import java.util.Scanner;

/**
 * @author Consumerfed Regional office kozhikode
 *
 */
public class StringPermutation {

/**
* @param args
*/
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
sc.nextLine();
String[] inputs = new String[count];
for(int j =0;j<count;j++) {
inputs[j] = sc.nextLine();
}

for(String input : inputs) {
String str1 = input.split("\\s")[0];
String str2 = input.split("\\s")[1];
// String str1 = "konzerntech";
// String str2 = "consumerfed";
int len = 0;
if ((len = str1.length()) != str2.length()) {
System.out.println("NO");
} else {
int val1 = 0;
int val2 = 0;
for (int i = 0; i < len; i++) {
val1 = str1.charAt(i) + val1;
val2 = str2.charAt(i) + val2;
}
if (val1 != val2) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
}
}
}

October 06, 2018

Java Example for Word Count

/**
 * Word count in java
 *
 * Using regular expression for split by space
 */
package com.belazy.algorithm;
/**
 *
 * @author Murali Krishnan
 *
 */
public class WordCount {

/**
* @param regex
*/
public static void main(String[] regex) {
String s = "I always value my    friends";
// split by single space
String[] sw = s.split("\\s");
// + indicates one or more
String[] wc = s.split("\\s+");
System.out.println(sw.length);
System.out.println(wc.length);
}
}

Facebook comments