0% found this document useful (0 votes)
99 views2 pages

Creating and Encoding Strings in Java

The document discusses different ways to create String objects in Java. It explains that strings can be created using string literals or the String constructor, passing in a character array. Once created, String objects are immutable so their value cannot be changed. The getBytes() method is described as encoding a String into bytes using a specified or default character set. Examples are provided to demonstrate getting the bytes using different character sets.

Uploaded by

zparu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views2 pages

Creating and Encoding Strings in Java

The document discusses different ways to create String objects in Java. It explains that strings can be created using string literals or the String constructor, passing in a character array. Once created, String objects are immutable so their value cannot be changed. The getBytes() method is described as encoding a String into bytes using a specified or default character set. Examples are provided to demonstrate getting the bytes using different character sets.

Uploaded by

zparu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Creating Strings:

The most direct way to create a string is to write: String greeting = "Hello world!"; Whenever it encounters a string literal in your code, the compiler creates a String object with its valuein this case, "Hello world!'. As with any other object, you can create String objects by using the new keyword and a constructor. The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters: public class StringDemo{

public static void main(String args[]){ char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloString = new String(helloArray); [Link]( helloString ); } } This would produce following result: hello Note: The String class is immutable, so that once it is created a String object cannot be changed. If there is a necessity to make alot of modifications to Strings of characters then you should use String Buffer & String Builder Classes.

This method has following two forms:

getBytes(String charsetName): Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array. getBytes(): Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

Syntax:
Here is the syntax of this method: public byte[] getBytes(String charsetName) throws UnsupportedEncodingException

or

public byte[] getBytes()

Parameters:
Here is the detail of parameters:

charsetName -- the name of a supported charset.

Return Value :

This method returns the resultant byte array

Example:
import [Link].*;

public class Test{

public static void main(String args[]){ String Str1 = new String("Welcome to [Link]");

try{ byte[] Str2 = [Link](); [Link]("Returned Value " + Str2 );

Str2 = [Link]( "UTF-8" ); [Link]("Returned Value " + Str2 );

Str2 = [Link]( "ISO-8859-1" ); [Link]("Returned Value " + Str2 );

}catch( UnsupportedEncodingException e){ [Link]("Unsupported character set"); } } } This produces following result: Returned Returned Returned Value [B@192d342 Value [B@15ff48b Value [B@1b90b39

You might also like