Java Articles

Page 10 of 450

What is the difference between System.out.println() and System.out.print() in Java?

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

The println() terminates the current line by writing the line separator string. The print() method just prints the given content.Examplepublic class Sample {    public static void main(String args[]) {       System.out.println("Hello");       System.out.println("how are you");       System.out.print("Hello");       System.out.print("how are you");    } }OutputHello how are you Hellohow are you

Read More

How to perform sort using Java?

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

You can sort an array using sort() method of the Arrays class.Exampleimport java.util.Arrays; public class MainClass {    public static void main(String args[]) throws Exception {       int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };       Arrays.sort(array);       printArray("Sorted array", array);       int index = Arrays.binarySearch(array, 1);       System.out.println("Didn't find 1 @ " + index);       int newIndex = -index - 1;       array = insertElement(array, 1, newIndex);       printArray("With 1 added", array);    }     ...

Read More

How to open a plain text file in Java?

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

You can access a plain text using the File class.Exampleimport java.io.File; public class ReadFile {    public static void main(String[] args) {       File f = null;       String str = "data.txt";       try {          f = new File(str);          boolean bool = f.canExecute();          String a = f.getAbsolutePath();          System.out.print(a);          System.out.println(" is executable: "+ bool);       } catch (Exception e) {          e.printStackTrace();       }    } }Output C:\Users\data is executable: true

Read More

How to find the vowels in a given string using Java?

Arushi
Arushi
Updated on 11-Mar-2026 39K+ Views

You can read a character in a String using the charAt() method. To find the vowels in a given String you need to compare every character in it with the vowel letters. Example public class FindingVowels {    public static void main(String args[]) {       String str = new String("Hi Welcome to Tutorialspoint");       for(int i=0; i

Read More

Is Java matcher thread safe in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data. Java provides the java.util.regex package for pattern matching with regular expressions.Matcher classA Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher() method on a Pattern object.The Instances of this class are not safe ...

Read More

What are parametrized constructors in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 14K+ Views

A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.Parameterized constructorsA parameterized constructor accepts parameters with which you can initialize the instance variables. Using parameterized constructor, you can initialize the class variables dynamically at the time of instantiating the class with distinct values.Syntaxpublic class Sample{    Int i;    public sample(int i){       this.i = i;    } }Examplepublic class Test {    String ...

Read More

Sort an arrays of 0’s, 1’s and 2’s using Java

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 489 Views

Given an array of 0, 1 and 2 sort the element in an order such that all the zeros come first before 1 and all the 2’s in the end. We have to sort all the elements of the array in-place.We can solve this problem using DNF (Dutch National Flag) Sorting Algorithm. For example, Input-1 −arr[ ]= {2, 0, 0, 1, 2, 1 }Output −0 0 1 1 2 2Explanation − Sorting the given array of elements containing 0, 1 and 2 using the DNF Sorting Algorithm, it will print the output as {0, 0, 1, 1, 2, 2}.Input-2 −arr[ ...

Read More

How to create a default constructor in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 4K+ Views

Default constructor (No-arg constructor)A no-arg constructor doesn’t accepts any parameters, it instantiates the class variables with their respective default values (i.e. null for objects, 0.0 for float and double, false for Boolean, 0 for byte, short, int and, long).There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.Rules to be rememberedWhile defining the constructors you should keep the following points in mind.A constructor does not have return type.The name of the constructor is same as the name of the class.A constructor cannot be abstract, final, static and Synchronized.You can use the access specifiers ...

Read More

What are various ways to compare time values in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

The LocalTime class represents the local time i.e. the time without time zone. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two times.Exampleimport java.time.LocalTime; public class Test {    public static void main(String args[]) {         LocalTime Time1 = LocalTime.of(10, 15, 45);       LocalTime Time2 = LocalTime.of(07, 25, 55);             Boolean bool1 = Time1.isAfter(Time2);         Boolean bool2 = Time1.isBefore(Time2);       if(bool1){          System.out.println(Time1+" is after "+Time2);       }else if(bool2){          System.out.println(Time1+" is ...

Read More

How to compare two dates in String format in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object Instantiate this class by passing desired format string.Parse the date string using the parse() method.The util.Date class represents a specific instant time This class provides various methods such as before(), after() and, equals() to compare two datesExampleOnce you create date objects from strings you can compare them using either of these methods as shown below −import java.text.ParseException; import ...

Read More
Showing 91–100 of 4,495 articles
« Prev 1 8 9 10 11 12 450 Next »
Advertisements