Java Articles

Page 342 of 450

Method overloading with autoboxing and widening in Java.

Arjun Thakur
Arjun Thakur
Updated on 25-Feb-2020 315 Views

Widening refers to passing a lower size data type like int to a higher size data type like long. Method overloading is possible in such case. Example public class Tester {     public static void main(String args[]) {         Tester tester = new Tester();         short c = 1, d = 2;         int e = 1, f = 2;         System.out.println(tester.add(c, d));         System.out.println(tester.add(e, f));     }     public int add(short a, short b) {         System.out.println("short");         return a + b;     }     public int add(int a, int b) {         System.out.println("int");         return a + b;     } } Output Short 3 Int 3

Read More

Which is the best book for Java interview preparation

Krantik Chavan
Krantik Chavan
Updated on 25-Feb-2020 208 Views

Following books are on top from popularity and content wise and are a good resource to learn java programming from beginner to advance level.

Read More

Pass by reference vs Pass by Value in java

Giri Raju
Giri Raju
Updated on 25-Feb-2020 7K+ Views

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter. While Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter. In call by value, the modification done to the parameter passed does not reflect in the caller's scope while in the call by reference, the modification done to the parameter passed are persistent and changes are reflected in the caller's scope. But Java uses only call by value. It creates a copy of references ...

Read More

Passing array to method in Java

Jai Janardhan
Jai Janardhan
Updated on 25-Feb-2020 432 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});

Read More

default access modifier in Java

Nancy Den
Nancy Den
Updated on 24-Feb-2020 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; }

Read More

How can I clear or empty a StringBuilder in Java.

mkotla
mkotla
Updated on 24-Feb-2020 3K+ Views

You can either setLength to be 0 or create a new StringBuilder() instance. See the example below −Examplepublic class Tester { public static void main(String[] args) { StringBuilder builder = new StringBuilder(); builder.append("sample"); System.out.println(builder.toString()); builder.setLength(0); System.out.println(builder.toString()); builder.append("sample"); System.out.println(builder.toString()); } }Outputsample sample

Read More

Breaking out of nested loop in java

usharani
usharani
Updated on 24-Feb-2020 509 Views

Yes, break statement can be used in nested for loops. It works on the for loop in which it is applied. See the example below.Examplepublic class Tester { public static void main(String[] args) { for(int i = 0; i< 2; i++) { for(int j = 0; j < 2; j++){ if(i == 1) { break; } System.out.println("i = " + i+",j = " + j); } } } }

Read More

How to get rows and columns of 2D array in Java?

varun
varun
Updated on 24-Feb-2020 10K+ Views

Following example helps to determine the rows and columns of a two-dimensional array with the use of arrayname.length.ExampleFollowing example helps to determine the upper bound of a two dimensional array with the use of arrayname.length. public class Main {    public static void main(String args[]) {       String[][] data = new String[2][5];       System.out.println("Dimension 1: " + data.length);       System.out.println("Dimension 2: " + data[0].length);    } }OutputThe above code sample will produce the following result. Dimension 1: 2 Dimension 2: 5

Read More

How to convert comma seperated java string to an array.

Prabhas
Prabhas
Updated on 24-Feb-2020 6K+ Views

Yes, use String.split() method to do it. See the example below −Examplepublic class Tester { public static void main(String[] args) { String text = "This,is,a,comma,seperated,string."; String[] array = text.split(","); for(String value:array) { System.out.print(value + " "); } } }OutputThis is a comma seperated string.

Read More

How to print a byte array in Java?

seetha
seetha
Updated on 24-Feb-2020 18K+ Views

You can simply iterate the byte array and print the byte using System.out.println() method.Examplepublic class Tester { public static void main(String[] args) { byte[] a = { 1,2,3}; for(int i=0; i< a.length ; i++) { System.out.print(a[i] +" "); } } }Output1 2 3

Read More
Showing 3411–3420 of 4,498 articles
« Prev 1 340 341 342 343 344 450 Next »
Advertisements