Found 9152 Articles for Object Oriented Programming

How to create a generic array in java?

Abhinanda Shri
Updated on 30-Jul-2019 22:30:20

233 Views

No, we can’t create generic arrays in java.

How to add items to an array in java dynamically?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:20

11K+ Views

Since the size of an array is fixed you cannot add elements to it dynamically. But, if you still want to do it then, Convert the array to ArrayList object.Add the required element to the array list.Convert the Array list to array.Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class AddingItemsDynamically {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array :: ");       int size = sc.nextInt();       String myArray[] = new String[size];       System.out.println("Enter elements of the array ... Read More

How to overwrite a specific chunk in a byte array using java?

Abhinaya
Updated on 16-Jun-2020 10:02:01

2K+ Views

Java provides a ByteBuffer class which allows you to wrap an array into a byte buffer using its wrap() method. Once you did that you can replace the contents of the buffer using the position(): To select the starting position and, put(): To replace the data methods:ExampleLive Demoimport java.nio.ByteBuffer; public class OverwriteChunkOfByteArray {    public static void main(String args[]) {       String str = "Hello how are you what are you doing";       byte[] byteArray = str.getBytes();       System.out.println("Contents of the byet array :: ");             for(int i = 0; i

What are the different types of keywords in Java?

varma
Updated on 18-Feb-2020 11:33:18

2K+ Views

Following are the different types of keywords in java—Access modifiers − private, protected, public.Class, method, variable modifiers− abstract, class, extends, final, implements, interface, native, new, static, strictfp, synchronized, transient, volatile.Flow control− break, case, continue, default, do, else, for, if, instanceof, return, switch, while.Package control− import, package.Primitive types− boolean, byte, char, double, float, int, long, short.Error handling− assert, catch, finally, throw, throws, try.Enumeration− enum.Others− super, this, void.Unused− const, goto.

How to declare Java array with array size dynamically?

Ramu Prasad
Updated on 19-Feb-2020 12:09:26

2K+ Views

To declare array size dynamically read the required integer value from the user using Scanner class and create an array using the given value:Exampleimport java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray {    public static void main(String args[]) {       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       System.out.println("Enter the elements of the array one by one ");             for(int i = 0; i

Is null a keyword in Java?

usharani
Updated on 30-Jul-2019 22:30:20

551 Views

No, null is not a keyword. Though they seem like keywords null, true and, false are considered as literals in Java.

Is main a keyword in Java?

varun
Updated on 30-Jul-2019 22:30:20

945 Views

No, main is not a keyword in Java.

Are ‘this’ and ‘super’ keywords in Java?

Prabhas
Updated on 30-Jul-2019 22:30:20

380 Views

Yes, this and super are keywords in Java. Where ‘this’ is used as a reference of the current object and, ‘super’ is used as a reference to the superclass object.

What does the native in Java stand for?

seetha
Updated on 30-Jul-2019 22:30:20

226 Views

A native method in Java is a method whose implementation is written in other languages such as c and c++.The ‘native’ keyword is used as a method to indicate that it is implemented in another language.

How to define an array size in java without hardcoding?

Sravani S
Updated on 19-Feb-2020 12:08:44

579 Views

To avoid hard coding you can read the size of the array from the user using command line arguments of the reader classes like Scanner. Then using this value create an array:Exampleimport java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray {    public static void main(String args[]) {       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       System.out.println("Enter the elements of the array one by one ");       for(int i=0; i

Advertisements