Posts

Showing posts with the label Java

What is the uses of int compareTo () method in Java?

Java.lang.String.compareTo() There are  three  variants of  compareTo()  method. This article depicts about all of them, as follows 1. int compareTo(Object obj)   :  This method compares this String to another Object. Syntax : int compareTo(Object obj) Parameters : obj : the Object to be compared. Return Value : The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string. public class Test {    public static void main(String args[]) {       String str1 = "Strings are immutable";       String str2 = new String("Strings are immutable");       String str3 = new String("Integers are not immutable");       int result ...

What is char charAt (int index) in Java, and how can I use it?

Java String charAt() method The Java String charAt() method returns the character at the specified index. The index value should lie between 0 and length()-1. Signature: public char charAt(int index) Parameter: index- Index of the character to be returned. Return: returns character at the specified position. Exception: StringIndexOutOfBoundsException- If index is negative or greater then the length of the String. Example: To show working of  charAt()  method class Gfg {      public static void main(String args[])      {          String s = "Welcome! to Java Planet" ;              char ch = s.charAt( 3 );          System.out.println(ch);              ch = s.charAt( 0 );          Syst...

What are concatenating strings in Java, and how do you use it?

String Concatenation in Java In java, string concatenation forms a new string  that is  the combination of multiple strings. There are two ways to concat string in java: By + (string concatenation) operator By concat() method 1) String Concatenation by + (string concatenation) operator Java string concatenation operator (+) is used to add strings. For Example: class TestStringConcatenation1{ public static void main(String args[]){ String s="Sachin"+" Tendulkar"; System.out.println(s);//Sachin Tendulkar } } Output:Sachin Tendulkar The  Java compiler transforms  above code to this: String s=(new StringBuilder()).append("Sachin").append(" Tendulkar).toString(); In java, String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method. String concatenation operator produces a new string by appending the second operand onto the end of the first operand. The string concatenation oper...

What is the use of Thread.sleep() method in Java?

Thread.sleep() : The  java.lang.Thread.sleep(long millis)  method causes the currently executing thread to sleep for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. Declaration Following is the declaration for  java.lang.Thread.sleep()  method public static void sleep(long millis) throws InterruptedException Parameters millis  − This is the length of time to sleep in milliseconds. Return Value This method does not return any value. Exception InterruptedException  − if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown. Example The following example shows the usage of java.lang.Thread.sleep() method. import java.lang.*; public class ThreadDemo implements Runnable { Thread t; public void run() {  for (int i = 10; i < 13; i++) {  System.out.println(Thread.currentThread().ge...

What are control statements in java?

Image
Java Control Statements:  The Java control statements inside a program are usually executed sequentially. Sometimes a programmer wants to break the normal flow and jump to another statement or execute a set of statements repeatedly. The statements which break the normal sequential flow of the program are called control statements. A control statement enables decision making, looping and branching. Decision Making Statements: These statements decide the flow of the execution based on some conditions. If then If then else switch  if Statement: The Java if statement tests the condition. It executes the  if block  if condition is true. Syntax: if (condition){   //code to be executed   }   Example: //Java Program to demonstate the use of if statement.   public   class  IfExample {   public   static ...