Convert String to and from UTF8 Byte Array

Amit Sharma
Updated on 24-Feb-2020 10:36:31

2K+ Views

Following example will showcase conversion of a Unicode String to UTF8 byte[] and UTF8 byte[] to Unicode byte[] using Reader and Writer classes.ExampleIOTester.javaimport java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.charset.Charset; import java.text.ParseException; public class I18NTester {    public static void main(String[] args) throws ParseException, IOException {    String input = "This is a sample text" ;    InputStream inputStream = new ByteArrayInputStream(input.getBytes());    //get the UTF-8 data Reader    reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));        //convert UTF-8 to Unicode    int data = reader.read();    while(data != ... Read More

Reverse an Int Array in Java

Ali
Ali
Updated on 24-Feb-2020 10:35:31

224 Views

Following program reverses an int array.Examplepublic class Tester {    public static void main(String[] args) {       int[] numbers = {1,2,3,4,5};       //swap the numbers till the midpoint comes       for (int start = 0, end = numbers.length - 1;       start

Print a Java Array in the Simplest Way

Johar Ali
Updated on 24-Feb-2020 10:34:41

137 Views

To make things simple, convert the array to list and then print it.Exampleimport java.util.Arrays; import java.util.List; public class Tester {    public static void main(String[] args) {       Integer[] numbers = {1,2,3,4,5};       List list = Arrays.asList(numbers);       System.out.println(list);    } }Output[1,2,3,4,5]

Why Char is Preferred Over String for Storing Passwords

Sreemaha
Updated on 24-Feb-2020 10:31:39

219 Views

Yes, Storing password in String object is not safe for following reasons −String objects are immutable and until garbage collected, they remain in memory.String being plain text can be tracked in memory dump of the application.In log, String based password may be printed which can cause a problem.Char[] can be cleared or wiped out after the job is done.

Convert ArrayList of Strings to String in Java

Fendadis John
Updated on 24-Feb-2020 10:28:51

500 Views

Following program is converting an ArrayList to String[];Exampleimport java.util.ArrayList; import java.util.List; public class Tester {    public static void main(String[] args) {       List names = new ArrayList();       names.add("A");       names.add("B");       names.add("C");       String[] nameArray = names.toArray(new String[names.size()]);       for(String name: nameArray) {          System.out.println(name);       }    } }OutputA B C

Convert Bytes to a String in Java

seetha
Updated on 24-Feb-2020 10:26:35

635 Views

Use String(byte[]) constructor to convert byte[] to String.Examplepublic class Tester {    public static void main(String[] args) {       String test = "I love learning Java";       byte[] bytes = test.getBytes();       String converted = new String(bytes);       System.out.println(converted);    } }OutputI love learning Java

Elegant Ways to Iterate Words of a Java String

Sreemaha
Updated on 24-Feb-2020 10:25:38

3K+ Views

Just split the string based on space and then iterate it. See the example below −Examplepublic class Tester {    public static void main(String[] args) {       String test = "I love learning Java";       String[] subStrings = test.split(" ");       for(String subString: subStrings) {          System.out.println(subString);       }    } }OutputI love learning Java

Java String equals vs == Operator

vanithasree
Updated on 24-Feb-2020 10:21:12

277 Views

String.equals() compares the content while == checks whether the references are pointing to the same object or not.ExampleSee the illustration example below −public class Tester {    public static void main(String[] args) {       String test = new String("a");       String test1 = new String("a");       System.out.println(test == test1);       System.out.println(test.equals(test1));    } }Outputfalse true

Loop Through an Array in Java

radhakrishna
Updated on 24-Feb-2020 10:19:29

311 Views

Following example shows how to loop through an array using a foreach loop.public class Tester {    public static void main(String[] args) {       int[] dataArray = {1, 2, 3, 4};       for(int i: dataArray) {          System.out.println(i);       }    } }

Why Use Java Annotations

varma
Updated on 24-Feb-2020 10:15:44

353 Views

Annotations provide information about java elements. Annotations can be interpreted by compiler or IDE at compile time or by JVM at runtime. Annotations can be usedto show attributes of an element: e.g. @Deprecated, @Override, or @NotNullto describe the purpose of an element of the framework, e.g. @Entity, @TestCase, @WebServiceto describe the behavior of an element: @Statefull, @TransactionBefore Java 5, XML was primarily used to store information about java objects, with annotations, this information can be stored within the java code itself.

Advertisements