Found 9321 Articles for Object Oriented Programming

how can I declare an Object Array in Java?

Arjun Thakur
Updated on 24-Feb-2020 10:41:06

226 Views

Array of Object class can be created which can accept any type of object. During operation on such array, instanceof operator can be used.Examplepublic class Tester {    public static void main(String[] args) {       Object[] dataArray = new Object[3];       dataArray[0] = new Integer(0);       dataArray[1] = new String("1");       dataArray[2] = new Boolean(false);       for(Object data: dataArray) {          if(data instanceof Integer) {             System.out.println(((Integer) data).intValue());          }          if(data instanceof String) {             System.out.println(data);          }          if(data instanceof Boolean) {             System.out.println(((Boolean) data).booleanValue());          }       }    } }Output0 1 false

Converting ArrayList to String[] in java

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

333 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

how to convert vector to string array in java

Fendadis John
Updated on 24-Feb-2020 10:40:14

277 Views

Following program converts a vector into a array of String.Exampleimport java.util.Vector; public class Tester {    public static void main(String[] args) {       Vector data = new Vector();       data.add("A");       data.add("B");       data.add("C");       String[] strObjects = data.toArray(new String[data.size()]);       for(String obj: strObjects) {          System.out.println(obj);       }    } }

how to convert Object array to String array in java

Sai Nath
Updated on 30-Jul-2019 22:30:21

3K+ Views

As list.toArray() returns an Object[], it can be converted to String array by passing the String[] as parameter. See the example below.import java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { List data = new ArrayList(); data.add("A"); data.add("B"); data.add("C"); //Object[] objects = data.toArray(); String[] strObjects = data.toArray(new String[0]); for(String obj: strObjects) { System.out.println(obj); } } }OutputA B C

Convert bytes to a string in java

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

461 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

The most elegant ways to iterate the words of a java string.

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

2K+ 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

Loop through an array in Java

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

187 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);       }    } }

How do we compare String in Java

Giri Raju
Updated on 30-Jul-2019 22:30:21

167 Views

https://www.tutorialspoint.com/javaexamples/string_compare.htm

Why is char[] preferred over String for storing passwords?

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

132 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.

Why use Java annotations?

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

262 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