![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
![Amit Sharma](https://www.tutorialspoint.com/assets/profiles/14294/profile/60_103411-1513053466.png)
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
![Johar Ali](https://www.tutorialspoint.com/assets/profiles/14293/profile/60_101691-1513053093.jpg)
77 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]
![Kumar Varma](https://www.tutorialspoint.com/assets/profiles/13883/profile/60_73481-1512711460.jpg)
15K+ Views
Use Arrays.copyOfRange() method to get a subarray.Exampleimport java.util.Arrays; public class Tester { public static void main(String[] args) { int[] array = new int[] {1, 2, 3, 4, 5}; int[] subArray = Arrays.copyOfRange(array, 0, 2); System.out.println("Array: "); for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("Sub array: "); for(int i = 0; i < subArray.length; i++) { System.out.print(subArray[i] + " "); } } }OutputArray: 1 2 3 4 5 Sub array: 1 2
![Rama Giri](https://www.tutorialspoint.com/assets/profiles/13881/profile/60_68188-1512710896.jpg)
532 Views
Following program shows how to remove negative numbers from an array.Exampleimport java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Tester { public static void main(String[] args) { List objArray = new ArrayList(); objArray.clear(); objArray.add(2); objArray.add(-3); objArray.add(4); System.out.println("Array before removing an element "+objArray); Iterator iterator = objArray.iterator(); while(iterator.hasNext()) { Integer next = iterator.next(); if(next < 0) { iterator.remove(); } } System.out.println("Array after removing an element"+objArray); } }OutputArray before removing an element [ 2, -3, 4 ] Array after removing an element [ 2, 4 ]
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
225 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
![Fendadis John](https://www.tutorialspoint.com/assets/profiles/13534/profile/60_64028-1512539837.jpg)
329 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
![Fendadis John](https://www.tutorialspoint.com/assets/profiles/13534/profile/60_64028-1512539837.jpg)
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); } } }
![Sai Nath](https://www.tutorialspoint.com/assets/profiles/13525/profile/60_63072-1512539005.jpg)
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
![seetha](https://www.tutorialspoint.com/assets/profiles/13549/profile/60_42938-1512640859.jpg)
451 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