Found 7442 Articles for Java

Find the dimensions of 2D array in Java

Rahul Sharma
Updated on 24-Feb-2020 10:44:41

489 Views

Following example helps to determine the upper bound of a two dimensional array with the use of arrayname.length.https://www.tutorialspoint.com/javaexamples/arrays_upperbound.htm

How to declare a static String array in Java

Alshifa Hasnain
Updated on 20-Mar-2025 18:18:57

1K+ Views

In this article, we will learn the declaration of static Strings array in Java. Arrays can be used to store multiple values in one variable. Static array has a specific size which cannot be increased in the later part of the program after creation. What is a Static String Array? A static array is a declared array as static, which means that it is associated with the class, not with a class instance. This brings the array into a shared state among all instances of the class. Static variables are loaded when the class is loaded, even before class instances ... Read More

Returning an Array from a Method in Java

Giri Raju
Updated on 25-Feb-2020 05:21:22

277 Views

A method may also return an array. For example, the following method returns an array that is the reversal of another array -Examplepublic static int[] reverse(int[] list) {    int[] result = new int[list.length];    for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {       result[j] = list[i];    }    return result; }

Passing array to method in Java

Jai Janardhan
Updated on 25-Feb-2020 05:05:56

375 Views

Just as you can pass primitive type values to methods, you can also pass arrays to methods. For example, the following method displays the elements in an int array -Examplepublic static void printArray(int[] array) {    for (int i = 0; i < array.length; i++) {       System.out.print(array[i] + " ");    } }You can invoke it by passing an array. For example, the following statement invokes the print Array method to display 3, 1, 2, 6, 4, and 2 -printArray(new int[]{3, 1, 2, 6, 4, 2});

public access modifier in Java

Vrundesha Joshi
Updated on 24-Feb-2020 12:32:52

343 Views

A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its sub classes.ExampleThe following function uses public access control -public static void main(String[] arguments) {    // ... }The main() method of an application has to be ... Read More

protected access modifier in Java

Rishi Rathor
Updated on 24-Feb-2020 12:32:07

731 Views

Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.Protected access gives the subclass a chance to use the helper method or variable, while preventing a non-related class from trying to use it.ExampleThe following parent class uses protected access control, to allow its child class override openSpeaker() method - class AudioPlayer ... Read More

default access modifier in Java

Nancy Den
Updated on 24-Feb-2020 12:31:33

5K+ Views

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.ExampleVariables and methods can be declared without any modifiers, as in the following examples -String version = "1.5.1"; boolean processOrder() {    return true; }

private access modifier in Java

Daniol Thomas
Updated on 24-Feb-2020 12:30:54

527 Views

Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.Private access modifier is the most restrictive access level. Class and interfaces cannot be private.Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.ExampleThe following class uses private access control - public class Logger {    private String format;    public String getFormat() {       return this.format;    }    public void setFormat(String ... Read More

remove null value from a String array in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

4K+ Views

Following program creates an array with null values. Convert it a list with not-null values only and then get the array of that list.Exampleimport java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { String[] array = {"I", null, "love", null, "Java" }; List values = new ArrayList(); for(String data: array) { if(data != null) { values.add(data); } } String[] target = values.toArray(new String[values.size()]); for(String data: target) { System.out.println(data + " "); } } }OutputI love Java

Returning an array in Java

George John
Updated on 30-Jul-2019 22:30:21

228 Views

Yes, an array can be returned from a java function. See the example below − Example public class Tester { public static void main(String[] args) { int[] array = getData(); for(int i: array) { System.out.println(i); } } public static int[] getData() { int[] dataArray = {1, 2, 3, 4}; return dataArray; } } Output 1 2 3 4

Advertisements