Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Showing posts with label STRING. Show all posts
Showing posts with label STRING. Show all posts

Sunday, 13 August 2017

Difference Between String and StringBuffer

Difference Between String and StringBuffer with Example

Difference Between String and StringBuffer

Here we will discuss what is the difference between String and StringBuffer in java. This is the most important topic for java interview.

So let's start with java String.

Java String

  • If the data which enclosed within double quote(" ") is by default treated as a String class.
  • String class object is immutable i.e after creating a string object, we cannot modify or update this string object.
  • The performance of String is slow because it creates a new instance every time when we concat a string.
  • String class consumes more memory when you concat too many string because every time it creates a new instance.
  • By default, no additional character memory space is created when we created String class object.
  • String class overrides the equals() method of Object class.


Java String Example

This the simple example of String class. Java String class belongs to the java.lang package.

class StringExample
{
public static void main(String args[])
{
String s = "India is a nice country";
System.out.println(s);
}
}

Output: India is a nice country 

Another Example of String with concat() Method

As we know that String is immutable i.e it cannot be modified or changed after it created, but if we will concat string(add another string) then it will make a new instance but still original string will not change.

class ConcatExample
{
public static void main(String args[])
{
String s = "Micro";
s.concat("soft");//concat() combines specified string at the end 
System.out.println(s);
}
}

Output: Micro

In the above example, Here s variable will print Micro not Microsoft but here it will make another new instance "Microsoft" in constant pool memory.



Java StringBuffer

  • If the data which enclosed within double quote(" ") is not by default treated as StringBuffer class.
  • StringBuffer class object is mutable i.e it can be modified or update after it created.
  • The performance of StringBuffer class is fast in comparison of String because it can be modified or change in existing string buffer object.
  • It consumes less memory because it doesn't create a new instance when we append another string.
  • By default, it takes 16 characters memory space when we create StringBuffer object.
  • StringBuffer class does not override the equals() method of Object class.

There are some StringBuffer methods in java e.g append() method, insert() method, replace() method etc.

There are given below some StringBuffer examples.

Java StringBuffer class is thread safe, StringBuffer is thread safe means, multiple threads cannot access it simultaneously.

StringBuffer append() Example

The append() method of StringBuffer class is used to add the new string to the original string.

class Demo
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("hello");
sb.append(" rahul");
System.out.println(sb);
}
}

Output: hello rahul


StringBuffer insert() Example

This is another StringBuffer example where we will use insert() method.

class Test
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("hello");
sb.insert(1,"world");
System.out.println(sb);
}
}

Output: hworldello

So there are many differences between String and StringBuffer and StringBuilder class in java.

I hope you have learned well StringBuffer vs String in this post.
Share:

Friday, 7 July 2017

Java String Conversion With Examples

 Java Convert

Here we will learn in detail some java conversion e.g How to convert String to int, int to String, String to long, long to String, String to float, float to String, String to double, double to String. This conversion is also very important in java programming interviews.
Java convert-Java String Converstion With Examples



So let's starts this beautiful java conversion concept.


( 1 ) Java String to int

We can easily convert String to int by using Integer.parseInt(String s) method.

Method Declaration

The parseInt is the static method of Integer class which is wrapper class in java. For example

public static int parseInt(String s)


Java String to int Example

This is a simple java convert String to int example.

public class StringToIntExample
{
public static void main(String args[])
{
String st = "50";
int i = Integer.parseInt(st);
System.out.println(i);
}
}


output : 50



( 2 ) Java int to String Example


We can easily convert int to String by using String.valueOf(int i) and Integer.toString(int i) methods.

Method Declaration

The valueOf is the static method of String class in java. For example

public static String valueOf(int i)

Java int to String Example

This is a simple java convert int to String example.

public class IntToStringExample
{
public static void main(String args[])
{
int i = 500;
String st = String.valueOf(i);
System.out.println(st);
}
}

output : 500

( 3 ) Java String to long

We can easily convert String to long by using Long.parseLong(String s) method.

Method Declaration

The parseLong() is the static method of Long class in java. For example

public static long parseLong(String s)

Java String to long Example

This is a simple java convert String to long example.

public class StringToLongExample
{
public static void main(String args[])
{
String st = "132547698";
long l = Long.parseLong(st);
System.out.println(l);
}
}


output : 132547698


( 4 ) Java long to String

We can easily convert long to String by using String.valueOf(long l) and Long.toString() method.

Method Declaration

The valueOf() is an static method of String class and it is an overloaded method in java. For example

public static String valueOf(long l)


Java long to String Example

This is a simple java convert long to String example.

public class LongToStringExample
{
public static void main(String args[])
{
long l = 12121212121212L;//add L
String s = String.valueOf(l);
System.out.println(s);
}
}

output : 12121212121212


( 4. 1 )  Java long to String - Using toString()

We can easily convert long to String by using Long.toString() method.

Method Declaration

The toString() method is the static method of Long class in java. For example

public static String toString(long l)

Java long to String Example - Using toString() method

This is a simple java convert to long to String by using toString() method.

public class Simple
{
public static void main(String args[])
{
long l = 123456789L;
String s = Long.toString(l);
System.out.println(s);
}
}

output : 123456789


( 5 ) Java String to float

We can easily covert String to float by using Float.parseFloat(String s) method.
  

Method Declaration

The parseFloat() is the static method of Float class in java. For example

public static int parseFloat(String s)

Java String to float Example

This is a simple java convert String to float example.

public class StringToFloatExample
{
public static void main(String args[])
{
String st = "5.6";
float f = Float.parseFloat(st);
System.out.println(f);
}
}

output : 5.6


( 6 ) Java float to String

We can easily convert float to String by using String.valueOf() and Float.toString() method.

Method Declaration

The valueOf() is the static method of String class in java. For example

public static String valueOf(float f)

Java float to String Example

This is a simple java convert float to String example.

public class FloatToStringExample
{
public static void main(String args[])
{
float f = 11.5F;//add F
String st = String.valueOf(f);
System.out.println(st);
}
}

output : 11.5


( 7 ) Java String to double

We can easily convert String to double by using Double.parseDouble() method.

Method Declaration

The parseDouble() is the static method of Double class in java. For example

public static double parseDouble(String s)

Java String to double Example

This is a simple java convert String to double example.

public class StringToDoubleExample
{
public static void main(String args[])
{
String st = "44.7";
double d = Double.parseDouble(st);
System.out.println(d);
}
}

Share:

Sunday, 25 June 2017

StringTokenizer In Java

 Java StringTokenizer

StringTokenizer

The StringTokenizer class allows an application to break a string into tokens(parts). The StringTokenizer class are available into java.util package.

In other words, StringTokenizer class breaks a string on the basis of delimiters(spaces or any special symbols).

for example : suppose there is a word or String "Java is a simple programming language". We can break this string into tokens(part) with the help of delimiters like spaces or any special symbols.

java
is
a
simple
programming
language

The StringTokenizer is a legacy class in java.


Constructors of StringTokenizer Class

There are some constructors of StringTokenizer class, these are

1) StringTokenizer(String str)

This constructor creates a string tokenizer for the specified string.

2) StringTokenizer(String str, String delim)

This constructor creates a string tokenizer for the specified string and delimiter.

3) StringTokenizer(String str, String delim, boolean retrunDelims)

This constructor creates a string tokenizer for the specified string delimiter and return value. If return value is true, delimiter characters are considered to be tokens. If return values is false, delimiter characters serve to separate tokens.


Methods of StringTokenizer Class

There are some certain methods of StringTokenizer class in java which is given below.

1) boolean hasMoreTokens()

This method tests if there are more tokens available from this tokenizer's string.

2) boolean hasMoreElements()

This method returns the same value as the hasMoreTokens method.

3) String nextToken()

This method returns the next token from this string tokenizer.

4) Object nextElement()

This method returns the same value as the nextToken() method, excepts that is declared return value is Object rather than String.

5) String nextToken(String delim)

This method returns the next token in this string tokenizer's string.

6) int countTokens()

This method returns the total numbers of tokens.


Java StringTokenizer Example

This is a simple example of string tokenizer class where we will break or tokenize this string " java is a simple programming language" on the basis of white spaces. Space is the default delimiter.

import java.util.StringTokenizer;
class StringTokenizerExample
{
public static void main(String args[])
{
StringTokenizer st = new StringTokenizer("java is a simple programming language", " ");//using space delimiter
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}

output : java
              is
              a
              simple
              programming
              language



Java StringTokenizer Example 1

This is another example of StringTokenizer, in this example we will break the string on the basis of 2 delimiters " space" and " comma". Space is the default delimiter.

import java.util.StringTokenizer;
public class StringTokenizerExample1
{
public static void main(String args[])
{
String s = "i am john, thank you";
StringTokenizer st = new StringTokenizer(s);//by using space
StringTokenizer st1 = new StringTokenizer(s, ",");//by using comma

System.out.println("tokenize by using space delimiter");

while(st.hasMoreTokens())

{

System.out.println(st.nextToken());

}

System.out.println("tokenize by using comma delimiter");


while(st1.hasMoreTokens())
{
System.out.println(st1.nextToken());
}
}
}

output : tokenize by using space delimiter
              i
              am
              john,
              thank
              you
              tokenize by using comma delimiter
              i am john
               thank you

Java StringTokenizer Example 2

This is the same example as above except that there we will use different method hasMoreElements() and nextElement() and in this example there is space between john and , e.g i am john , thank you.


import java.util.StringTokenizer;
public class StringTokenizerExample2
{
public static void main(String args[])

{

String s = "i am john , thank you";

StringTokenizer st = new StringTokenizer(s);//by using space

StringTokenizer st1 = new StringTokenizer(s, ",");//by using comma



System.out.println("tokenize by using space delimiter");


while(st.hasMoreElements())

{
System.out.println(st.nextElement());
}

System.out.println("tokenize by using comma delimiter");


while(st1.hasMoreElements())

{

System.out.println(st1.nextElement());
}
}
}

output : 
tokenize by using space delimiter
am
john
,
thank
you
tokenize by using comma delimiter
i am john
 thank you


Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate