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
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
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]
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.
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
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
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
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
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); } } }
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.
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP