Java Articles

Page 117 of 450

How to return an array from a method in Java?

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 11-Mar-2026 6K+ Views

We can return an array in Java from a method in Java. Here we have a method createArray() from which we create an array dynamically by taking values from the user and return the created array.Exampleimport java.util.Arrays; import java.util.Scanner; public class ReturningAnArray {    public int[] createArray() {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created:: ");       int size = sc.nextInt();       int[] myArray = new int[size];       System.out.println("Enter the elements of the array ::");       for(int i=0; i

Read More

How to pull distinct values from an array in java?

Sharon Christine
Sharon Christine
Updated on 11-Mar-2026 1K+ Views

To pull distinct values in an array you need to compare each element of the array to all the remaining elements, in case of a match you got your duplicate element. One solution to do so you need to use two loops (nested) where the inner loop starts with i+1 (where i is the variable of outer loop) to avoid repetitions in comparison.Exampleimport java.util.Arrays; import java.util.Scanner; public class DetectDuplcate {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created::"); ...

Read More

How to switch data from an array to array list in java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 350 Views

The Arrays class of the java.util package provides you a method named asList(). This method accepts an array (of objects) and converts them into a list and returns it.Exampleimport java.util.Arrays; import java.util.List; public class ArrayToList {    public static void main(String args[]) {       Integer[] myArray = {23, 93, 56, 92, 39};       List list = Arrays.asList(myArray);       System.out.println(list);    } }Output[23, 93, 56, 92, 39]

Read More

What's the simplest way to print a Java array?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 265 Views

Generally, to print the contents of an array you need to use for loops, in addition to that you can directly print an array using the toString() method of this class. This method accepts an array as a parameter and returns it in String format.Exampleimport java.util.Arrays; public class PrintingArrays {    public static void main(String args[]) {       int[] myArray = {23, 93, 30, 56, 92, 39};       System.out.println(Arrays.toString(myArray));    } }Output[23, 93, 30, 56, 92, 39]

Read More

What is the difference between integer and floating point literals in Java?

Rahul Sharma
Rahul Sharma
Updated on 11-Mar-2026 1K+ Views

Integer literals represent fixed integer values like 900, 12, 400, -222 etc. (with in the integer range). Whereas, floating point literals represents fractional values i.e. numbers with decimal values like 25.53, 45.66, 58.66 etc. while writing these literals we should use the notation f or F as 25.53. Example public class StringExample { public static void main(String args[]){ int num1 = 100; float num2 = 30.0f; System.out.println("Value of integer:"+num1); System.out.println("Value of integer:"+num2); } } Output Value of integer:100 Value of integer:30.0

Read More

What is the difference between character literals and string literals in Java?

Ali
Ali
Updated on 11-Mar-2026 1K+ Views

Character literals represents alphabets (both cases), numbers (0 to 9), special characters (@, ?, & etc.) and escape sequences like , \b etc. Whereas, the String literal represents objects of String class. Example public class LiteralsExample { public static void main(String args[]){ char ch = 'H'; String str = "Hello"; System.out.println("Value of character: "+ch); System.out.println("Value of string: "+str); } } Output Value of character: H Value of string: Hello

Read More

What are the default values of instance variables whether primitive or reference type in Java?

Amit Sharma
Amit Sharma
Updated on 11-Mar-2026 3K+ Views

When we haven’t initialized the instance variables compiler initializes them with default values.For boolean type, the default value is false, for float and double types default values are 0.0 and for remaining primitive types default value is 0.Examplepublic class Sample {    int varInt;    float varFloat;    boolean varBool;    long varLong;    byte varByte;    short varShort;    double varDouble;    public static void main(String args[]){       Sample obj = new Sample();       System.out.println("Default int value ::"+obj.varInt);       System.out.println("Default float value ::"+obj.varFloat);       System.out.println("Default boolean value ::"+obj.varBool);       ...

Read More

Can static variables be called using className.variableName in Java?

Rahul Sharma
Rahul Sharma
Updated on 11-Mar-2026 2K+ Views

Class variables also known as static variables are declared with the static keyword There would only be one copy of each class variable per class, regardless of how many objects are created from it. You can access a class variable without instantiation using the class name as className.variableName. Example public class Test{ static int num = 92; public static void main(String args[]) throws Exception { System.out.println(Test.num); } } Output 92

Read More

What are final, abstract, synchronized non-access modifiers in Java?

Jai Janardhan
Jai Janardhan
Updated on 11-Mar-2026 527 Views

The abstract keyword is used to declare methods abstract methods and abstract classes. Once a method is declared abstract we should not specify body for those. And once a class is declared abstract it cannot be instantiated. Example abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; ...

Read More

What does synchronized modifier do in Java?

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ Views

The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier can be applied with any of the four access level modifiers. Example public class TestThread { public static Object Lock1 = new Object(); public static Object Lock2 = new Object(); public static void main(String args[]) { ThreadDemo1 T1 = new ThreadDemo1(); ThreadDemo2 T2 = new ThreadDemo2(); ...

Read More
Showing 1161–1170 of 4,498 articles
« Prev 1 115 116 117 118 119 450 Next »
Advertisements