Object Oriented Programming Articles

Page 484 of 589

how can I declare an Object Array in Java?

Arjun Thakur
Arjun Thakur
Updated on 24-Feb-2020 344 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

Read More

How do you find the sum of all the numbers in a java array

Arushi
Arushi
Updated on 24-Feb-2020 255 Views

Following program print the sum of the all the numbers in an array.Examplepublic class Tester { public static void main(String[] args) { int[] dataArray = {1, 2, 3, 4}; int sum = 0; for(int i: dataArray) { sum += i; } System.out.println(sum); } }Output10

Read More

How to convert a String to and fro from UTF8 byte array

Amit Sharma
Amit Sharma
Updated on 24-Feb-2020 3K+ 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")); ...

Read More

How do I reverse an int array in Java

Ali
Ali
Updated on 24-Feb-2020 255 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

Read More

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

Sreemaha
Sreemaha
Updated on 24-Feb-2020 248 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.

Read More

Converting ArrayList to String[] in java

Fendadis John
Fendadis John
Updated on 24-Feb-2020 525 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

Read More

Convert bytes to a string in java

seetha
seetha
Updated on 24-Feb-2020 665 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

Read More

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

Sreemaha
Sreemaha
Updated on 24-Feb-2020 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

Read More

Why use Java annotations?

varma
varma
Updated on 24-Feb-2020 388 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.

Read More

How to create a JAR file?

usharani
usharani
Updated on 24-Feb-2020 24K+ Views

You can create a JAR file using the following command.jar cf jar-file input-file(s)  You can also create JAR files using IDE’s. To create a JAR file using eclipse follow the procedure given below.Open the Jar File wizardThe Jar File wizard can be used to export the content of a project into a jar file. To bring up the Jar File wizard −In the Package Explorer select the items that you want to export. If you want to export all the classes and resources in the project just select the project.Click on the File menu and select Export.In the filter text box ...

Read More
Showing 4831–4840 of 5,881 articles
« Prev 1 482 483 484 485 486 589 Next »
Advertisements